Code covered by the BSD License  

Highlights from
Program to Solve initial value problems by various methods

image thumbnail
from Program to Solve initial value problems by various methods by Daniel Klawitter
initial value problem solver enter >>IVPsolve to start

Error(ue,xp,uh)
function [L2err,H1err,LI0err] = Error(ue,xp,uh)
% function that calculates three different errors
% the discrete L2error, the discrete H1error and the L_inf error
%
% input:   o ue.......the exact solution as function_handle
%          o xp.......vector containing the gridpoints
%          o uh.......vector containing the numerical solution
%
% output:  o L2err....error calculated in discrete L2 norm
%          o H1err....error calculated in discrete H1 norm
%          o LI0err...error calculated in L_inf norm
%
% notes:   o for big step sizes the approximated derivation could not fit
%            to the exact one and the H1err could get to big
%          o might be buggy, please mail if you have an improvement...
%
% authors: o Christian Jkel (University of Technology Dresden)
%          o Daniel Klawitter (University of Technology Dresden)


% for H1err we need the derivation of the exact solution
uprime = str2func(['@(x)',char(diff(sym(ue)))]);

% set the errors to zero, for summation
L2err = 0;
H1err = 0;


h = diff(xp);
duh = diff(uh)./h;
for j = 1:length(uh)
    %         L2err = L2err + sqrt(h(j))*(ue(xp(j))-uh(j))^2;
    L2err = L2err + (ue(xp(j))-uh(j))^2;
end
for j = 1:length(duh)
    %L2err = L2err + (ue(xp(j))-uh(j))^2;
    H1err = H1err + ((uprime(xp(j)))-duh(j))^2;
end

H1err = sqrt(mean(h))*sqrt(H1err + L2err);
L2err = sqrt(mean(h))*sqrt(L2err);

    



for k = 1:length(xp)
    uex(k) = ue(xp(k));
end

LI0err = max(abs(uex-uh));

Contact us at files@mathworks.com