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

조회 수: 1 (최근 30일)
Chris
Chris 2014년 4월 15일
댓글: Chris 2014년 4월 15일
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