필터 지우기
필터 지우기

ode45 array size error

조회 수: 2 (최근 30일)
Kevin Brinneman
Kevin Brinneman 2018년 11월 8일
댓글: Kevin Brinneman 2018년 11월 8일
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
Stephan
Stephan 2018년 11월 8일
편집: Stephan 2018년 11월 8일
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 2018년 11월 8일
Sure, here it is. It is a model predictive controller. unconstrained. Thanks for taking the time.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephan
Stephan 2018년 11월 8일
편집: Stephan 2018년 11월 8일
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
Kevin Brinneman
Kevin Brinneman 2018년 11월 8일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by