Borrar filtros
Borrar filtros

How do I solve this index out of bounds error?

1 visualización (últimos 30 días)
Windell
Windell el 6 de Ag. de 2017
Respondida: Jan el 6 de Ag. de 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!

Respuestas (2)

Star Strider
Star Strider el 6 de Ag. de 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 comentario
Jan
Jan el 6 de Ag. de 2017
Editada: Jan el 6 de Ag. de 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.

Iniciar sesión para comentar.


Jan
Jan el 6 de Ag. de 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.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by