How to store conditional values during iteration by using Ode45?

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Wan Ji
Wan Ji il 27 Ago 2021
Modificato: Wan Ji il 27 Ago 2021
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

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by