ode45 array size error
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Kevin Brinneman
il 8 Nov 2018
Commentato: Kevin Brinneman
il 8 Nov 2018
Hello everyone,
I have a lenghtly code but pretty much i'm only having 1 problem.
I have the following line of code:
[t,xdot] =ode45(@thetardynamics,[(k-1)*h k*h],X(:,k),options);
and whenever i run my script i get the following error:
Index in position 2 exceeds array bounds.
Error in problemSolver_4 (line 22) [t,xdot] =ode45(@thetardynamics,[(k-1)*h k*h],X(:,k),options);% [t,y] = ode45(odefun,tspan,y0,options)
I know it has to do with the size of the matrix but where exactly is it index 2? Does it mean at the output xdot or is inside the funcion that i have named as thetardynamics.
Thabks for the help
4 Commenti
Risposta accettata
Stephan
il 8 Nov 2018
Modificato: Stephan
il 8 Nov 2018
Hi,
lot of bugs - find the partially fixed version atacched. The ode45 part now runs - but there is a line:
du=[eye(p) zeros(2,2*(np-1))]*DU;
where you did not specify or define p - in this i can not help you, since i dont know what is meant with p.
To fix the code there were some assumptions needed:
[t, xdot] = ode45(@(t,x)thetardynamics(t,x,u),tspan,x0,options);
--> I guess you want tspan and x0 to pass to ode, otherwise you would not have defined them
In:
function [xdot]= thetardynamics(~,x,u)
m1=10; m2=3; g=9.81; r=1;
xdot(1) = x(3);
xdot(2) = x(4);
xdot(3) = (-2*m2*x(3)*x(2)*x(4)-g*cos(x(1))*(m1*r+m2*x(2))+u(1))/(m1*r^2+m2*x(2)^2);
xdot(4) = x(3)^2*x(2)-g*sin(x(1))+((u(2)))/(m2);
xdot= xdot';
end
i replaced ue by u, because i did not find any ue in your code, but u is there.
Please check this, define p and i think your code is fixed. The obviously errors are not part of this list of fixes done.
However if you look at te results of ode45, you made one step into the right direction:
plot(t,xdot(:,1),t,xdot(:,2),t,xdot(:,3),t,xdot(:,4))
gives:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194344/image.png)
Best regards
Stephan
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!