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

1 次查看(过去 30 天)
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!

采纳的回答

ragesh r menon
ragesh r menon 2014-4-15
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 个评论
Chris
Chris 2014-4-15
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by