Why do I get the error message "Index exceeds matrix dimensions."
1 次查看(过去 30 天)
显示 更早的评论
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')
0 个评论
回答(1 个)
Abhisek Roy
2016-2-4
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!