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.

ibvp_int_succapprox_test.m
function [x,solnum]=ibvp_int_succapprox_test(n,dom,kind)
% Example: y(x)=1+sin^2(x)-int_0^x{3sin(x-t)y^2(t)}dt, x in [0,1]
% From: Razzaghi, M., Ordokhani, Y., Math. Probl. Eng. 7, 205219 (2001)
% Exact solution: solex=cos(x)
% call: [x,solnum]=ibvp_int_succapprox_test(32,[0 1],2);
%
tic;
[Vspec,Vphys]=volt(n,dom,kind);
myINIT;dy=1;cont=1;
while dy(cont) > 1.e-12
    if cont>100, break, end; 
    cont=cont+1;
    F=myfun(x);yn=F-Vphys*(yo.^2);
    dy(cont)=norm(yn-yo);yo=yn;
end;
toc;solnum=yn;sol=x2t(solnum,kind);myOUT;
function [Vspec,Vphys]=volt(n,dom,kind)
% Volterra operator with kernel K(x,t)
x=pd(n,dom,kind);T=cpv(n,x,dom);[J,J0]=prim(n,dom);
V=T*J0/T;
[YY,XX]=meshgrid(x,x);KW=mykernel(XX,YY);
Vphys=V.*KW;Vspec=T\Vphys*T;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function res=mykernel(x,t)
    % x,t matrices
    res=3*sin(x-t);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=myfun(x)
    % calculates the function f(x) in physical space
    f=1+sin(x).^2;% values of f(x) 
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myINIT
    % describes the initial approximation in spectral form
    yo=zeros(n,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
    solex=cos(x);
    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);
    semilogy(1:cont,dy,'.');grid;title('History of iterations - corrections');
    figure(3);plot(x,abs(solnum-solex));grid; xlabel('x');ylabel('err');
    title('The error');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us