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_int_ex(n,dom,kind)
function [x,solnum]=ibvp_ode_int_ex(n,dom,kind)
% Example: ep*y''+ x*y'= -ep*pi^2*cos(pi*x)-pi*x*sin(pi*x),
%          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));
% call: [x,solnum]=ibvp_ode_int_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);J=prim(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*speye(n)+J*X-J^2;b=J^2*x2t(f,kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    T=cpv(n,dom,dom);% T(1,:) for xc=-1, T(2,:) for xc=1
    A(1,:)=T(1,:);b(1)=-2;
    A(2,:)=T(2,:);b(2)=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 values 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