Use global variables in ODE45

24 ビュー (過去 30 日間)
edoardo trabaldo
edoardo trabaldo 2015 年 10 月 6 日
コメント済み: edoardo trabaldo 2015 年 10 月 6 日
Hello everyone!
I need to solve a differential equation where one of the functions is defined by me. This function needs to know the previous value in input and output of the function itself to calculate the next point.
When I use ODE45 to solve the differential equation with my function in it the results are uncomprehensible at best, my question is: anyone knows if the declaration of global variable still works when using ODE45?
Thank you and best regards,
Edoardo.

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 6 日
Yes, global works in the functions called by ode45.
I would point out that your method cannot work if you call it from a solver that can move backwards in time (not unless you are working entirely backwards in time, of course.) It would also point out that use of the method is doubtful unless the time step is fixed and your function is called only once per time step. ode45 is a variable step solver. Especially when there are multiple elements in the y vector, ode45 may call the function multiple times per time step. Your function should not be dependent on the order in which it is called with different y at the same time step but if you define it as being dependent upon the immediately previous function call then it is implicitly dependent on the order of y values for a given time step.
  3 件のコメント
Walter Roberson
Walter Roberson 2015 年 10 月 6 日
When you say the function needs to know the previous value in input and output of the function, then which input is needed, and which output is needed?
My speculation is that you might be trying to do a calculation based upon delta t
edoardo trabaldo
edoardo trabaldo 2015 年 10 月 6 日
The code for the function is this:
function I=CPR_LY(Y)
global eps i0 y0 yshift;
temp=(Y-yshift)/eps*(1-((Y-yshift)/eps)^2);
if (sign(Y-y0)*(temp-i0))<0
yshift=yshift+sign(Y-y0);
end
I=(Y-yshift)/eps*(1-((Y-yshift)/eps)^2);
y0=Y;
i0=I;
I have the same curve (I(Y)) repeated periodically, with period 1, to know on which of this curves I am I use the value yshift. This indicates how many period I am from the curve in 0. As a control to know when to "jump" from a curve to the next(or previous) I need to know the derivative of my function. Here I use temp as temporary value to calculate the derivative with respect to i0, which is the previous point on the curve. Moreover I need y0 (previous value of Y in input) to check if I am increasing or decresing Y. Anyhow, the function depends strongly on the state in which the system is.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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