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 CenterFile Exchange에서 Directed Graphs에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by