Code covered by the BSD License

# Chebpack

### Damian Trif (view profile)

15 Jul 2011 (Updated )

The MATLAB package Chebpack solves specific problems for differential or integral equations.

[x,solnum]=fred_eq_ex4(n,dom,kind)
```function [x,solnum]=fred_eq_ex4(n,dom,kind)
% Example: y(x)=int_0^1{abs(x-t)y(t)}dt-(2x^3-9x+2)/6, 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=x
% call: [x,solnum]=fred_eq_ex4(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 column vector, t scalar
res=abs(x-t);% K(x,t) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=myfun(x)
f=-(2*x.^3-9*x+2)/6;% f(x) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=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```