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_sys_ex2(n,dom,kind,y0)
function [x,solnum]=ibvp_sys_ex2(n,dom,kind,y0)
% Example: y1'-y2=0;y2'+(4x^2+alpha^2-2)y1-4x y2=alpha^2exp(x^2), x in [0,1.5]
%          y1(0)=1, y2(0)=beta, alpha=beta=20
% Exact solution: y1ex=[1+beta/alpha sin(alpha x)]exp(x^2),
% call: [x,solnum]=ibvp_sys_ex2(128,[0,1.5],2,[1,20]);
%
tic;m=length(y0);A=spalloc(m*n,m*n,5*m*n);b=zeros(m*n,1);
x=pd(n,dom,kind);X=mult(n,dom);D=deriv(n,dom);
myDE;myBC;sol=reshape(A\b,n,m);% the solution in spectral space
solnum = t2x(sol,kind);% the solution in physical space
toc;myOUT;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDE
    A=[D -speye(n);4*X^2+398*speye(n) D-4*X];
    r=[zeros(n,1),400*exp(x.^2)];b=reshape(x2t(r,kind),m*n,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    T=cpv(n,dom(1),dom);
    for k=1:m
        A(k*n,:)=0;A(k*n,(k-1)*n+1:k*n)=T;b(k*n)=y0(k);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    xx=linspace(dom(1),dom(2),10000);fx = barycheb(xx,solnum(:,1),x,kind);
    y1ex=(1+sin(20*x)).*exp(x.^2);
    err=norm(solnum(:,1)-y1ex);
    subplot(2,1,1);semilogy(abs(sol),'.');
    title('Absolute value of the coefficients of y_1 and y_2');grid;
    subplot(2,1,2);plot(xx,fx);grid;xlabel('x');ylabel('y_1(x)');
    title(['Numerical solution (error = ',num2str(err),')']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us