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_ex1(n,dom,kind)
```function [x,solnum]=ibvp_ode_ex1(n,dom,kind)
% Example: sin(x)y'-cos(x)y=cos(2x), x in [0,pi/2]
%          y(0)-y(pi/2)=0
% Exact solution: solex=(pi-1)sin(x)-cos(x)-2x sin(x)
% call: [x,solnum]=ibvp_ode_ex1(32,[0 pi/2],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=-fact(x2t(cos(x),kind),16);% examples
A=P1*D+P0;b=x2t(cos(2*x),kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
T=cpv(n,dom,dom);% T(1,:) for xc=0, T(2,:) for xc=pi/2
A(n,:)=T(1,:)-T(2,:);b(n)=0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=(pi-1)*sin(x)-cos(x)-2*x.*sin(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)),')']);
display(['y(0)= ',num2str(solnum(1))]);
display(['y(pi/2)= ',num2str(solnum(end))]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

```