How to store conditional values during iteration by using Ode45?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!