making a table of my results from AdamsBashforth function
3 ビュー (過去 30 日間)
古いコメントを表示
**How do I make a table of results that includes h and the error: Eh = max|y(t) − y ̃(t)|, where y ̃ is my approximated solution? I'm using Adams Bashforth function with startingtimet =0, endingtimet =20, and stepsizes: h= 1/2^n for n=1, . . . , 10.
f=@(t,y) -y-3*t;
F=@(t) 3-3*t-2*exp(-t);
t0=0; y0=1;
te=20;
nn=1:10;
for ii=1:length(nn)
n=nn(ii);
h=1./(2^n);
[ys, ts] = MyAdamsBashforth2(f, t0, te, y0, h);
%what should I put here
end
********************************************
this is my function code
function [ys, ts] = MyAdamsBashforth2(f, t0, te, y0, h)
ts = (t0:h:te).';
ys(1,1) = y0;
reqsteps = (te-t0)/h;
ys(2,1) = ys(1,1) + h*f(ts(1,1),ys(1,1));
for n = 3:reqsteps+1
ys(n,1) = ys(m-1,1) + h*(3/2)*f(ts(n-1,1),ys(n-1,1)-(1/2)*f(ts(n-1,2),ys(n-2,1)));
end
end
0 件のコメント
回答 (1 件)
Torsten
2022 年 5 月 1 日
編集済み: Torsten
2022 年 5 月 1 日
f=@(t,y) -y-3*t;
F=@(t) 3-3*t-2*exp(-t);
t0=0; y0=1;
te=20;
nn=1:10;
h = 1./2.^nn;
for ii=1:length(nn)
[ys, ts] = MyAdamsBashforth2(f, t0, te, y0, h(ii));
Eh(ii) = max(abs(F(ts)-ys));
end
plot(nn,Eh)
T = table(h,Eh);
function [ys, ts] = MyAdamsBashforth2(f, t0, te, y0, h)
ts = (t0:h:te).';
ys(1,1) = y0;
reqsteps = (te-t0)/h;
ys(2,1) = ys(1,1) + h*f(ts(1,1),ys(1,1));
for n = 3:reqsteps+1
ys(n,1) = ys(n-1,1) + h*((3/2)*f(ts(n-1,1),ys(n-1,1))-(1/2)*f(ts(n-2,1),ys(n-2,1)));
end
end
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Directed Graphs についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!