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_test(n,dom,kind)
function [x,solnum]=ibvp_ode_test(n,dom,kind)
% Example: y'''- x y = (x^3-2*x^2-5*x-3)*exp(x), x in [0,1]
%          y(0)=0, y'(0)=1, y(1)=0
% Exact solution: solex=x*(1-x)*exp(x)
% call: [x,solnum]=ibvp_ode_test(32,[0 1],2);
tic;
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
    % describes the differential equation
    A=D^3-X;b=x2t((x.^3-2*x.^2-5*x-3).*exp(x),kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    % describes the initial/boundary conditions
    T=cpv(n,dom,dom);% values of Chebyshev polynomials at 0 and 1
    A(n-2,:)=T(1,:);b(n-2)=0;% y(0)=0
    A(n-1,:)=T(1,:)*D;b(n-1)=1;% y'(0)=1
    A(n,:)=T(2,:);b(n)=0;% y(1)=0
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
function myOUT
    % describes the output of the code
    solex=x.*(1-x).*exp(x);
    subplot(2,1,1);stem(sol,'.');grid;title('The coefficients of the solution');
    err=norm(solnum-solex);
    subplot(2,1,2);plot(x,solnum,'b');grid;xlabel('x');ylabel('y(x)');
    title(['The solution (error = ',num2str(err),')']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us