Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Finding zeros (also called roots) of a function Overview: Define the problem Methods of solution Graphical Newton’s Bisection Secant."— Presentation transcript:

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

4 Find intersections of e x and 3x 2 graphically Graph f(x) = e x – 3x 2 Read approximate zeros of f(x) from graph

5 How do I create this graph? Open the editor Type commands Copy and paste into command window or Press RUN (which also save the current version)

6 Many ways to produce this graph Easy: fplot(‘exp(x)-3*x^2’,[-1,4]); grid on

7 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 x 2 ?

8 Find the roots of x 3 + 3x - 1 graphically Let me know when you have an answer.

9 Big picture: how many real roots?

10 Focus on the single real root

11 Newton’s Method

12 Non-linear problem reduced to sequence of linear problems How do we get this result?

13 Generalize to x n and x n+1

14 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 x 3 + 3x – 1.

15 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.

16 In format long, find the intersection of e x and 3x 2 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

17 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?

18 Assignment 1, Due 9/22/15 f(x) = exp(x) - 3x 2 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  n = |(x n – x n-1 )/x n | On a semi-log plot, compare the rates of convergence of the 3 methods.

19 Y axis is log10(re) -5 corresponds to re = 10 -5 Convergence of Newton’s method X axis is number of iterations X 0 =1.5 X 0 =0.5 You must write a script to produce this type of result Convergence to root of e x – 3x 2 in [0.5,1.5] with different x 0 What is the value of the root?

20 Develop a code to compare convergence of Newton’s method Task 1: modify newtfun to output convergence data Save as a different version of Newton’s method Task 2: write script to get the required results Define functions needed by newtfunV2 Call newtfunV2 to produce data for graph Put multiple curves on the same set of axes

21 Modified Newton’s method for HW1 Note alternative to “inline” Note suppressed output of logre

22 Driver for graph to compare convergence of Newton’s method

23 Derive expected convergence rate of Newton’s method

24 Convergence problems with Newton’s method Example: 3.2-19 text p102 6 th edition

25 Roots with multiplicity greater than one

26 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; 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 Not a good choice of starting values Result will depend on which test f(c)f(a)<0 or f(c)f(b)<0 is conducted first Only one root should be between starting values

27 Change Newton’s method code into a code for Bisection method Note alternative to “inline” Note suppressed output of logre

28

29 Test Bisection f(x)=x 5 + x 3 +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 -1 Call to bisection function with a=-1.5 and b=-1 finds zero at -1.1053

30 Test Bisection again f(x)=x 5 + x 3 +3 With format long compare bisection result with Newton’s method for the root between -1.5 and -1

31 Modify to get results for HW1 from Bisection method

32 Y axis is log10(re) -5 corresponds to re = 10 -5 Have you produced this graph?

33 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?

34 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 x 3 -x 2 -2x+1.

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

36

37

38 Y axis is log10(re) -5 corresponds to re = 10 -5 Convergence of bisection method is linear on log(y) vs x plot x 3 -x 2 -2x+1

39 Newton’s method without derivatives

40 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?

41 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?

42 Graphical solution

43 MatLab compared to pseudo code from text p113

44

45 Note similarity to Newton’s method (slide 17)  n+1 = [ ]  n 2

46 Modify to get results from Secant method Include comparison of values of the root

47 Secant, axis labels, and legend added Bisection result differs from Newton and secant

48

49 Josh’s HW1

50


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

Similar presentations


Ads by Google