How do I solve this index out of bounds error?

조회 수: 2 (최근 30일)
Windell
Windell 2017년 8월 6일
답변: Jan 2017년 8월 6일
Hello, I am attempting to run a controller simulator for a simple damped pendulum and I'm running into an index out of bounds error on line 14 of main.m. I've banged my head on this for a while and I'm thinking its something obvious. Could someone help me? Attached is the code. main.m runs everything. ODESolver solves the equations of motion, and the last file generates a trajectory. You will have to run the trajectory.m file first before running main.
Thanks so much in advance!

답변 (2개)

Star Strider
Star Strider 2017년 8월 6일
If I’m counting correctly, ‘Line 14’ is your ode45 call.
First, forget the global variables. They are likely a significant part of the problem. Pass those variables as extra parameters to your ODE function, if necessary.
Second, it’s difficult to understand your code, and I can’t figure out what ‘time_array’ is doing as a separate variable in your ‘ODEsolver’ function. I would do the ode45 call as:
[t_ode, x] = ode45(@ODESolver, time_array, x0, options);
This also does away with the need to do the later interp1 call, since your ‘ODEsolver’ function is evaluated very close to those time vector elements, and returned at those elements, so the subsequent interp1 call is not necessary.
There are ways to deal with time-varying variables in ODE functions, using interp1 and the returned time value to your ODE function. These are in the documentation on ODE with Time-Dependent Terms (link).
  댓글 수: 1
Jan
Jan 2017년 8월 6일
편집: Jan 2017년 8월 6일
Unfortunately the documentation on ODE with Time-Dependent Terms suggests to use interp1, which causes discontinuities in the function to be integrated, which conflicts with the specification of e.g. ODE45. See my answer.

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


Jan
Jan 2017년 8월 6일
See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047: Matlab's integrators with step size control cannot handle discontinuities in the function to be integrated. Therefore functions like rand, max, if, interp1, abs, ... are a serious problem here.
The only reliable solution from the view point of numerics is to stop the integration, when the function to be integrated changes a parameter non-smoothly by an event function.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by