Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Shooting Method for Boundary-value Problems

Similar presentations


Presentation on theme: "The Shooting Method for Boundary-value Problems"— Presentation transcript:

1 The Shooting Method for Boundary-value Problems
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © 2012 by Douglas Wilhelm Harder. Some rights reserved.

2 Outline This topic to approximating boundary-value problems
The Shooting Method for Boundary-value Problems Outline This topic to approximating boundary-value problems We will describe boundary-value problems (BVPs) We will look at solutions with linear ordinary differential equations (ODEs) We will consider solutions for non-linear ODEs This will require successive approximations using the secant method

3 Outcomes Based Learning Objectives
The Shooting Method for Boundary-value Problems Outcomes Based Learning Objectives By the end of this laboratory, you will understand: Boundary-value problems (BVPs) How to use an initial-value problem (IVP) solver to approximate solutions to BVPs The solution to linear BVPs The application of the secant method to solve non-linear BVPs

4 The Shooting Method for Boundary-value Problems
Using Matlab These methods assume that the student has written the initial-value problem solver pd45 with the signature dp45( f, x_rng, u0, h, eps_abs ) which uses the Dormand-Prince method If this routine is not available, you are welcome to use the built-in Matlab routine ode45 which has the similar signature ode45( f, x_rng, u0 )

5 The Shooting Method for Boundary-value Problems
2nd-order ODEs A boundary-value problem in one dimension is any 2nd-order ODE F(x, u(x), u(1)(x), u(2)(x)) = 0 with two constraints u(a) = ua u(b) = ub In general, we will look at functions of the form u(2)(x) = f(x, u(x), u(1)(x))

6 The Shooting Method for Boundary-value Problems
2nd-order ODEs Consider the corresponding initial-value problem for this 2nd-order ODE F(x, u(x), u(1)(x), u(2)(x)) = 0 with two constraints u(a) = ua u(1)(a) = ua(1) Here we specify the slope at the left-hand point x = a

7 2nd-order ODEs Thus, a boundary-value problem could be restated as:
The Shooting Method for Boundary-value Problems 2nd-order ODEs Thus, a boundary-value problem could be restated as: Given one initial condition u(a) = ua, what slope is required at that initial point so that the solution to that initial condition passes through the point (b, ub)? (b, ub) (a, ua)

8 2nd-order ODEs We will consider two possible cases:
The Shooting Method for Boundary-value Problems 2nd-order ODEs We will consider two possible cases: When the ODE is linear, and When it is not (b, ub) (a, ua)

9 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs A 2nd-order ODE is said to be linear if it can be written as u(2)(x) + q(x) u(1)(x) + r(x) u(x) = g(x) or u(2)(x) = g(x) – q(x) u(1)(x) – r(x) u(x) We will also consider the corresponding homogenous LODE: u(2)(x) + q(x) u(1)(x) + r(x) u(x) = 0 u(2)(x) = –q(x) u(1)(x) – r(x) u(x)

10 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs To demonstrate 2nd-order LODEs, we will consider the example The corresponding homogeneous LODE is the

11 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs Recall that if ug(x) is a solution to a 2nd-order LODE and u0(x) is a solution to the corresponding homogenous LODE, then ug(x) + c·u0(x) is also a solution the LODE g(x)

12 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs Suppose we have redefine a boundary-value problem in terms of two IVPs, the first on the LODE with constraints and the second on the homogenous LODE with constraints Let ug(x) and u0(x) be the solutions, respectively

13 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs For example, given the LODE from before, we create the two IVPs The plots of the solutions: (b, ub) ug(x) (a, ua) u0(x)

14 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs Having found these two solutions, ug(x) and u0(x), we now want to find a linear combination of these two such that it satisfies the second boundary value For example, here we see ug(x), ug(x) + u0(x), and ug(x) – u0(x) It seems ug(x) + 2u0(x) will pass close to the second boundary point (b, ub) ug(x) + u0(x) (a, ua) ug(x) ug(x) – u0(x)

15 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs We want to find the appropriate linear combination to match our second boundary condition First, we note We want Solving this for c yields

16 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs Easy enough; however, recall that we must find ug(x) and u0(x) but if we use dp45, the step sizes may be different… Solution: define a system of two uncoupled equations and unknowns and solve them simultaneously u(2)(x) = g(x) – q(x) u(1)(x) – r(x) u(x) u(2)(x) = 0 – q(x) u(1)(x) – r(x) u(x)

17 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs Using our techniques from Laboratory 5, we can write this as a system of IVPs: w1(1)(x) = w2 (x) w2(1)(x) = g(x) – q(x) w2(x) – r(x) w1(x) w3(1)(x) = w4 (x) w4(1)(x) = 0 – q(x) w4(x) – r(x) w3(x) w1(a) = ua w2(a) = 0 w3(a) = 0 w4(a) = 1 ug(x) = w1(x) ug(1)(x) = w2(x) u0(x) = w3(x) u0(1)(x) = w4(x)

18 2nd-order Linear ODEs For example, consider the boundary-value problem
The Shooting Method for Boundary-value Problems 2nd-order Linear ODEs For example, consider the boundary-value problem The Matlab function would be: function [dw] = f6a( x, w ) dw = [w(2); 1 - sin(x)*w(2) - w(1); w(4); 0 - sin(x)*w(4) - w(3)]; end with the initial conditions [u_a, 0, 0, 1]'

19 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs For our example, we would run the following code: a = 2; b = 3; u_a = 1.5; u_b = 2.5; [x6a, u6a] = [a, b], [u_a 0 0 1]', 0.01, 1e-8 ); c6a = (u_b - u6a(1, end))/u6a(3, end) c6a = u6a_soln = u6a(1, :) + c6a*u6a(3, :); plot( x6a, u6a_soln, 'r' ) ylim( [0, 2.5] ) u6a_soln(1) ans = u6a_soln(end)

20 2nd-order Linear ODEs Maple can find an answer, but it’s not nice…
The Shooting Method for Boundary-value Problems 2nd-order Linear ODEs Maple can find an answer, but it’s not nice… > a := 2; b := 3; u_a := 1.5; u_b := 2.5; > dsolve( + sin(x)*D(u)(x) + u(x) = 1, u(a)=u_a, u(b)=u_b} );

21 The Shooting Method for Boundary-value Problems
2nd-order Linear ODEs It takes Maple about two minute to plot the same figure Note: this is not an issue with Maple—it is giving you the exact solution and not a numeric approximation

22 2nd-order Linear ODEs: Example
The Shooting Method for Boundary-value Problems 2nd-order Linear ODEs: Example For example, consider the boundary-value problem u(2)(x) + 4 u(1)(x) + 7x u(x) = sin(x) u(2) = 1.5 u(3) = 2.5 The Matlab function would be: function [dw] = f6b( x, w ) dw = [w(2); sin(x) - 4*w(2) - 7*x*w(1); w(4); *w(4) - 7*x*w(3)]; end

23 2nd-order Linear ODEs: Example
The Shooting Method for Boundary-value Problems 2nd-order Linear ODEs: Example For example, consider a = 2; b = 3; u_a = 1.5; u_b = 2.5; [x6b, u6b] = [a, b], [u_a 0 0 1]', 0.01, 1e-4 ); u6b_soln = u6b(1,:) + (u_b - u6b(1,end))/u6b(3,end)*u6b(3,:); plot( x6b, u6b_soln, 'o-' ); grid

24 2nd-order Linear ODEs: Example
The Shooting Method for Boundary-value Problems 2nd-order Linear ODEs: Example Maple can find an answer, but it’s not nice… > a := 2; b := 3; u_a := 1.5; u_b := 2.5; > dsolve( + 4*D(u)(x) + 7*x*u(x) = sin(x), u(a) = u_a, u(b) = u_b}, u(x) );

25 2nd-order Linear ODEs: Example
The Shooting Method for Boundary-value Problems 2nd-order Linear ODEs: Example Comparing the results with Maple’s plot, we see they are similar

