Example: Bisection Method

36
Example: Bisection Method The bisection method in mathematics is a root-finding method that repeatedly bisects an interval and then selects a subinterval in which a root must lie for further processing. (Why?) It is often used to obtain an approximate solution. Zheng-Liang Lu 205 / 240

Transcript of Example: Bisection Method

Page 1: Example: Bisection Method

Example: Bisection Method

The bisection method in mathematics is a root-findingmethod that repeatedly bisects an interval and then selects asubinterval in which a root must lie for further processing.(Why?)

It is often used to obtain an approximate solution.

Zheng-Liang Lu 205 / 240

Page 2: Example: Bisection Method

Zheng-Liang Lu 206 / 240

Page 3: Example: Bisection Method

Idea of Bisection Method

1. At each step, the algorithm divides the interval in two bycomputing the midpoint c = (a + b)/2 of the interval and thevalue of the function f (c) at that point.

2. Unless c is itself a root (which is very unlikely, but possible),there are now two possibilities:

either f (a) and f (c) have opposite signs and bracket a root,

or f (c) and f (b) have opposite signs and bracket a root.

Zheng-Liang Lu 207 / 240

Page 4: Example: Bisection Method

3. The method selects the subinterval that is a bracket as a newinterval to be used in the next step.

4. In this way the interval that contains a zero of f is reduced inwidth by 1

2 at each step.

5. The process is continued until the interval is sufficientlysmall.1

1How small? A certain number you give is to be the stop criteria.Zheng-Liang Lu 208 / 240

Page 5: Example: Bisection Method

Problem Formulation

Input

- Endpoints of the interval [a, b] for any real numbers a < b

- Minimal interval length epsint = |b − a|- Absolute error epsabs = |f (xi )− 0|, i = a, b

Output

- the root r

Note that a, b will be updated in each step.

Zheng-Liang Lu 209 / 240

Page 6: Example: Bisection Method

Solution

1 clear; clc; format long;2 % main: input parameters3 disp('Root Finding using Bisection Method in [a,b].')4 disp('f = x.ˆ3-x-2'); % target function5 a = input('a = ?\n');6 b = input('b = ?\n');7 f = @(x) x.ˆ3-x-2; % target function8 eps int = 1e-5;9 eps abs = 1e-9;

10 %% main: algorithm11 if f(a) == 012 r = a; % lucky a13 elseif f(b) == 014 r = b; % lucky b15 else16 while ( (b-a > eps int) | | ...17 (abs(f(a)-0) > eps abs && abs(f(b)-0) ...

> eps abs ) )

Zheng-Liang Lu 210 / 240

Page 7: Example: Bisection Method

