Rainer von Seggern
unread,Aug 30, 2010, 6:14:14 AM8/30/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to tumbi
Hello
I'm new in this group, and my first steps in learning Nsp are by
starting to solve boundary value problems for nonlinear ordinary
differential equations by spline collocation. Since it may be of
interest I give here a very first result.
Rainer
// Nonlinear boundary value problem:
// y''=2*y^3; y(0)=1; y'(1)=-1/4
// Solution: y(x)=1/(1+x)
// Numerical solution by spline collocation.
// The differential equation may be implicit of order three,
// and it should be simple to extend the method to systems.
nk=21;x=linspace(0,1,nk)';
n=100;xp=linspace(0,1,n)';
y0=ones(nk,1);
d0=splin(x,y0);
p0=[y0;d0];
function lhs = DEQ(x,y,y1,y2,y3)
lhs=y2-2*y.^3;
endfunction
function lhs = LBC(a,y,y1,y2)
lhs=log(y(1)); // lhs=y(1)-1;
endfunction
function lhs = RBC(b,y,y1,y2)
lhs=exp(y1($)+1/4)-1; // lhs=y1($)+1/4
endfunction
function z=res(p)
yy=p(1:nk,1);
d=p(nk+1:$,1);
[yp,yp1,yp2,yp3]=interp(xp,x,yy,d);
z=[DEQ(x,yp,yp1,yp2);LBC(0,yp);RBC(1,yp,yp1)];
endfunction
p1=fsolve_lsq(p0,res,n+2);
y1=p1(1:nk,1);d1=p1(nk+1:$,1);
[yp,yp1,yp2,yp3]=interp(xp,x,y1,d1);
function y=yex(x), y=1./(x+1); endfunction
function y=yex1(x), y=-1./((x+1).^2); endfunction
plot2d(xp,[yp-yex(xp),yp1-yex1(xp)],leg='yp-yex@yp1-yex1');