Index exceeds the number of array elements (2)

1 回表示 (過去 30 日間)
Alli Krininger
Alli Krininger 2019 年 11 月 16 日
回答済み: Erivelton Gualter 2019 年 11 月 16 日
I am getting an index error for the code below
tspan=linspace(0,10,100);
init=[5,0]; %=[t,z]
[t,y]=ode45(@(t,y)dsolve(t,y), tspan,init);
plot(t,y)
tn=(y(2)*3/1)
function dx=dsolve(t,y)
dx(1,1)=y(2)*2+y(4)*2;
dx(2,1)=-2*y(1)+3*y(2);
dx(3,1)=y(3)+2*y(1);
dx(4,1)=y(1)+2*y(2)+3*y(3)+4*y(4);
end
output:
Index exceeds the number of array elements (2).
Error in blahjifedfjl>dsolve (line 12)
dx(1,1)=y(2)*2+y(4)*2;
Error in blahjifedfjl>@(t,y)dsolve(t,y)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in blahjifedfjl (line 5)
[t,y]=ode45(@(t,y)dsolve(t,y), tspan,init);

回答 (1 件)

Erivelton Gualter
Erivelton Gualter 2019 年 11 月 16 日
Hi Ali,
Your dsolve function has 4 states variables: dx(1,1), dx(2,1), dx(3,1), and dx(4,1). Therefore you need an initial variable of size equalt o 4 as weel. Let's say you have init=[5,0, 0, 0];
tspan=linspace(0,10,100);
init=[5,0,0,0]; %=[t,z]
[t,y]=ode45(@(t,y) dsolve(t,y), tspan,init);
plot(t,y)
function dx=dsolve(t,y)
dx(1,1)=y(2)*2+y(4)*2;
dx(2,1)=-2*y(1)+3*y(2);
dx(3,1)=y(3)+2*y(1);
dx(4,1)=y(1)+2*y(2)+3*y(3)+4*y(4);
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by