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_ex3(n,dom,kind,y0)
function [x,solnum]=ibvp_sys_ex3(n,dom,kind,y0)
% Example: y1'-y2=0;y2'-y3=0;x^2 y3'-2x y1+(x^2-3)y2-xy3=0, x in [0,2]
%          y1(2)=0, y2(2)=4, y3(2)=2
% Exact solution: y1ex=x^2-4
% Remark: the general solution is C1J0(x)+C2Y0(x)+C3(x^2-4)
%         the conditions y1(0)=-4,y2(0)=0,y3(0)=2 give
%         y(x)=4(C3-1)J0(x)+C3(x^2-4) 
% call: [x,solnum]=ibvp_sys_ex3(128,[0,2],2,[0;4;2]);
%
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
    b=zeros(m*n,1);
    A=[D,-speye(n),zeros(n);zeros(n),D,-speye(n);-2*X,X^2-3*speye(n),X^2*D-X];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    T=cpv(n,dom(2),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
    y1ex=x.^2-4;err=norm(solnum(:,1)-y1ex);
    subplot(2,1,1);semilogy(abs(sol),'.');grid;
    title('Absolute value of the coefficients of y_1, y_2, y_3');
    subplot(2,1,2);plot(x,solnum(:,1));grid;xlabel('x');ylabel('y_1(x)');
    title(['Numerical solution (error =',num2str(err),')']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us