Filter löschen
Filter löschen

Index exceeds the number of array elements (2)

2 Ansichten (letzte 30 Tage)
Alli Krininger
Alli Krininger am 16 Nov. 2019
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);

Antworten (1)

Erivelton Gualter
Erivelton Gualter am 16 Nov. 2019
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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by