Passing a temporary parameter from time step to time step using ode113?

5 ビュー (過去 30 日間)
lu_po
lu_po 2018 年 7 月 21 日
コメント済み: lu_po 2018 年 7 月 22 日
Hello,
i'm using ode113 to solve some differential equations wich i stored in a function called RSDGL:
[t,F]=ode113(@RSDGL,[t0 tf],initial_conditions);
However in the function RSDGL i am using fzero to calculate zeros of functions wich appear in the differential equations as those zeros are time dependent. The problem i have is that i want to memorize those zeros temporarly to use them as starting points when i use fzero in the next time step (when ode113 calls RSDGL again). This should increase the performance as those zeros only vary little between each timestep. So i want to programm something like:
zero_new=fzero(function,zero_old);
inside my RSDGL function. I dont really have a clue how to store and overwrite those zeros and how to pass them each time ode113 calls my function RSDGL. I hope you understand my problem, as english is not my first language.

採用された回答

Jan
Jan 2018 年 7 月 22 日
編集済み: Jan 2018 年 7 月 22 日
This is a job for a persistent variable:
function dx = RSDGL(t, x)
persistent zero_old
if isempty(zero_old)
zero_old = 1; % Use a meaningful value!
end
zero_new = fzero(function, zero_old);
zero_old = zero_new;
...
end
It is smart to implement a method to reset this variable, e.g. for different runs of your code. Using clear RSDGL works, but I'd prefer an explicit method:
function dx = RSDGL(t, x)
persistent zero_old
if nargin == 1 % Reset!
zero_old = [];
return;
end
if isempty(zero_old)
zero_old = 1; % Use a meaningful value!
end
...
Then this resets the persistently store value:
RSDGL('reset')
This is clear and easy to understand during reading the code.
  1 件のコメント
lu_po
lu_po 2018 年 7 月 22 日
Thanks a lot! This worked 100% for me. My programm is now running 50% faster wich is crucial for the calculations i want to make.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by