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]=ibvp_ode_ex2(n,dom,kind)
```function [x,solnum]=ibvp_ode_ex2(n,dom,kind)
% Example: y''+sin(x)y'+exp(x)y=-16pi^2 sin(4 pi x)+4 pi sin(x)cos(4 pi x)+
%              +exp(x)(2+sin(4 pi x)), x in [-1,1]
%          y(-1)+ y'(-1)=2+4 pi, y(1)+ y'(1)=2+4 pi
% Exact solution: solex=2+sin(4 pi x)
% call: [x,solnum]=ibvp_ode_ex2(64,[-1 1],2);
%
tic;
A=zeros(n);b=zeros(n,1);
x=pd(n,dom,kind);X=mult(n,dom);D=deriv(n,dom);
myDE;myBC;sol=A\b;% the solution in spectral form
toc;
solnum = t2x(sol,kind);% the solution in physical form
myOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDE
P1=funm(full(X),@sin);P0=funm(full(X),@exp);
f=-16*pi^2*sin(4*pi*x)+4*pi*sin(x).*cos(4*pi*x)+exp(x).*(2+sin(4*pi*x));
A=D^2+P1*D+P0;b=x2t(f,kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
T=cpv(n,dom,dom);% T(1,:) for xc=-1, T(2,:) for xc=1
A(n-1,:)=T(1,:)+T(1,:)*D;b(n-1)=2+4*pi;
A(n,:)=T(2,:)+T(2,:)*D;b(n)=2+4*pi;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=2+sin(4*pi*x);
subplot(2,1,1);semilogy(abs(sol),'.');grid;
title('The absolute value of the coefficients of the solution');
subplot(2,1,2);plot(x,solnum);grid;xlabel('x');ylabel('y(x)');
title(['The solution (err = ',num2str(norm(solnum-solex)),')']);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end```