Share data between two functions

2 ビュー (過去 30 日間)
Califfo
Califfo 2020 年 5 月 27 日
コメント済み: Califfo 2020 年 5 月 28 日
Hi,
I need to update and share a variable between two functions to solve a system of ODEs. These functions are, respectively, odefun - the function that ode45 accepts as input - and myOutputFcn - that is the custom output function that ode45 accepts as option.
function dsol_dt = odefun(t, sol, hyspar, L, M)
dsol_dt = zeros(2,1);
u = sol(1,1);
y = sol(2,1);
fri = hyspar.fri;
ud = y;
yd = - L(y,fri) / M ;
dsol_dt = [ud; yd];
end
function status = myOutputFcn(t,sol,flag,hyspar)
switch flag
case 'init'
case ''
hyspar.fri = RestoringForce(sol(1),sol(2),ecc,hyspar);
case 'done'
assignin('base','hyspar.fri',fri);
end
status = 0;
end
I would share and update the variable that is hyspar.fri (fri). When it updates in myOutputFcn through the function RestoringForce, I am only changing the local copy. So, in odefun will not be taken the updated value of the variable fri.
I read the main techniques to share variables between workspaces
but I didn't find any examples that suit me, I guess...
I would like to specify that I can't call RestoringForce in odefun for several different reasons.
Kind regards
Califfo

回答 (1 件)

darova
darova 2020 年 5 月 27 日
What abuo this?
function [hyspar1,status] = myOutputFcn(t,sol,flag,hyspar)
switch flag
case 'init'
case ''
hyspar.fri = RestoringForce(sol(1),sol(2),ecc,hyspar);
case 'done'
hyspar1 = hyspar;
hyspar1.fri = fri;
end
status = 0;
end
  1 件のコメント
Califfo
Califfo 2020 年 5 月 28 日
Thank you darova. However, I must update and share the variable after each integration step. Additionally, the variable hyspar.fri(fri) still does not update even if you add the output hyspar to the function myOutputFcn

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

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by