ODE45 help with function (numel(y)=1 error)

1 view (last 30 days)
Hello, I am trying to solve a system of ODEs with the following function and solver:
function dy=response_h(t,y)
load('Matrices.mat') %loads the workspace to obtain coefficients
dy=zeros(2,1);
K=Stiff(1,1);
C=C_damp(1,1);
M=M_inertia(1,1);
dy(1)=y(2);
dy(2)=-(K/M)*y(1)-(C/M)*y(2);
The ode45 solver (in the script that the Matrices.mat workspace is from):
[T,Yh]=ode45(@response_h,[0 100],[1 0]);
[T,Ya]=ode45(@response_a,[0 100],[1 0]);
[T,Yb]=ode45(@response_b,[0 100],[1 0]);
plot(T,Yh(:,1))
figure
plot(T,Ya(:,1))
figure
plot(T,Yb(:,1))
The other response functions are identical to response_h only they call out different coefficients from the respective matrices.
The error I am getting:
??? Attempted to access y(2); index out of bounds because numel(y)=1.
Error in ==> response_h at 7
dy(1)=y(2);
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> AA554HW1 at 139
[T,Yh]=ode45(@response_h,[0 100],[1 0]);
I am relatively new to these types of MATLAB functions, but I do know that the y(2) element doesn't seem to exist, even though I have the correct number of elements in my I.C. vector. Any help for this would be great. Thanks!

Accepted Answer

ragesh r menon
ragesh r menon on 15 Apr 2014
I don't think there is an error in the m script. But this error is shown because you are defining another "y" probably a local y which is another scalar value. See the data "matrices" which you are loading . I think there could be a scalar y in that
  1 Comment
Chris
Chris on 15 Apr 2014
That looks like it was the problem. I defined "y" at the beginning part of my script (it is much longer than posted), and I missed it. Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!