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.

[xg,solnumg]=ibvp_sys_newton_ex4(n,dom,kind,M)
function [xg,solnumg]=ibvp_sys_newton_ex4(n,dom,kind,M)
% Example: y1'+y1=1-10^3 y1y2, y2'=-10^(-3) y1y2; x in [0,2000000]
%          y1(0)=0, y2(0)=1,
% From:MEDEEA, F. Aluffi-Pentini, V. De Fonzo, V. Parisi
%      Journal of Mathematical Chemistry, Vol. 33, No. 1, 2003
% Test: lim y1=1,lim y2=0
% Remark: uses Newton method with adaptive step
% call: [x,solnum]=ibvp_sys_newton_ex4(64,[0 2000000],2,16);
%
tic;xg=[];solnumg=[];solg=[];m=length(dom)-1;dyg=[];contg=0;step=1;
domloc=[dom(1),min(dom(1)+step,dom(2))];
myINIT;y0=yv(1,:);y=x2t(yv,kind);% starting approximation in physical space
while domloc(2)<dom(2)
x=pd(n,domloc,kind);X=mult(n,domloc);J=prim(n,domloc);
yv=[ones(n,1)*y0(1),ones(n,1)*y0(2)];y=x2t(yv,kind);
dy=1;cont=1;
while dy(cont) > 1.e-12,display(domloc(1));%display independent variable x
if cont>30, break, end;
cont=cont+1;myDElin;myDEnonlin;myIC;cor=A\b;% this is the correction
dy(cont)=norm(cor);
if dy(cont)>100, break,end,
y=reshape(cor,n,2)+y;yv=t2x(y,kind);
end
if cont>20 || norm(y(end-2:end,:))>1.e-12 || dy(cont)>100, step=step/2;
domloc=[domloc(1),domloc(1)+step];
else  step=step*11/10;
solnum = yv;
solnumg=[solnumg;solnum];solg=[solg;y];
xg=[xg;x];dyg=[dyg,dy];contg=contg+cont;
y0=solnum(end,:);domloc=[domloc(2),min(domloc(2)+step,dom(2))];
end;
end;toc;myOUT;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myINIT
yv=[ones(n,1)*0,ones(n,1)*1];% initial approximation
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDElin
dr=df(x,yv);
DF=[fact(x2t(dr(1:n,1),kind),M),fact(x2t(dr(1:n,2),kind),M);...
fact(x2t(dr(n+1:2*n,1),kind),M),fact(x2t(dr(n+1:2*n,2),kind),M)];
A=[speye(n) zeros(n);zeros(n) speye(n)]+[J,zeros(n);zeros(n),J]*...
([speye(n) zeros(n);zeros(n),zeros(n)]-DF);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDEnonlin
% must be written by the user
r=f(x,yv);F=x2t(r,kind);
b=-(reshape(y,2*n,1)+[J,zeros(n);zeros(n),J]*...
([speye(n) zeros(n);zeros(n),zeros(n)]*reshape(y,2*n,1)-...
reshape(F,2*n,1)));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myIC
T=cpv(n,dom(1),dom);A(1,:)=0;A(1,1:n)=T;A(n+1,:)=0;A(n+1,n+1:2*n)=T;
b(1)=0;b(n+1)=0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function rez=f(x,yv)
rez=[1-10^3*yv(:,1).*yv(:,2),-1/10^3*yv(:,1).*yv(:,2)];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function drez=df(x,yv)
drez=[-10^3*yv(:,2),-10^3*yv(:,1);-1/10^3*yv(:,2),-1/10^3*yv(:,1).^1];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
figure(1);
subplot(2,1,1);semilogy(abs(solg),'.');
title('Absolute value of the coefficients of y_1, y_2');grid;
subplot(2,1,2);semilogy(1:contg,dyg,'.');grid;
title('History of iterations - corrections');
figure(2);
loglog(xg,solnumg(:,1),'b',xg,max(abs(solnumg(:,2)),0),'-r');
axis([1.e-8 dom(2) 1.e-8 5]);grid;xlabel('x');ylabel('y1,2(x)');
title('Numerical solution');legend('y1(x)','y2(x)');
display(solnumg(end,:));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end