making a table of my results from AdamsBashforth function
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
**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 commentaires
Réponses (1)
Torsten
le 1 Mai 2022
Modifié(e) : Torsten
le 1 Mai 2022
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 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!