Borrar filtros
Borrar filtros

ode45 array size error

2 visualizaciones (últimos 30 días)
Kevin Brinneman
Kevin Brinneman el 8 de Nov. de 2018
Comentada: Kevin Brinneman el 8 de Nov. de 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 comentarios
Stephan
Stephan el 8 de Nov. de 2018
Editada: Stephan el 8 de Nov. de 2018
We need to know more about tspan and how ue is passed to the function. Provide the whole code please - i suspect there is more than one issue.
Kevin Brinneman
Kevin Brinneman el 8 de Nov. de 2018
Sure, here it is. It is a model predictive controller. unconstrained. Thanks for taking the time.

Iniciar sesión para comentar.

Respuesta aceptada

Stephan
Stephan el 8 de Nov. de 2018
Editada: Stephan el 8 de Nov. de 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:
Best regards
Stephan
  1 comentario
Kevin Brinneman
Kevin Brinneman el 8 de Nov. de 2018
Yes!, everything is as you defined it. variable is p=2 but i changed it since it was a constant throughout the code. I will check it out and plot the results, many thanks for taking the time to review my code!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming 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