Skip to content

Instantly share code, notes, and snippets.

@folsen
Created November 2, 2009 17:49
Show Gist options
  • Save folsen/224311 to your computer and use it in GitHub Desktop.
Save folsen/224311 to your computer and use it in GitHub Desktop.
function errVSstep(A, y0, t0, tf)
errors = [];
range = 1:20;
h = (tf-t0)./2.^range;
for i=range
[approx, err] = eulerint(A,y0,t0,tf,2^i);
errors = [errors, norm(err)];
end
loglog(h,errors);
end
function [approx, err] = eulerint(A, y0, t0, tf, N)
approx=0;
h = (tf-t0)/N;
y_t = y0;
for i=0:N
approx = approx + y_t;
y_t = eulerstep(A,y_t,h);
end
approx = approx/N;
exact = (expm(A*tf)-expm(A*t0))/A*y0;
err = exact-approx;
function unew = eulerstep(A, y_t, h)
unew = y_t+h*A*y_t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment