ND 2D24 Numerical Methods II / ndré Jaun, ND, KTH 2. INITIL- / OUNDRY VLUE PROLEMS 2.. Remember: what we saw during the last lesson Euler and Runge-Kutta methods for solving ODEs Predator-prey model using a system of ODEs nharmonic pendulum writing 2 a order ODE as a system with two ODEs nd 2..2 Overview: what you will learn today oundary value problem Solution by shooting and finite differences 5 theory questions for the exam
2.2 Initial- / oundary value problems (NM 8.7, H.) Take Newton s second law of motion for example! ψ! ψ x v v, x = f(t; x; x ) = This second order problem is specified with 2 additional constraints. Initial value problems satisfy 2 initial conditions (I) at the beginning of the interval m F x(t ) = x ; v(t ) = v Knowing the initial position and velocity of a bullet, where is it going to land? oundary value problems satisfy 2 boundary conditions () at the boundaries = x a ; x(b) = x b x(a) What is the trajectory of a bullet shot a from to b? oundary conditions can be specified as the x(a) value (Dirichlet), the derivative (a) (Neuman) or a linear combination of the two x(a) + ffx (a) = fi (Robin). x
t=[ ]; v=2.5; options=odeset('events','on','outputsel',[2],'refine',5) >> a=pi/6; X=[. v*cos(a) v*sin(a)]; [T,Y]=ode23(shoot,t,X,options); >> a=pi/3; X=[. v*cos(a) v*sin(a)]; [T2,Y2]=ode23(shoot,t,X,options); >> x=:.:.5; plot(x,exp(x.^2), Y(:,),Y(:,2),'--',Y2(:,),Y2(:,2),':') >> 2.3 Shooting method for boundary value problems (NM 8.7., H.3) The method solves initial value problems, adjusting the I to approach the. Shoot from a with a guess velocity; if the bullet falls short, increase the velocity, etc Example determine the canon angle to shoot from the hill (; ) to the target (; =e). varargout = shoot(t,x,flag); function.4 Shooting from the top of a Gaussian hill flag switch '', case.2 varargout } = dydt(t,x); 'events', [varargout :3}]= events(t,x); case error(['unknown flag.']); otherwise, end f=dydt(t,x) function X(4); ; -9.8]; f=[x(3); f=(vx,vy,ax,ay) [value,isterminal,direction]=events(t,x) function vertical coordinate x(2).8.6.4.2 shapemountain=exp(-x()^2); ground elevation = X(2)-shapeMountain; value = ; isterminal present height stop if ground.5.5 horizontal coordinate x() direction = -; going down Here events(t,x) is used to stop the integration when the bullet hits the ground; experimentally, the canon angle is found to be somewhere ff 2 [ß=6; ß=3].
b=pi/3; tol=pi/8; t=[ ]; v=2.5; xtarget=; a=pi/6; options=odeset('events','on','outputsel',[2],'refine',5). v*cos(a) v*sin(a)];[t,y]=ode23(shoot,t,x,options);fa=y(end,)-xtarget; X=[. v*cos(b) v*sin(b)];[t,y]=ode23(shoot,t,x,options);fb=y(end,)-xtarget; X=[ abs(b-a)>tol while x=b-fb*(b-a)/(fb-fa); The solution is computed by solving a non-linear equation f(ff) = x end (ff) x b = : the secant method is most effective when f (ff) is not known explicitly. In Matlab use the same driver shoot() to search with the secant method: fa=fb; b=x; fb=fx; a=b; x b] sol=8/pi*[a.4 Shooting from the top of a Gaussian hill X=[. v*cos(x) v*sin(x)];[t,y]=ode23(shoot,t,x,options);fx=y(end,)-xtarget; end.2 x b a --------------------------- 4.748 4.748 6. 46.476 46.476 4.748 48.2975 48.2975 46.476 47.9854 47.9854 48.2975 vertical coordinate x(2).8.6.4.2 ==> alpha = 48. +/-.3 degree.5.5 horizontal coordinate x() Linear boundary value problems can be solved with a system using only two trials.
i+ 2y + i i y p y i+ y y i i x a 2 + h ha p xi ) ( e i d i y i + ( + p xi ) z } i f b i 2.4 Finite differences for boundary value problems (NM 8.7.2, H.4) The method divides the interval where the solution is sought into equidistant pieces using finite difference (FD) to approximate derivatives with unknowns y i = y(x i ) x i = x a + i h; h = (x b x a )=N i ß y i+ y i y ; y i ß y i+ 2y i + y i h 2 2h here for example with a O(h precision ). Linear example: solve the boundary value problem in the interval 2 2 [; 4] x y + a p xy (6 y)x = ; y() = ;y(4) = for parameter values a 2 f 3; 2; ; ; ; 2g. Discretize using a uniform mesh x i = x a + i h, x a =, x b = 4, h = 4=N where N is the number of intervals. Insert the FD approximation and evaluate for every mesh point x i inside 2h This yields a tridiagonal linear y (E + D + F)y = b system (6 y i )x i = 8i = 2 :::;N i (2 h 2 x i ) z } y i+ = 6h 2 x i z } y ha 8i 2 z } 2
y 2 y n y n y y 2 y n y n y y(a) 2 b oundary conditions are usually imposed by replacing the corresponding equation ::: 2 d 2 f 2 ::: e e 3 d 3 f 3 :::........ y 3. = b 3. ::: e n d n f n ::: n b y(b) and substituting the columns to keep the matrix symmetric if it was in the beginning ::: d 2 f 2 ::: y 3. e 3 d 3 f 3 :::........ = y(a) 2 e 2 y(a) b b 3. ::: e n d n ::: n f n y(b) b y(b) The system is can be solved directly using tridia or solvers for sparse systems.
plot(x,y), subplot(3,2,k); ylabel(l) l=sprintf('a=i',a), In Matlab, this yields X=[; 4]; Y=[; -]; k=:6 for a=k-4; n=; h=diff(x)/(n-); x=linspace(x(),x(2),n)'; e=-.5*h*a*sqrt(x); boundaries parameter a n- intervals assemble tridiagonal d=-(2-h^2*x); matrix f=+.5*h*a*sqrt(x); d [; f(:n-)]],-:,n,n); =spdiags([[e(2:n);] assemble RHS b=6*h^2*x; (,2)=; b()=y(); (,)=.; (n,n-)=; b(n)=y(2); (n,n)=.; y= b; solve a= 3 a= a= 2 2 4 3 2 y +ay x /2 (6 y)x=, y()=, y(4)= 2 4 8 6 4 2 2 2 4 a= 2 a= a=2 2 2 4 2 5 2 4 2 4 end Peer Teaching (2 minutes to think, explain to your neighbour and vote) omputational cost. Examine the number of operations required to solve a linear boundary value problem in dimension. Which method is more efficient? ψ shooting " about equal # cannot say! finite differences
i+ 2y i + y i y 2 + cy 2 i (6 y i)x i = 8i = 2 :::;N h ( G(yi ) = (2 h 2 x i )y i + ch 2 y 2 i Non-linear example: solve the boundary value problem in the interval x 2 [; 4] + cy 2 (6 y)x = with y() = ;y(4) = ; for c = : y Discretize using finite differences on a uniform mesh b i = 6h 2 x i This non-linear system of the form F(y) = is solved after inserting the boundary conditions using the Newton method y new = y old + ffiy, withjffiy = f(y old ). The Jacobian matrix J i;j = F i =y j is tridiagonal y i + G(y i ) + y i+ b i = with (y ) ::: G G (y 2 ) :::..... J = ::: G (y n ) ::: G (y n ) with G (y i ) = (2 h 2 x i ) + 2ch 2 y i. What should we take for the initial guess?
randolinj, y"+c*y^2=(6-y)x; y()=;y(4)=- bandmatrix, non-linear system with Newton's method clf clear, xend= 4; x=[ xend]; x=; yend=-; y=[ ]; plot(x,y); hold on y=; + cy^2 =(6-y)x, y()=, y(4)=- ') title('y" ntot=4; h=xend/ntot; Solve linear problem with c= for an initial guess c= one=ones(n-,); b=6*h^2*x; dia=-(2-h^2*x); b(n)=b(n)-yend; b()=b()-y; y=tridia(dia,one,one,b); x; xend]; Y=[y; y; yend]; plot(x,y,'--') X=[; Method: gradually increase the non-linearity With strongly non-linear equations, first switch-off the non-linear term and calculate a linear solution for the inital guess. Then gradually switch-on the nonlinearity, using increasingly non-linear solutions to start the Newton iterations. In Matlab this can be implemented as n=ntot-; x=h*(:n)'; [ymax,ix]=max(y); text(x(ix),ymax,'c='), drawnow
ymax [ymax,ix]=max(y); num2str(c)]), drawnow text(x(ix),ymax,['c=' end plot(x,y,'r') Gradually increase c using Newton's method with previous solutions as initial guess 4 y" + cy 2 =(6 y)x, y()=, y(4)= cvec=[.25.5.]; nr=:3 for err=; c=cvec(nr), experimentally iter=; 3 2 c=. c=.25 c=.5 err>e-4 & iter<5 while Gprim=dia+2*c*h^2*y; G=dia.*y+c*h^2*y.^2; y(:n-)]+g+[y(2:n);yend]-6*h^2*x; F=[y; J dy=-f(y) dy=-tridia(gprim,one,one,f); err=norm(dy,inf)/norm(y,inf) iter=iter+; y=y+dy; end iter==5, disp('no convergence'),break,end if 2 c= 3 2 3 4 Y=[y; y; yend]; plot(x,y,':') The non-linearity is very strong as can be seen from the plot.