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_ex(n,dom,kind)
function [x,solnum]=ibvp_ode_ex(n,dom,kind)
% Example: ep*y''+ x*y'= -ep*pi^2*cos(pi*x)-pi*x*sin(pi*x), x in [-1,1]
%          y(-1)=-2, y(1)=0, ep=1.e-5
% From: Matlab demo for bvp4c
% Exact solution: solex=cos(pi*x)+erf(x/sqrt(2*ep))/erf(1/sqrt(2*ep))
% Remark: solves a bvp for the linear second order ode with shock
% call: [x,solnum]=ibvp_ode_ex(2048,[-1 1],2); 
%
tic;ep=1.e-5;
A=zeros(n);b=zeros(n,1);
x=pd(n,dom,kind);X=mult(n,dom);D=deriv(n,dom);
myDE;myBC;sol=A\b;% the solution in spectral form
toc;
solnum = t2x(sol,kind);% the solution in physical form
myOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDE
    f=-ep*pi^2*cos(pi*x)-pi*x.*sin(pi*x);
    A=ep*D^2+X*D;b=x2t(f,kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    T=cpv(n,dom,dom);% T(1,:) for xc=-1, T(2,:) for xc=1
    A(n-1,:)=T(1,:);b(n-1)=-2;
    A(n,:)=T(2,:);b(n)=0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    solex=cos(pi*x)+erf(x/sqrt(2*ep))/erf(1/sqrt(2*ep));
    figure(1)
    subplot(2,1,1);semilogy(abs(sol),'.');grid;
    title('The absolute value of the coefficients of the solution');
    subplot(2,1,2);plot(x,solnum);grid;xlabel('x');ylabel('y(x)');
    axis([-1 1 -2.1 2.1]);
    title(['The solution (err = ',num2str(norm(solnum-solex)),')']);
    figure(2);semilogy(x,abs(solnum-solex));title('The errors');grid;
    xlabel('x');ylabel('err');
    figure(3);spy(A);title('spy(A)');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us