Borrar filtros
Borrar filtros

Index exceeds the number of array elements (2)

2 visualizaciones (últimos 30 días)
Alli Krininger
Alli Krininger el 16 de Nov. de 2019
Respondida: Erivelton Gualter el 16 de Nov. de 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);

Respuestas (1)

Erivelton Gualter
Erivelton Gualter el 16 de Nov. de 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

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by