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_ex2(n,dom,kind)
function [x,solnum]=fred_eq_ex2(n,dom,kind)
% Example: int_0^1/2{exp(x+t)y(t)}dt=1/2(e-1)exp(x),x in [0,1/2]
% From: A. Adawi and F. Awawdeh, Int. J. Contemp. Math. Sciences,
% Vol. 4(10), 2009, 485 - 496      
% Exact solution: solex=exp(x)
% call: [x,solnum]=fred_eq_ex2(32,[0,1/2],2);
%
tic;Fspec=fred(n,dom,kind);
yo=x2t(myfun(x),kind);sol=yo;% start successive iterations
for k=1:20
    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/2*(exp(1)-1)*exp(x);% f(x)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=exp(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