How to store conditional values during iteration by using Ode45?

1 回表示 (過去 30 日間)
Cola
Cola 2021 年 8 月 27 日
コメント済み: Wan Ji 2021 年 8 月 27 日
I want to store data of each iteration, including the conditional values. Is there a way to do this? Thanks!
There is an example where 'b' is the conditional value. And the goal ([b0,t,X1]) is as follows:
1 0 1.0000
1 1.0000 1.5378
1 2.0000 1.6115
1 3.0000 1.6175
1 4.0000 1.6180
1 5.0000 1.6180
2 0 1.0000
2 1.0000 1.9317
2 2.0000 1.9987
2 3.0000 2.0000
2 4.0000 2.0000
2 5.0000 2.0000
3 0 1.0000
3 1.0000 2.2584
3 2.0000 2.3036
3 3.0000 2.3028
3 4.0000 2.3028
3 5.0000 2.3028
Code:
save=[]
t0=0;
tf=15;
a0=1
global b0;
for b0=1:1:3
X=[a0]
[t, X1] = ode45(@fun,[t0:0.1:tf], X);
% X2=[b0,t,X1(1)];
X2=[t,X1];
save=[save;X2]
end
function fx=fun(t,x)
fx=zeros(1,1);
global b0
b=b0
fx(1)=x(1)-x(1)^2+b
end

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 27 日
編集済み: Wan Ji 2021 年 8 月 27 日
You can define a table to store the data including the conditional values
function main
t0=0;
tf=15;
a0=1;
T = table();
global b0;
for b0=1:1:3
X=[a0];
[time, xval] = ode45(@fun,[t0:0.1:tf], X);
b = b0*ones(size(time));
T = [T; table(b, time, xval)]; % use table T to store the data
end
writetable(T,'Table.txt')
end
function fx=fun(t,x)
fx=zeros(1,1);
global b0
b=b0;
fx(1)=x(1)-x(1)^2+b;
end
The final file Table.txt is what you need
  2 件のコメント
Cola
Cola 2021 年 8 月 27 日
編集済み: Cola 2021 年 8 月 27 日
Sir, you are like god for me. Thank you very much.
Wan Ji
Wan Ji 2021 年 8 月 27 日
My pleasure

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

その他の回答 (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