finding zeros (also called roots) of a function overview: define the problem methods of solution...

50
Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Upload: erica-smith

Post on 21-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Finding zeros (also called roots) of a functionOverview:

Define the problemMethods of solution

GraphicalNewton’sBisectionSecant

Page 2: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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

Page 3: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Graphical solutions

Page 4: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Find intersections of ex and 3x2 graphicallyGraph f(x) = ex – 3x2

Read approximate zeros of f(x) from graph

Page 5: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

How do I create this graph?

Open the editorType commandsCopy and paste into command window orPress RUN (which also save the current version)

Page 6: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Many ways to produce this graph

Easy:fplot(‘exp(x)-3*x^2’,[-1,4]);grid on

Page 7: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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?

Page 8: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Find the roots of x3 + 3x - 1 graphically

Let me know when you have an answer.

Page 9: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Big picture: how many real roots?

Page 10: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Focus on the single real root

Page 11: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Newton’s Method

Page 12: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Non-linear problem reduced to sequence of linear problems

How do we get this result?

Page 13: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Generalize to xn and xn+1

Page 14: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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.

Page 15: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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.

Page 16: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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

Page 17: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Find intersections of ex and 3x2 by Newton’s method.How do you get Newton’s method to converge to a particular root?

Page 18: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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.

Page 19: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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?

Page 20: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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

Page 21: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Modified Newton’s method for HW1

Note alternative to “inline”

Note suppressed output of logre

Page 22: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Driver for graph to compare convergence of Newton’s method

Page 23: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Derive expected convergence rate of Newton’s method

Page 24: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Convergence problems with Newton’s method

Example: 3.2-19 text p102 6th edition

Page 25: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Roots with multiplicity greater than one

Page 26: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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

Page 27: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Change Newton’s method code into a code for Bisection method

Note alternative to “inline”

Note suppressed output of logre

Page 28: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant
Page 29: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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

Page 30: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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

Page 31: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Modify to get results for HW1 from Bisection method

Page 32: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Y axis is log10(re)-5 corresponds to re = 10-5

Have you produced this graph?

Page 33: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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?

Page 34: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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.

Page 35: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Graphical estimates of real roots of x3-x2-2x+1>>myf=inline(‘x^3-x^2-2*x+1’);>>graphical(myf,-2,2);

Page 36: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant
Page 37: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant
Page 38: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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

Page 39: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Newton’s method without derivatives

Page 40: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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?

Page 41: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

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?

Page 42: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Graphical solution

Page 43: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

MatLab compared to pseudo code from text p113

Page 44: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant
Page 45: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Note similarity to Newton’s method (slide 17)en+1 = [ ] en

2

Page 46: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Modify to get results from Secant method

Include comparison of values of the root

Page 47: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Secant, axis labels, and legend added

Bisection result differs from Newton and secant

Page 48: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant
Page 49: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant

Josh’s HW1

Page 50: Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant