How do I solve this index out of bounds error?

1 visualizzazione (ultimi 30 giorni)
Windell
Windell il 6 Ago 2017
Risposto: Jan il 6 Ago 2017
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!

Risposte (2)

Star Strider
Star Strider il 6 Ago 2017
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 Commento
Jan
Jan il 6 Ago 2017
Modificato: Jan il 6 Ago 2017
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.

Accedi per commentare.


Jan
Jan il 6 Ago 2017
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.

Community Treasure Hunt

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

Start Hunting!

Translated by