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]=fred_eq_ex3(n,dom,kind)
function [x,solnum]=fred_eq_ex3(n,dom,kind)
% Example: int_0^1{exp(x*t)y(t)dt=exp(x)/x-exp(x)/x^2+1/x^2, x in [0,1]
% From: A. H. Borzabadi, O. S. Fard, World Academy of Science, Engineering 
%       and Technology 33 (2007), pp. 70-73
% Exact solution: solex=x
% call [x,solnum]=fred_eq_ex3(16,[0,1],1);
% 
tic;
Fspec=fred(n,dom,kind);
yo=x2t(myfun(x),kind);sol=yo;% start successive iterations
for k=1:500
    yn=(eye(n)-Fspec)*yo;sol=sol+yn;yo=yn;norm(yo)
end
solnum=t2x(sol,kind);% solution in the physical space
toc;myOUT
function [Fspec,Fphys]=fred(n,dom,kind)
% Fredholm operator with kernel K(x,t)
[x,w]=pd(n,dom,kind);T=cpv(n,x,dom);
[YY,XX]=meshgrid(x,x);KW=mykernel(XX,YY);
Fphys=KW*diag(w);Fspec=T\Fphys*T;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function res=mykernel(x,t)
    % x,t matrices
    res=exp(x.*t);% K(x,t) 
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=myfun(x)
    f=(1+exp(x).*(x-1))./(x.^2);% f(x)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=x;subplot(2,1,1);semilogy(abs(sol),'.');
title('The absolute value of the coefficients of the solution');grid;
subplot(2,1,2);plot(x,solnum);grid;xlabel('x');ylabel('y(x)');
title(['The numerical solution (error = ',num2str(norm(solnum-solex)),')']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us