Download presentation
Presentation is loading. Please wait.
Published byAmanda King Modified over 8 years ago
1
Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant
2
For a function f(x) of a single independent variable, find all x 0 such that f(x 0 )=0. (Some methods can be generalized to multiple independent variables) Other ways that the problem can be stated: find all x where graphs of f 1 (x) and f 2 (x) intersect find all extrema of f(x) on [a,b] Define the problem
3
Graphical solutions In-class work: Create a directory for functions and scripts that will be used to find zeros of functions Navigate to that directory In the editor under “new” open the macro for function definition Type the following: function [ ]=graphical(fh,xa,xb) fplot(fh,[xa,xb]) grid on end fh is a “function handle” defined before graphical is called xa and xb are the range of the independent variable in the output plot
4
Calling graphical in the command window >>myf=inline(‘x^3-x^2-2*x+1’); >>graphical(myf,-1,1.5); Read approximate zero of myf(x) from graph
5
Alternative way to define myf >>myf=@(x) x^3-x^2-2*x+1; >>graphical(myf,-1,1.5); Read approximate zero of myf(x) from graph
6
Looks like this cubic has more than one real root >>myf=inline(‘x^3-x^2-2*x+1’); >>graphical(myf,-1,1.5); >>graphical(myf,-2,2.5); Read approximate zeros of myf(x) from graph
7
In-class work: Use graphical to find the intersections of e x and 3x 2
8
Solution: approximate points of intersection are -0.5, 1, and 3.75
9
Making a graph with MatLab’s plot function >>myf=inline(‘exp(x)-3*x.^2’); >>x=linspace(-1,4,50); >>plot(x,myf(x)); >>grid on Why x.^2 for x 2 ?
10
In-class work: Use graphical to estimate the zeros of f(x) = ln(x+1) + tan(2x) on [-1,9]
11
Does your graph look like this? Not very useful for estimating roots. Change range of y axis using >>axis([xmin,xmax,ymin,ymax])
12
X f(x) = ln(x+1) + tan(2x) Where are the roots? f(x)
13
Newton’s Method
14
Non-linear problem reduced to sequence of linear problems How do we get this result?
15
Generalize to x k and x k+1
16
Develop a pseudocode for Newton’s Method x k+1 = x k – f(x k )/f’(x k ) Basic idea behind calculation; calculate sequence x 1, x 2, … x k, x k+1,… set x 1 to the initial guess stop when change x k -> x k+1 is small assign root to x k+1 calculate f(x k+1 )
17
pseudocode for Newton’s method x k+1 = x k – f(x k )/f’(x k ) function [r,fr]=newton(fh,dfh,x0) Set parameter maxsteps convergence requirement on fractional change (minfc) Initialize steps fractional change upper bound x 1 while (fc>minfc) && (steps<maxsteps) increment steps save current estimate of root calculate next estimate of root calculate absolute value of fractional change end while loop r = best estimate of root fr = fh(r) end function
18
Assignment 1: due 1/19/16 Estimate all of the zero of x 3 -x 2 -2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical estimates. Hand in copies of your graph, your code for Newton’s method and the command window where the functions were called.
19
Find intersections of e x and 3x 2 by Newton’s method. How do you get Newton’s method to converge to a particular root?
20
Derive expected convergence rate of Newton’s method
21
Develop a code to compare convergence of Newton’s method with different initial guesses for root Task 1: modify Newton function to output convergence data. Save as a different version, newtonV2, for example. What should we use for measure of convergence? How do we save convergence data?
22
Note alternative to “inline” Note suppressed output of logre Modified Newton’s method for convergence data
23
Develop a code to compare convergence of Newton’s method with different initial guesses for root Task 2: write script to get convergence data for same root with different initial guesses Make a semi-log plot convergence data How do we make a semi-log plot? Put results for both guesses on the same set of axes
24
Pseudocode for script to compare convergence define f(x) and f’(x) inline Call newtonV2 with initial guess1 Save convergence data as array1 Call newtonV2 with initial guess2 Save convergence data as array2 Call plot(array1) Just one argument hold on Call plot(array2) hold off
25
Convergence of Newton’s method X axis is number of iterations X 0 =1.5 X 0 =0.5 Convergence to root of e x – 3x 2 in [0.5,1.5] with different x 0 Y axis is log10(re) -5 corresponds to re = 10 -5
26
Assignment 2, Due 1/21/16 f(x) = exp(x) - 3x 2 has a zero in the interval [0.5, 1.5]. Modify your Newton’s method code to return convergence data. On a semi-log plot, compare the rates of convergence to the root with initial guesses 0.5 and 1.5. Hand in a copy of your modified Newton’s method function, the script to get the convergence data and make the plot, and the plot of data.
27
Driver for graph to compare convergence of Newton’s method
28
Convergence problems with Newton’s method Example: 3.2-19 text p102 6 th edition
29
Roots with multiplicity greater than one
30
X f(x) = cos(x) – cos(3x) What are the multiplicities of these roots?
31
Implies zero between a and b Bisection method for continuous functions Let c = (a+b)/2 One of the following will be true If f(c)=0 then c is a root; stop If f(c)f(a)<0 then root is in [a,c]set b=c and repeat If f(c)f(b)<0 then root is in [c,b]set a=c and repeat
32
Implies zero between a and b Bisection method for continuous functions Let c = (a+b)/2 Bisection method does not require derivative of f(x) Requires starting values a and b with f(a)f(b)<0 Even though f(a)f(b)<0, a and b are not good starting values for f(x) shown above. Only one root should be between starting values
33
Newton to bisection Open newton in the editor and save as bisection Change name of the function and dummy variables to fh, xa, and xb Test if xa and xb are correctly chosen starting values. if f(xa)f(xb) > 0 write warning message Warning is sufficient. May converge to a root anyway. Relational operators in MatLab <less than <=less than or equal ==equal -=not equal >greater than >=greater than or equal
34
Newton to bisection 2: how does bisection work? xc = (xa + xb)/2 is the current best estimate of the root Initialize xc with the starting values of xa and xb If f(xc)=0 then the initial xc is a root. Assign values to outputs and stop. Initialize steps, minfc, and fc as in Newton Start the while loop
35
Does your code, thus far, look something like function [r,fr]=bisection(fh,xa,xb) if fh(xa)*fh(xb) >= 0 disp(‘warning: f(xa)f(xb) > 0’) end xc=(xa+xb)/2; if fh(xc) == 0 r=xc; fr=fh(r); else steps=0; minfc=1e-8; fc=1; while fc>minfc && (steps<20)
36
Is it reasonable to assume that f(xc)==0 will only occur at initialization of xc? If yes, then inside the while loop consider only if f(xc)f(xa)<0 then root is in [xa,xc]set xb=xc if f(xc)f(xb)<0 then root is in [xc,xb]set xa=xc If no, then inside the while loop also consider if f(xc)==0 assign output values and stop Inside while loop, f(xc)==0 is very unlikely Why is it a possibility at initialization?
37
X f(x) = ln(x+1) + tan(2x) x=0 is a root. Don’t need a root finder If bisection call with xb = -xa, initial xc is a root f(x)
38
Note alternative to “inline” Note suppressed output of logre Newton to bisection 3: convergence cycle Inside while loop, as in Newton save current xc calculate new xc calculate fractional change in xc increment steps Ignoring possibility that fh(current xc)==0, use if fh(xc)fh(xa)<0 then root is in [xa,xc]set xb=xc if fh(xc)fh(xb)<0 then root is in [xc,xb]set xa=xc to get the correct xa and xb to calculate the new xc. Do you really need the 2 nd if statement?
39
Does your while loop look something like while fc>minfc && (steps<20) xold=xc; if fh(xc)*fh(xa)<0 xb=xc; else xa=xc; end; xc=(xa+xb)/2; steps=steps+1; fc=abs((xc-xold)/xc); end %while loop
40
Assignment 3, Due 1/26/16 Write bisection function that finds root and saves convergence data Use code to find zero f(x) = e x - 3x 2 in the interval [0.5, 1.5]. On a semi-log plot, compare the rate of convergence of the bisection method to that of Newton’s method with initial guesses 0.5 and 1.5. Hand in a copy of your bisection-method function, the script to get and plot the convergence data for bisection and Newton’s method, and the plot of the data.
41
Your plot should look something like
42
In-class work: Test your bisection function f(x)=x 5 + x 3 +3 does not have a zero on [-1,1] Call your bisection function with xa=-1 and xb=1 Does it generates warning message? Does not converge to a zero of f(x)? How many cycles of convergence did it try? f(x) has a zero between -1.5 and -1 Did your bisection function find a zero at -1.1053?
43
More in-class work: Set format long Find the root of f(x)=x 5 + x 3 +3 between -1.5 and -1 by Newton’s method Compare result by Newton’s method to result with your bisection function
45
Linear on semi_log_y plot
46
Your plot should look something like
47
Newton’s method without derivatives ~ x n – f(x n )/f’(x n )
48
Convergence may not be uniform Always use best estimate of root as x n in each cycle If starting values are 1 and -1, what do we expect to happen?
49
Initial x n+1 is not as good as x n, (how do we know this?) Use best estimate of root as x n (what value is this?) Use bad first estimate as x n-1 Did the second attempt work? How do we avoid this type of convergence?
50
Graphical solution
51
Use pseudo code from text p113 to write your secant function [r,fr,logfc]=secant(fh,a,b); Initialize steps, fcmin, and fc a is not the best estimate of the root Interchange a and b Use a while loop = x/ f Store the current best estimate in b and fb Get a new best estimate Calculate fractional change Increment steps and save fc
52
In-class work: Write MatLab code for secant method following pseudo code in text p113 Test by find real root of x 5 + x 3 + 3 between -1.5 and -1 Did you get a value of -1.1053? How many cycles were required?
53
Assignment 4, Due 1/28/16 Write secant function that finds root and saves convergence data Use code to find zero f(x) = e x - 3x 2 in the interval [0.5, 1.5]. On a semi-log plot, compare the rate of convergence of the secant method to that of the bisection method with the same starting values and to Newton’s method with initial guesses 0.5 and 1.5. Hand in a copy of your secant function, the script to get and plot the convergence data, and the plot of the data.
55
Note similarity to Newton’s method (slide 20) n+1 = [ ] n 2
56
Suggested problems from text (6 th edition) Chapter 3.1 p85 Problems 1, 4, 9, 10 Computer problems 1, 2, 4, 7 Chapter 3.2 p101 Problems 4, 15, 17, 19 Computer problems 1, 2, 4, 8, 9, 14 Chapter 3.3 p119 Problems 2, 3, 5 Computer problems 1, 3, 5, 6, 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.