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.

[xx,solnum]=ibvp_ode_split_test(n,dom,kind)
```function [xx,solnum]=ibvp_ode_split_test(n,dom,kind)
% Example: (ep+x^2)y''+ 4xy'+2y=0, x in [-1,1], ep=1.e-3
%          y(-1)=y(1)=1/(1+ep),
% Exact solution: solex=1/(ep+x^2)
% call: [xx,solnum]=ibvp_ode_split_test(96,[-1 -0.06 0.06 1],2);
%
tic;
m=length(dom);ep=1.e-3;
A=spalloc(n*(m-1),n*(m-1),100*n*(m-1));b=zeros(n*(m-1),1);xx=[];
xs=pd(n,[-1 1],kind);Js=prim(n,[-1,1]);Xs=mult(n,[-1 1]);Ds=deriv(n,[-1,1]);
for k=1:m-1
pdom=[dom(k),dom(k+1)];l=(pdom(2)-pdom(1))/2;med=(pdom(2)+pdom(1))/2;
x=l*xs+med;J=l*Js;X=l*Xs+med*speye(n);xx=[xx,x];
myDE;
E=zeros(m-1);E(k,k)=1;AA(1:2,:)=0;A=A+kron(E,AA);
EE=zeros(m-1,1);EE(k)=1;bb(1:2)=0;b=b+kron(EE,bb);
end;
myBC; sol=A\b;% the solution in spectral form
toc;solnum=[];
for i=1:m-1
solnum = [solnum,t2x(sol((i-1)*n+1:i*n),kind)];
end
% the solution in physical form
x=reshape(xx,(m-1)*n,1);solnumv=reshape(solnum,(m-1)*n,1);
myOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDE
% describes the differential equation
f=zeros(n,1);
AA=ep*speye(n)+X^2;bb=J^2*x2t(f,kind);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
% describes the initial/boundary conditions
T=cpv(n,[dom(1),dom(m)],[dom(1),dom(m)]);% T(1,:) for xc=d1, T(2,:) for xc=dm
Tl=T(1,:);Tr=T(2,:);
A(1,1:n)=T(1,:);b(1)=1/(1+ep);% y(-1)
A(2,(m-2)*n+1:(m-1)*n)=T(2,:);b(2)=1/(1+ep);% y(1)
for j=2:m-1
fl=(dom(j)-dom(j-1))/2;fr=(dom(j+1)-dom(j))/2;
A((j-1)*n+1,(j-2)*n+1:(j-1)*n)=Tr;
A((j-1)*n+1,(j-1)*n+1:j*n)=-Tl;b((j-1)*n+1)=0;% y_(j-1)(dj)=y_j(dj)
A((j-1)*n+2,(j-2)*n+1:(j-1)*n)=Tr*Ds/fl;
A((j-1)*n+2,(j-1)*n+1:j*n)=-Tl*Ds/fr;b((j-1)*n+2)=0;% y'_(j-1)(dj)=y'_j(dj)
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
% describes the output of the code
solex=1./(ep+x.^2);
figure(1);
subplot(2,1,1);semilogy(x,abs(sol),'.');grid;
title('The absolute value of the coefficients of the solution');
subplot(2,1,2);plot(x,solnumv,dom,zeros(length(dom),1),'.r');grid;
title('The solution');xlabel('x');ylabel('y(x)');
figure(2);
semilogy(x,abs(solnumv-solex));title('The error');xlabel('x');ylabel('err');grid;
figure(3);spy(A);title('spy(A)');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

```