Finding zeros (also called roots) of a functionOverview:
Define the problemMethods of solution
GraphicalNewton’sBisectionSecant
For a function f(x) of a single independent variable, find all x0 such that f(x0)=0.(Some methods can be generalized to multiple independent variables)
Other ways that the problem can be stated:find all x where graphs of f1(x) and f2(x) intersectfind all extrema of f(x) on [a,b]
Define the problem
Graphical solutions
Find intersections of ex and 3x2 graphicallyGraph f(x) = ex – 3x2
Read approximate zeros of f(x) from graph
How do I create this graph?
Open the editorType commandsCopy and paste into command window orPress RUN (which also save the current version)
Many ways to produce this graph
Easy:fplot(‘exp(x)-3*x^2’,[-1,4]);grid on
Harder ways to produce this graph
myf=inline(‘exp(x)-3*x.^2’);x=linspace(-1,4,50);plot(x,myf(x));grid on
Why x.^2 for x2?
Find the roots of x3 + 3x - 1 graphically
Let me know when you have an answer.
Big picture: how many real roots?
Focus on the single real root
Newton’s Method
Non-linear problem reduced to sequence of linear problems
How do we get this result?
Generalize to xn and xn+1
From “Essentials of MATLAB” p150
Use fh(x) in place of feval(fh,x)
MatLab code for Newton’s method
Use the code to improve graphical solution to root of x3 + 3x – 1.
When you are in the directory where newtfun has been saved,you can work in the command window.
I recommend that you work in the editor. Write and save a script that you can modify for use on tests.
In format long, find the intersection of ex and 3x2 near 3.5 by Newton’s method
Write a script, copy script into command window and run
Copy newtfun function into command window and print
Sign printout (one page) and hand in
Another dry-run quiz
Find intersections of ex and 3x2 by Newton’s method.How do you get Newton’s method to converge to a particular root?
Assignment 1, Due 9/22/15 f(x) = exp(x) - 3x2 has a zero in the interval [0.5, 1.5]. Write MATLAB codes to find this zero by the Bisection, Newton, and Secant methods.
Use format long to compare your results to the value returned by MATLAB’s fzero function.
Define convergence by Dn = |(xn – xn-1)/xn| On a semi-log plot, compare the rates of convergence of the 3 methods.
Y axis is log10(re)-5 corresponds to re = 10-5
Convergence of Newton’s method
X axis is number of iterations
X0=1.5
X0=0.5
You must write a script to produce this type of result
Convergence to root of ex – 3x2 in [0.5,1.5] with different x0 What is the value of the root?
Develop a code to compare convergence of Newton’s method
Task 1: modify newtfun to output convergence dataSave as a different version of Newton’s method
Task 2: write script to get the required resultsDefine functions needed by newtfunV2Call newtfunV2 to produce data for graphPut multiple curves on the same set of axes
Modified Newton’s method for HW1
Note alternative to “inline”
Note suppressed output of logre
Driver for graph to compare convergence of Newton’s method
Derive expected convergence rate of Newton’s method
Convergence problems with Newton’s method
Example: 3.2-19 text p102 6th edition
Roots with multiplicity greater than one
Implies zero between a and b
Bisection method for continuous functions
Let c = (a+b)/2
If f(c)=0 then c is a root; stopIf f(c)f(a)<0 then root is in [a,c] set b=c and repeatIf f(c)f(b)<0 then root is in [c,b] set a=c and repeat
Not a good choice of starting valuesResult will depend on which test f(c)f(a)<0 or f(c)f(b)<0 is conducted firstOnly one root should be between starting values
Change Newton’s method code into a code for Bisection method
Note alternative to “inline”
Note suppressed output of logre
Test Bisectionf(x)=x5 + x3 +3 has no zero on [-1,1]Call to bisection function with a=-1 and b=1 generates warning message and does not converge to a zero of f(x)Zero is between -1.5 and -1Call to bisection function with a=-1.5 and b=-1 finds zero at -1.1053
Test Bisection againf(x)=x5 + x3 +3 With format long compare bisection result with Newton’s method for the root between -1.5 and -1
Modify to get results for HW1 from Bisection method
Y axis is log10(re)-5 corresponds to re = 10-5
Have you produced this graph?
Housekeeping
Create a folder with MatLab codes used to find zeros.
Functions: newtfun, newtfunV2, bisection, secant, etc.
Scrips: quiz1, quiz2, HW1, etc.
How do I navigate between folders in MatLab?
Functions without a named output
Navigate to your find-zeros folder
Use function macro to write “graphical”, a function that produces a graph of f(x) between xa and xb using fplot
Use graphical for estimate all the real roots of x3-x2-2x+1.
Graphical estimates of real roots of x3-x2-2x+1>>myf=inline(‘x^3-x^2-2*x+1’);>>graphical(myf,-2,2);
Y axis is log10(re)-5 corresponds to re = 10-5
Convergence of bisection method is linear on log(y) vs x plot
x3-x2-2x+1
Newton’s method without derivatives
Convergence may not be uniformAlways use best estimate of root as xn in each cycle
If starting values are 1 and -1,what do we expect to happen?
Initial xn+1 is not as good as xn, (how do we know this?)Use best estimate of root as xn (what value is this?)Use bad first estimate as xn-1
Did the second attempt work?
How do we avoid this type of convergence?
Graphical solution
MatLab compared to pseudo code from text p113
Note similarity to Newton’s method (slide 17)en+1 = [ ] en
2
Modify to get results from Secant method
Include comparison of values of the root
Secant, axis labels, and legend added
Bisection result differs from Newton and secant
Josh’s HW1