18 c = (a+b)/2; % middle point19 if (f(c) == 0)20 r = c; % lucky c21 break;22 elseif (f(a)*f(c) < 0)23 b = c;24 elseif (f(b)*f(c) < 0)25 a = c;26 else27 error('Failure: f(a)*f(c)>0 and ...

f(c)*f(b)>0.');28 return; % terminate the program29 end30 end31 r = c % approximate solution which satisfies ...

the criterion32 end

Zheng-Liang Lu 211 / 240

Page 8: Example: Bisection Method

−1 −0.5 0 0.5 1 1.5 2−3

−2

−1

0

1

2

3

4

c = 1.52137970691547

Zheng-Liang Lu 212 / 240

Page 9: Example: Bisection Method

Remarks

Time complexity: O(log2(n))

What is n? n =b − a

epsint.

This means that you need to make a trade-off between thenumerical precision, that is, the number of digits, and thecomputation time.

Be aware that this algorithm works well only with the premisethat the behavior in [a, b] is mild.

Approximate solutions may be significantly influenced by theinitial interval [a, b].

f (c) ≈ 0 but not equal to exactly 0. (Why?)

Zheng-Liang Lu 213 / 240

Page 10: Example: Bisection Method

Exercise

Please make your bisection method algorithm into auser-defined function, say, bisec.

So, you can call bisec to find a root for a specific function inthe command window or other programs.

Besides, you should extend the function with more inputarguments for parameters used in bisec.

Zheng-Liang Lu 214 / 240

Page 11: Example: Bisection Method

Problem Formulation

Input

- Target function f

- Endpoints of the interval [a, b] for any real numbers a < b

- Minimal interval length epsint = |b − a|- Absolute error epsabs = |f (xi )− 0|, xi = a, b

Output

- The root r

Zheng-Liang Lu 215 / 240

Page 12: Example: Bisection Method

1 function r=bisec(f,a,b,eps int,eps abs)2

3 ...

1 >> bisec(@(x) x.ˆ3-x-2,0,3,1e-9,1e-9)2

3 ans =4

5 1.5214

Zheng-Liang Lu 216 / 240

Page 13: Example: Bisection Method

Recursive Functions

Recursion is when something is defined in terms of itself.

A recursive function is a function that calls itself.

Recursion is an alternative form of program control.

It is essentially repetition without a loop.

Zheng-Liang Lu 217 / 240

Page 14: Example: Bisection Method

Example

The factorial of a non-negative integer n, denoted by n!, is theproduct of all positive integers less than or equal to n.2

For example,

4! = 4× 3!

= 4× (3× 2!)

= 4× (3× (2× 1!))

= 4× (3× (2× (1)))

= 4× (3× (2))

= 4× (6)

= 24.

Can you find the pattern?

2Note that 0! = 1.Zheng-Liang Lu 218 / 240

Page 15: Example: Bisection Method

Divide and Conquer

In general, to solve a problem using recursion, you break itinto subproblems.

Each subproblem is the same as the original problem butsmaller in size.

You can apply the same approach to each subproblem to solveit recursively.

Zheng-Liang Lu 219 / 240

Page 16: Example: Bisection Method

Zheng-Liang Lu 220 / 240

Page 17: Example: Bisection Method

Exercise

Write a program which determines return a factorial of n.

1 function y = recursiveFactorial(n)2 if n == 0 | | n == 1 % base condition3 y = 1;4 else5 y = n * recursiveFactorial(n-1);6 end7 end

Note that there must be a base condition in recursion.

So, can you replace a recursive method by a loop?

Zheng-Liang Lu 221 / 240

Page 18: Example: Bisection Method

Equivalence: Recursion Replaced by Loops

1 function y = loopFactorial(n)2 y = 1;3 for i = 1 : n4 y = y * i;5 end6 end

One more intriguing question is, Can we always turn arecursive method into an iterative one?

Yes, theoretically.3

3The Church-Turing thesis proves it if the memory serves.Zheng-Liang Lu 222 / 240

Page 19: Example: Bisection Method

Example: Fibonacci Numbers4

The Fibonacci sequence is a sequence with the followingpattern:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 . . . .

Alternatively, the Fibonacci sequence is defined by therecurrence relation

Fn = Fn−1 + Fn−2,

where n ≥ 3 and F1 = 0,F2 = 1.

Given a positive integer n, determine Fn.

Input: n ≥ 1Output: Fn

4See Fibonacci numbers.Zheng-Liang Lu 223 / 240

Page 20: Example: Bisection Method

Solution

1 function y = recursiveFib(n)2 if n == 13 y = 0;4 elseif n == 25 y = 1;6 else7 y = recursiveFib(n-1) + recursiveFib(n-2);8 end9 end

It is a binary recursion, because there are two calls in thefunction.

Time complexity: O(2n) (Why?)

Can we implement a linear recursion for this problem?

Also, you can implement it without recursive algorithms.(Try.)

Zheng-Liang Lu 224 / 240

Page 21: Example: Bisection Method

Remarks

Recursion bears substantial overhead.

So, the recursive algorithm may execute a bit more slowlythan the iterative equivalent.5

Besides, a deeply recursive method depletes the call stack,which is limited, and causes a exception fast.6

In practice, the decision whether to use recursion or iterationshould be based on the nature of, and your understanding of,the problem you are trying to solve.

5In modern compiler design, recursion elimination is automatic if therecurrence occurs in the last statement in the recursive function, so called tailrecursion.

6Stack overflow.Zheng-Liang Lu 225 / 240

Page 22: Example: Bisection Method

1 >> Lecture 42 >>3 >> -- Graphics4 >>

Zheng-Liang Lu 226 / 240

Page 23: Example: Bisection Method

Introduction

Engineers use graphing techniques to make the informationeasier to understand.

With a graph, it is easy to identify trends, pick out highs andlows, and isolate data points that may be measurement orcalculation errors.

Graphs can also be used as a quick check to determinewhether a computer solution is yielding expected results.

A set of ordered pairs is used to identify points on atwo-dimensional graph.

The data points are then connected by lines in common cases.

Zheng-Liang Lu 227 / 240

Page 24: Example: Bisection Method

Simple xy Plots

MATLAB provides the simplest function plot(x,y,’CLM’)which is an overloaded function, where

(x,y): data set,C: colors,L: line types,M: data markers.

A graphic window automatically opens when plot is called.

Zheng-Liang Lu 228 / 240

Page 25: Example: Bisection Method

About CLM

Zheng-Liang Lu 229 / 240

Page 26: Example: Bisection Method

Example

1 clear all;2 clc;3 % main4 x=0:0.1:2*pi;5 y=sin(x);6 figure(1); % create Figure 17 plot(x,y,'r-'); % red line8 grid on; % show grid line

Note that you can simply call figure without specifying anumber.

Zheng-Liang Lu 230 / 240

Page 27: Example: Bisection Method

0 1 2 3 4 5 6 7−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Zheng-Liang Lu 231 / 240

Page 28: Example: Bisection Method

Example 2: Multiple Curves

1 clear all;2 clc;3 % main4 x=linspace(0,2*pi,100);5 figure(2); % Create Figure 26 plot(x,sin(x),'*',x,cos(x),'o',x,sin(x)+cos(x),'+');7 grid on;

plot(x ,A), where A is an m-by-n matrix and x is an array ofsize n, draws a figure for m lines. (Try.)

Zheng-Liang Lu 232 / 240

Page 29: Example: Bisection Method

0 1 2 3 4 5 6 7−1.5

−1

−0.5

0

0.5

1

1.5

Zheng-Liang Lu 233 / 240

Page 30: Example: Bisection Method

Exercise

Plot sin(x), x2, log10(x), and 2x where x ∈ [0, 1] with 100points in a single figure.

1 clear; clc;2

3 x = linspace(0,1,100);4 y = [sin(x);5 x.ˆ2;6 log10(x);7 2.ˆx];8 C = 'rgby';9 M = '.od+';

10 L = {'-','-.',':','--'};11 for i = 1:412 plot(x,y(i,:),[C(i) M(i) L{i}]);13 end

Zheng-Liang Lu 234 / 240

Page 31: Example: Bisection Method

0 0.2 0.4 0.6 0.8 1−2

−1.5

−1

−0.5

0

0.5

1

1.5

2

Zheng-Liang Lu 235 / 240

Page 32: Example: Bisection Method

Annotating Plots

title adds a title to a plot.

xlabel adds a label to the x-axis.

ylabel adds a label to the y -axis.

legend allows you to add a legend to your graph.

The legend shows a sample of the line and lists the string youhave specified.

text allows you to add a text box to the graph.

The box is placed at the specified x- and y - coordinates andcontains the string value specified.

gtext is similar to text.

The box is placed at a location determined interactively by theuser by clicking in the figure window.

Zheng-Liang Lu 236 / 240

Page 33: Example: Bisection Method

Example

1 clear all;2 clc;3 % main4 x = linspace(0,1,100);5 y = [sin(x);x.ˆ2;log10(x);2.ˆx];6 color = 'rgbk';7 figure(1);hold on; grid on;8 for i = 1 : 49 plot(x,y(i,:),[color(i) 'o:']);

10 end11 title('Demonstration');12 xlabel('Time (Year)');13 ylabel('This is Y label. (Unit)');14 legend({'Curve A', 'xˆ{2}', 'Quadtratic', ...

'Exponential'});15 text(0.4,-1.5,'This is text.');16 gtext('Support special symbols of LaTex.');

Zheng-Liang Lu 237 / 240

Page 34: Example: Bisection Method

0 0.2 0.4 0.6 0.8 1−2

−1.5

−1

−0.5

0

0.5

1

1.5

2Demonstration

Time (Year)

Thi

s is

Y la

bel.

(Uni

t)

This is text.

Support speical symbols of LaTex.

Curve A

x2

QuadtraticExponential

Zheng-Liang Lu 238 / 240

Page 35: Example: Bisection Method

Exercise: Simulation on Stock Price7

1 clear; clc;2 % main3 y = zeros(1,1000);4 y(1) = 100;5 for i = 2 : length(y)6 y(i) = y(i - 1) + 10 * randn;7 end8 hold on;9 plot(1 : length(y), y, '.:');

10 title('MATLAB-244');11 ylabel('Stock Price (TWD)');12 xlabel('Trading Day (Day)');13 grid on;

7In financial theory, the stock price S is modelled by Black-Scholes formula,given by dS = µSdt + σSdW , where µ is the annual yield, σ is the annualvolatility, and dW follows a Brownian motion.

Zheng-Liang Lu 239 / 240

Page 36: Example: Bisection Method

0 200 400 600 800 100085

90

95

100

105

110

115

120

125

130

135MATLAB−244

Sto

ck P

rice

(TW

D)

Trading Day (Day)

Zheng-Liang Lu 240 / 240