How do I solve this index out of bounds error?

1 次查看(过去 30 天)
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