26 2nd-order Linear ODEs: Example
The Shooting Method for Boundary-value Problems 2nd-order Linear ODEs: Example If we repeat using b = 4, we get significantly more varying, but still similar, graphs

27 2nd-order Non-linear ODEs
The Shooting Method for Boundary-value Problems 2nd-order Non-linear ODEs What happens if the ODE is not linear? In this case, there is no guarantee that we can find a solution using this simple technique Instead, we will use u(2)(x) = f(x, u(x), u(1)(x)) and let us(x) be the solution to the IVP u(a) = ua u(1) (a) = s Now, define a new function err(s) = us(b) – ub

28 2nd-order Non-linear ODEs
The Shooting Method for Boundary-value Problems 2nd-order Non-linear ODEs When this function has a zero, we have a solution to the boundary-value problem err(s) = us(b) – ub What is err(s) in Matlab? function [du_b] = err_shot( s ) [t_out, u_out] = ... [a, b], [u_a, s], h, eps_abs ); du_b = u_out(1, end) - u_b; end

29 2nd-order Non-linear ODEs
The Shooting Method for Boundary-value Problems 2nd-order Non-linear ODEs When this function has a zero, we have a solution to the boundary-value problem err(s) = us(b) – ub In reality, however, it will require a number of other parameters: function [du_b] = err_shot(s, f, x_rng, u_bndry, h, eps_abs) u_a = u_bndry(1); u_b = u_bndry(2); [~, u_out] = dp45( f, x_rng, [u_a, s]', h, eps_abs ); du_b = u_out(1, end) - u_b; end

30 Secant Method We will use the secant method for fining this root:
The Shooting Method for Boundary-value Problems Secant Method We will use the secant method for fining this root: Suppose we are trying to find a root of a real-valued function of a real variable err(s) Suppose s1 and s2 are two initial approximations of the root err(s) s2 s1

31 The Shooting Method for Boundary-value Problems
Secant Method We can find a better approximation of the root by finding the interpolating straight line that passes through these points err(s) s2 s1

32 Secant Method The formula for this new point is err(s) s3 s2 s1
The Shooting Method for Boundary-value Problems Secant Method The formula for this new point is err(s) s3 s2 s1

33 Secant Method In the first case, we considered linear ODEs
The Shooting Method for Boundary-value Problems Secant Method In the first case, we considered linear ODEs Essentially, what we are doing here is asking: Given these two approximations, what would our next approximation be if the ODE was linear? err(s) s3 s2 s1

34 The Shooting Method for Boundary-value Problems
Secant Method Now, suppose we have s2 and s3, the next step is to use these approximations to find the next approximation, s4, and so on... err(s) s3 s2 s1

35 The Shooting Method for Boundary-value Problems
Secant Method Thus, we will continue to iterate, using the secant method at each step... With any iterative numerical method, we must know under what conditions we will: Halt with success, and Indicate a failure to find a solution After all, not all iterative numerical methods converge...

36 The Shooting Method for Boundary-value Problems
Secant Method For the secant method, we must have two approximations, s1 and s2 First, if |err(s1)| < |err(s2)|, we will swap s1 and s2 We’re assuming that s2 is more accurate

37 Secant Method We will iterate at most Nmax times:
The Shooting Method for Boundary-value Problems Secant Method We will iterate at most Nmax times: With each iteration, we will approximate a new point s If |s2 – s| < estep and |err(s)| < eabs, we will assume we are finished and we will return the value s (shooting, however, will take one more step) Otherwise, we will set s1 = s2 and s2 = s If we have iterated Nmax times and not found a solution, we will throw an exception with an appropriate message

38 The Shooting Method for Boundary-value Problems
Normally, the secant method simply returns the slope Your function, shooting, however, will have to return the x- and u-values corresponding to that optimal slope Thus, your last steps before you successfully return will be: [x_out, u_out] = dp45( f, x_rng, [u_bndry(1), s]', h, eps_abs ); return;

39 The Shooting Method The arguments of are as follows:
The Shooting Method for Boundary-value Problems The Shooting Method The arguments of function [x_out, u_out] = shooting( s1, s2, f, x_rng, u_bndry, ... h, eps_abs, eps_step, N_max ) are as follows: s1 and s2 are the two approximations of the slopes f is a function handle for the differential equation: u(2)(x) = f(x, u(x), u(1)(x)) [a, b] is a row vector defining the range on which we are approximating the boundary-value problem [ua, ub] is a row vector defining the boundary conditions: u(a) = ua and u(b) = ub h is the initial step size for the function dp45 eabs is the parameter passed to dp45 and is also the parameter used by the secant method estep is the parameter used by the secant method for the step size Nmax is the parameter used by the secant method to define the maximum number of iterations

40 The Shooting Method Question: what do we use as initial conditions?
The Shooting Method for Boundary-value Problems The Shooting Method Question: what do we use as initial conditions? Answer: as an engineer, you will understand your problem; you should know the expected behaviour around the point x = a In these laboratories, you will be given the initial points In reality, you will understand what they should be when you get there

41 The Shooting Method for Boundary-value Problems
Consider the following non-linear boundary-value problem: The corresponding function is function [dw] = f6c( x, w ) dw = [w(2); sin(x) - 4*w(2)*w(1) + 2*x*w(1)^2]; end

42 The Shooting Method When I run the function
The Shooting Method for Boundary-value Problems The Shooting Method When I run the function [x6c, u6c] = shooting( -3, [2, 3], [1.5, 2.5]', ... 0.01, 1e-6, 1e-6, 20 ); I get a solution after four iterations where the approximations of the initial slopes are: – – – – These last two slopes are sufficiently close enough and the u6c(1, end) = close enough to 2.5

43 The Shooting Method We can look at the plot and the result:
The Shooting Method for Boundary-value Problems The Shooting Method We can look at the plot and the result: plot( xs, us(1,:) ) us(1,end) ans = length( xs ) 36

44 The Shooting Method for Boundary-value Problems
You might get slightly different values depending on your implementation of the secant method; however, the final result should be reasonably close

45 The Shooting Method Plotting approximations with the various slopes:
The Shooting Method for Boundary-value Problems The Shooting Method Plotting approximations with the various slopes: slopes = [ ]; for s = slopes [x6s, u6s] = [2, 3], [1.5, s]', 0.01, 1e-6 ); plot( x6s, u6s(1,:) ) u6s(1,end) end s1 = 3 s2 = 3.1 All others...

46 The Shooting Method for Boundary-value Problems
If you plot the relative errors of the approximation of u6s(1,end) as an approximation of ub = 2.5, we observe the fast (i.e., O(hf)) convergence of the secant method s u6s(1,end) Absolute Error –3 1.686 × 10–1 –3.1 1.170 × 10–1 – 1.893 × 10–3 – 2.129 × 10–5 – 3.879 × 10–9 – 7.105 × 10–15

47 Hints You will work with various values of s and err(s)
The Shooting Method for Boundary-value Problems Hints You will work with various values of s and err(s) Instead of continually recalculating value err(s), just calculate err1 = err_shot( s1 ); err2 = err_shot( s2 ); and when you calculate a new value of s, just use a simpler expression with the variables s1, s2, err1 and err2 and then immediately calculate errs = err( s ); When you update s1 and s2, update err1 and err2

48 The Shooting Method for Boundary-value Problems
Summary We have looked at using the function dp45 to approximate a boundary-value problem The shooting method converts a BVP into an IVP For linear IVPs, we use the ODE and the homogeneous ODE and find a linear combination of the two solutions For non-linear IVPs, we use dp45 and the secant method to guide us to an approximation

49 The Shooting Method for Boundary-value Problems
References [1] Glyn James, Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2007. [2] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011. [3] John H. Mathews and Kurtis D. Fink, Numerical Methods using Matlab, 4th Ed., Prentice Hall, 2004, pp [4] Shooting Method,


Download ppt "The Shooting Method for Boundary-value Problems"

Similar presentations


Ads by Google