フィルターのクリア

How do I solve this index out of bounds error?

1 回表示 (過去 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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by