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.

fred_eq_test.m
```function [x,solnum]=fred_eq_test(n,dom,kind)
% Example: y(x)=int_a^b(K(x,t)y(t)dt+f(x), x in [0,1]
%          K(x,t)=t^2 x-1.5t x^2, f(x)=2ln(x+1)+5/9 x+3/4 x^2-4/3 x ln2
% From: M. GLSU, Y. ZTRK, Erciyes niversitesi Fen Bilimleri Enstits
%       Dergisi 26(3): 203-216 (2010)
% Exact solution solex=2ln(x+1)
% call: [x,solnum]=fred_eq_test(32,[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=t.^2.*x-3/2*t.*x.^2;% K(x,t) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=myfun(x)
f=2*log(x+1)+5/9*x+3/4*x.^2-4/3*log(2)*x;% f(x) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=2*log(x+1);
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```