Why do I get the error message "Index exceeds matrix dimensions."

1 view (last 30 days)
Martin
Martin on 27 Jan 2016
Answered: Abhisek Roy on 4 Feb 2016
The complete error message:
Index exceeds matrix dimensions.
Error in @(t,u,g,L)[u(2);-g/L*sin(u(1))]
Error in @(t,u)f(t,u,g,L)
Error in odearguments (line 87)
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);
The questioning is about a pendulum that have different starting values(10,30,....,170)of angle phi0. and I aren´t able to solv it. Thanks in advance :)
The code:
L=0.1; g=9.82;
phi0=linspace(10*pi/180,170*pi/180,9);
f=@(t,u,g,L)[u(2); -g/L*sin(u(1))];
tspan=linspace(0,1,200);
o=zeros(size(phi0));
u0=[phi0;o];
options=odeset('Events',@funevent);
for i=1:1
[t,U]=ode45(@(t,u)f(t,u,g,L),tspan,u0(i),options);
end
subplot(1,2,1), plot(t,U(:,1),'g')
subplot(1,2,2), plot(U(:,1),U(:,2),'g')

Answers (1)

Abhisek Roy
Abhisek Roy on 4 Feb 2016
Hi Martin,
The error occurred as u0 provided to ode45 should be a [2,1] vector as the dynamics has two states. So change u0(i) to u0(:,i) and you should be able to integrate it. Also you have not defined funevent function, so define it before you run again, or you can remove it also.

Community Treasure Hunt

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

Start Hunting!

Translated by