Code covered by the BSD License  

Highlights from
Chebpack

image thumbnail

Chebpack

by

 

15 Jul 2011 (Updated )

The MATLAB package Chebpack solves specific problems for differential or integral equations.

[x,solnum]=ibvp_ode_newton_ex2(n,dom,kind,M)
function [x,solnum]=ibvp_ode_newton_ex2(n,dom,kind,M)
% Example: ep u" + 2(1-x^2)u + u^2 = 1, x in [-1,1], (Carrier equation)
%          u(-1) = 0, u(1) = 0, ep=0.01 
% From: A. Birkisson, www.maths.ox.ac.uk/chebfun/examples/
% call: [x,solnum]=ibvp_ode_newton_ex2(128,[-1 1],2,64);
%
tic;ep=0.01;
solnum=zeros(n,1);x=pd(n,dom,kind);X=mult(n,dom);D=deriv(n,dom);
yv=zeros(n,1);myINIT;y=x2t(yv,kind);% starting approximation in spectral space
dy=1;cont=1;
while dy(cont) > 1.e-13
    if cont>100, break, end; 
    cont=cont+1;myDElin;myDEnonlin;cor=A\b;% this is the correction
    dy(cont)=norm(cor);y=cor+y;yv=t2x(y,kind);
end;toc;solnum = yv;myOUT;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myINIT
    yv=2*(x.^2-1).*(1-2./(1+20*x.^2))*2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDElin
    dr=df(x,yv);DF=fact(x2t(dr,kind),M);A=ep*D^2+2*(speye(n)-X^2)-DF;
    T=cpv(n,[-1 1]',dom);A(n-1,:)=T(1,:);A(n,:)=T(2,:);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDEnonlin
    r=f(x,yv);b=(-ep*D^2-2*(speye(n)-X^2))*y+x2t(r,kind);b(n-1)=0;b(n)=0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function rez=f(x,yv)
    rez=1-yv.^2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function drez=df(x,yv)
    drez=-2*yv;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    figure(1);
    subplot(2,1,1);semilogy(abs(y),'.');grid;
    title('Absolute value of the coefficients of the solution');
    subplot(2,1,2);plot(x,solnum);grid;xlabel('x');ylabel('y(x)');
    title('The numerical solution');
    figure(2);
    semilogy(1:cont,dy,'.');grid;
    title('History of iterations - corrections');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us