Sunteți pe pagina 1din 4

ODE BVP satisfies “extra conditions”

Numerical Analysis 2, lecture 6: at two or more points

Boundary Value Problems second order BVP


(chapter 10 sections 8, 9 & 11) y!! = f (x, y, y! ), g 1 (y(a), y!(a)) = 0
g2 (y(b), y!(b)) = 0

• ode bvp linear second order BVP


• finite difference method y!!(x) + p(x) y!(x) + q(x)y(x) = r(x), y(a) = " , y(b) = #

• shooting method 0.14

example: temperature in rod (p. 337)


(! y" )" = 0 y(0) = 100

y(1) = 0

0
0 1

Numerical Analysis 2, lecture 6, slide! 2

ODE BVP existence/uniqueness theory Derivatives can be approximated


differs from ODE IVP theory by finite differences

y(x + h) " y(x " h)


y!(x) = + O(h 2 )
a BVP with 2 solutions 2h

y!! = " y , y(0) = 0 h h


y(4) = "2
y(x + h) " 2y(x) + y(x " h)
y!!(x) = + O(h 2 )
h2

sinh x
2
y(x) = !2
sinh 4
(0 " x " 4) derivation:
0
Find the polynomial p(z) that interpolates y(z) at z !{x " h, x, x + h}.
$ 2 sin x
& sinh(4 ! " ) (0 # x # " ) Then y#(x) $ p#(x) and y##(x) $ p##(x).
&
!2
0 4 y(x) = %
&! 2 sinh(x ! " ) (" # x # 4)
&' sinh(4 ! " )
error term: Taylor

Numerical Analysis 2, lecture 6, slide! 3 Numerical Analysis 2, lecture 6, slide! 4


Derivatives on a grid can be approximated Linear BVP can be approximated as a
using differentiation matrices differentiation matrix equation

grid h=
b!a
, xn = a + nh, yn " y(xn ) yN = ! linear BVP
N … y!!(x) + p(x) y!(x) + q(x)y(x) = r(x), y(a) = " , y(b) = #
! = y0
y1
… yn–1 yn
yn+1

h h
finite difference approximation
differentiation matrices yn+1 ! 2yn + yn!1 yn+1 ! yn!1
+ pn + qn yn = rn
# y1! & #0 1 & # y1 & # "* & h2 2h
% y! ( % "1 0 " (% y ( % (
% 2 () 1 % (% 2 ( + 1 % (
% ! ( 2h %
% y! ( %
" " 1 ( % ! ( 2h % ( diff. matrix equation
$ N "1 ' #%%%
$ "1 0 ( % y ( %+ (
D2 y + b 2 + diag(p) ( D1y + b1 ) + diag(q)y = r
$%%%&' $ N "1 ' #
%$$ %&'
D1 b1
" y1 % " p(x1 ) % " q(x1 ) % " r(x1 ) %
# y1!! & # "2 1 & # y1 & #* & where y = $ ! ' , p = $ ! ', q = $ ! ', r=$ ! '
% y!! ( % 1 "2 " (% y ( % ( $ ' $ ' $ ' $ '
% 2 () 1 % (% 2 ( + 1 % ( $# yN !1 '& $# p(x N !1 ) '& $# q(x N !1 ) '& $# r(x N !1 ) '&
% ! ( h %
2 " " 1 (% ! ( h % (
2
% y!! ( %
$ N "1 ' #%%%
$ 1 "2 (' %$ yN "1 (' %+ (
$ ' ( D2 + diag(p)D1 + diag(q)) y = r ! diag(p)b1 ! b 2
$%%%& #$&
D2 b2
Numerical Analysis 2, lecture 6, slide! 5 Numerical Analysis 2, lecture 6, slide! 6

Example The finite difference method in Matlab

example (p.342) y!! " y = x 2 " 2, y(0) = 1 example (p.342) y!! " y = x 2 " 2, y(0) = 1
y(1) = cosh(1) " 1 y(1) = cosh(1) " 1

function err = bvp1(N)


a = 0; b = 1;
Solution: 1
h = , xn = nh
h = (b-a)/N;
x = (a:h:b)';
1

N
alpha = 1; beta = cosh(1)-1;
yn!1 ! 2yn + yn+1 e = ones(N-1,1);
! yn = xn2 ! 2 n "{1,…, N ! 1}, y0 = 1, yN = cosh(1) ! 1 D2 = spdiags([e -2*e e],[-1:1],N-1,N-1)/h^2;
h2 b2 = [alpha; zeros(N-3,1); beta]/h^2;
q = -ones(N-1,1);
r = x(2:end-1).^2-2;
" 1 %
%+ " y1 % " x1 ! 2 %
2
( " !2 1 % " !1 $ '
y = (D2+diag(q))\(r-b2);

* 1 $ 1 !2 ! ' $ '- $ y ' $ ' 0 plot(x,[alpha;y;beta])


!1 $ x 2
! 2 ' 1 $ ' err = max(abs([alpha;y;beta]-(cosh(x)-x.^2)));
* $ '+$ '- $ 2 '= 2
! 2$ " '
0
0 1

* h2 $ ! ! 1 ' $ ! '- $ " ' $ " ' h $


$ ' 0 '
* $ 1 !2 '& $# !1'&-, $# yN !1 '& $ x 2 ! 2 ' $ ' >> bvp1(50)
) # # N !1 & $# cosh(1) ! 1'& ans =
4.3498e-06
solve for y1, y2 ,…, yN !1
>> bvp1(100) O(h2) accuracy
ans =
Numerical Analysis 2, lecture 6, slide! 7 1.0875e-06 Numerical Analysis 2, lecture 6, slide! 8
The shooting method uses an IVP code The shooting method in Matlab
to solve a BVP
y!! = f (x, y, y! ), g 1 (y(a), y!(a)) = 0
g2 (y(b), y!(b)) = 0
example (p. 353)
$ z2 ' $ z1 (0) '
y!! + y = 0, y(0) = 0, y(2) = 1 ! z " = & ) , & =0
#z1 ( %1 # z1 (2) )(
convert to 1st-order %!
f (x,z)
# z2 & # g1 (z(a)) &
z1 = y, z2 = y! " z ! = % (, % (=0 shooting problem: Find ! such that 1 " z1 (2) = 0,
$ f#
!
(x, z1, z2 ) ' $ g2 (z(b)) '
#"## $ !#"#$
f (x,z) g(z(a),z(b)) #0 &
where z(x) is the solution of the IVP z ' = f (x, z), z(0) = % ( .
$! '

shooting = root finding >> f=@(x,z) [z(2);-z(1)];


>> gamma=1; [x,z] = ode45(f,[0 2],[0;gamma]); y(2) = 0.9093
>> error = 1-z(end,1)
Find v such that g(v, z(b)) = 0,
error = y!(0) = 1
where z(x) is the solution of the IVP z ' = f (x, z), z(a) = v. 0.0907 y(2) = 0
>> gamma=0; [x,z] = ode45(f,[0 2],[0;gamma]); 0
x
2
y!(0) = 0
linearity If f (x, z) = Az + b then z(b) = Cv + d
>> error = 1-z(end,1)

error = solution: ! = 1"


0.0907
= 1.0998
1 # 0.0907 " 1 &
if g is also linear then secant method converges in one iteration step %$ ('
1" 0
Numerical Analysis 2, lecture 6, slide! 9 Numerical Analysis 2, lecture 6, slide! 10

A general-purpose root-finding code Shooting method


can be used to find the shooting parameter vs. finite difference method
example (p. 354)
# z2 & # 1 ) z1 (0) & shooting
y!! = 1 + yy!, y(0) = 1, y(0.6) = 2 ! z " = % , =0
1 + z1z2 (' %$ 2 ) z1 (0.6) ('
$! ! can use existing IVP and rootfinding codes
#"# $
f (x,z) ! root-finding problem may be ill-conditioned (unstable IVP)
shooting problem: Find ! such that 2 " z1 (0.6) = 0, even when BVP is well-conditioned
#1 &
! IVP solution may not be defined over entire [a,b]
where z(x) is the solution of the IVP z ' = f (x, z), z(0) = % ( .
$! '
function doit
gammaguess = 1;
gamma = fzero(@g,gammaguess);
[x,z] = solveIVP(gamma); 2

plot(x,z(:,2))

function error=g(gamma)
finite difference
[x,z]=solveIVP(gamma);
error=2-z(end,1);
! need to solve large system of equations
1
end
!!the equation system is sparse
function [x,z]=solveIVP(gamma)
f=@(x,z) [z(2); 1+z(1)*z(2)]; !!method works well when BVP is well-conditioned
x=0:.01:0.6;
[x,z]=ode45(f,x,[1;gamma]); 0
0 0.6
end

end Numerical Analysis 2, lecture 6, slide! 11 Numerical Analysis 2, lecture 6, slide! 12


what happened

• BVP solution may not be unique


• finite difference formulas for derivatives
approximate BVP by a system of equations

• shooting method: choose initial conditions,


solve IVP, iterate to match boundary
condition

Numerical Analysis 2, lecture 6, slide! 13

S-ar putea să vă placă și