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_succapprox_test(n,dom,kind)
function [x,solnum]=ibvp_ode_succapprox_test(n,dom,kind)
% Example: xy''+ 2y' = -xy^5, x in [0,1]
%          y'(0)=0, y(1)=sqrt(3)/2,  
% From: MATLAB help, emdenbvp
% Exact solution: solex = (1+x^2/3)^(-1/2);
% call: [x,solnum]=ibvp_ode_succapprox_test(32,[0 1],2);
% 
tic;
A=zeros(n);b=zeros(n,1);y=zeros(n,1);
x=pd(n,dom,kind);X=mult(n,dom);D=deriv(n,dom);
myDElin;myINIT;cont=1;dy(cont)=1;
while dy(cont) > 1.e-14
    if cont>100, break, end; 
    cont=cont+1;myDEnonlin;
    sol=A\b;dy(cont)=norm(sol-y);y=sol;% the solution in spectral form
end
toc;
solnum = t2x(sol,kind);% the solution in physical form
myOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDElin
    % describes the linear part of the differential equation
    % describes the type of the initial/boundary conditions
    A=X*D^2+2*D;
    T=cpv(n,[0,1],dom);A(n-1,:)=T(1,:)*D;A(n,:)=T(2,:);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myINIT
    % describes the initial approximation in spectral form
    y(1)=1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDEnonlin
    % describes the nonlinear part of the differential equation
    % describes the boundary conditions
    yv=t2x(y,kind);work=-x.*yv.^5;b=x2t(work,kind);
    b(n-1)=0;b(n)=sqrt(3)/2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    % describes the output of the code
    solex=1./sqrt(1+x.^2/3);
    figure(1);
    subplot(2,1,1);semilogy(abs(sol),'.');grid;
    title('Absolute value of the coefficients of the solution');
    subplot(2,1,2);plot(x,solnum);grid;xlabel('x');ylabel('y(x)');
    title('The numerical solution');
    figure(2);
    subplot(2,1,1);semilogy(1:cont,dy,'.');grid;
    title('History of iterations - corrections');
    subplot(2,1,2);semilogy(x,abs(solnum-solex));grid;
    xlabel('x');ylabel('err');title('The error');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end


Contact us