making a table of my results from AdamsBashforth function

1 次查看(过去 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

回答(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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by