Clear Filters
Clear Filters

ode45 array size error

2 views (last 30 days)
Kevin Brinneman
Kevin Brinneman on 8 Nov 2018
Commented: Kevin Brinneman on 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 Comments
Stephan
Stephan on 8 Nov 2018
Edited: Stephan on 8 Nov 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 on 8 Nov 2018
Sure, here it is. It is a model predictive controller. unconstrained. Thanks for taking the time.

Sign in to comment.

Accepted Answer

Stephan
Stephan on 8 Nov 2018
Edited: Stephan on 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:
Best regards
Stephan
  1 Comment
Kevin Brinneman
Kevin Brinneman on 8 Nov 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!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!