making a table of my results from AdamsBashforth function

3 ビュー (過去 30 日間)
Seungryul Lee
Seungryul Lee 2022 年 5 月 1 日
編集済み: Torsten 2022 年 5 月 1 日
**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

回答 (1 件)

Torsten
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 件のコメント
Seungryul Lee
Seungryul Lee 2022 年 5 月 1 日
Thanks.
Can you also figure out why I'm getting an error for
reqsteps = (te-t0)/h;
this line?
It says the matrix dimensions should agree to use in my for loop.
Do you know how should I fix it?
Torsten
Torsten 2022 年 5 月 1 日
編集済み: Torsten 2022 年 5 月 1 日
I adapted the code.
There was still an error in the Adams-Bashford update.
I adapted the code once more.

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

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by