concatenate multiple 'OutputFcn' options in odeset?

5 ビュー (過去 30 日間)
feynman feynman
feynman feynman 2024 年 2 月 29 日
コメント済み: feynman feynman 2024 年 3 月 1 日
Is there a way to output both odeplot+odephas2 after modifying the following?
options=odeset('OutputFcn',@odeplot,'OutputFcn',@odephas2);
[t,u]=ode23(@ode,[0;t],[u0;v0],options);

採用された回答

Stephen23
Stephen23 2024 年 2 月 29 日
編集済み: Stephen23 2024 年 3 月 1 日
Define a wrapper function to call whatever functions you want.
Note that the wrapper function itself will need to fulfill the requirements given under "Output Function" here:
Something like this:
function myfun
f1 = figure();
f2 = figure();
options = odeset('OutputFcn',@mywrapper);
[T,u] = ode23(@ode,[0;1],[0;1],options)
%%
function dudt = ode(t,u)
dudt=[u(2);-u(1)];
end
function status = mywrapper(varargin)
figure(f1);
s1 = odeplot(varargin{:});
figure(f2);
s2 = odephas2(varargin{:});
status = or(s1,s2);
end
end
Hopefully ODEPLOT et al are robustly written such that they can be called at the same time, i.e. have no assumptions on what the current figure/axes are. You will have to check that yourself.
EDIT: both of those plot functions call GCF, so I added the FIGURE() calls and tested it.
  7 件のコメント
Stephen23
Stephen23 2024 年 3 月 1 日
編集済み: Stephen23 2024 年 3 月 1 日
Unfortunately both of those functions use GCF, which is the cause of that error. This is why I advised you to check for any assumptions they make about the current figure/axes.
The solution is to explicitly define the two figures ourselves, before calling the plotting functions, e.g.:
function myfun
f1 = figure();
f2 = figure();
options = odeset('OutputFcn',@mywrapper);
[T,u] = ode23(@ode,[0;1],[0;1],options)
%%
function dudt = ode(t,u)
dudt=[u(2);-u(1)];
end
function status = mywrapper(varargin)
figure(f1);
s1 = odeplot(varargin{:});
figure(f2);
s2 = odephas2(varargin{:});
status = or(s1,s2);
end
end
I modified my answer to include this change.
feynman feynman
feynman feynman 2024 年 3 月 1 日
thank you so much

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStochastic Differential Equation (SDE) Models についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by