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_ex5(n,dom,kind)
function [x,solnum]=fred_eq_ex5(n,dom,kind)
% Example: y(x)=int_0^1{(min(x,t)-xt)y(t)}dt+(1-1/pi^2)sin(pi x), x in [0,1]
% From: K. E. Atkinson, L. F. Shampine,ACM Transactions on Mathematical 
%       Software (TOMS), 34(4), 2008 pp. 21:1--21.20 
% Exact solution solex=sin(pi x)
% call: [x,solnum]=fred_eq_ex5(128,[0,1],2);
tic;
Fspec=fred(n,dom,kind);
F=x2t(myfun(x),kind);
sol=(speye(n)-Fspec)\F;% solution in the spectral space
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=min(x,t)-x.*t;% K(x,t) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=myfun(x)
    f=(1-1/pi^2)*sin(pi*x);% f(x) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=sin(pi*x);       
subplot(2,1,1);semilogy(abs(sol),'.');
title('The absolute value of the Chebyshev coefficients');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