Sei sulla pagina 1di 4

MATLAB’s fzero Function

• MATLAB’s fzero provides the best qualities of both bracketing methods and
open methods.
– Using an initial guess:
xc = fzero(function, x0)
[xc, fxc] = fzero(function, x0)
• function is a function handle to the function being evaluated
• x0 is the initial guess
• xc is the computed root
• fxc is the function evaluated at that root
– Using an initial bracket:
xc = fzero(function, [x0 x1])
[xc, fxc] = fzero(function, [x0 x1])
• As above, except x0 and x1 are guesses that must bracket a
sign change
1

fzero Options

• Options may be passed to fzero as a third input argument - the options are
a data structure created by the optimset command

• options = optimset(‘par1’, val1, ‘par2’, val2,…)

– parn is the name of the parameter to be set

– valn is the value to which to set that parameter

– The parameters commonly used with fzero are:

• display: when set to ‘iter’ displays a detailed record of all the


iterations
• tolx: A positive scalar that sets a termination tolerance on x.

• options = optimset(‘display’, ‘iter’);


– Sets options to display each iteration of root finding process
2
fzero Example
f=inline('x^3+x-1');
option=optimset('display','iter');
[xc]=fzero(f,-0.7,option);
Results:
Search for an interval around -0.7 containing a sign change:
Func-count a f(a) b f(b) Procedure
1 -0.7 -2.043 -0.7 -2.043 initial interval
3 -0.680201 -1.99491 -0.719799 -2.09273 search
5 -0.672 -1.97546 -0.728 -2.11383 search
7 -0.660402 -1.94842 -0.739598 -2.14416 search
9 -0.644 -1.91109 -0.756 -2.18808 search
11 -0.620804 -1.86006 -0.779196 -2.25228 search
13 -0.588 -1.7913 -0.812 -2.34739 search
15 -0.541608 -1.70048 -0.858392 -2.49089 search
17 -0.476 -1.58385 -0.924 -2.71289 search
19 -0.383216 -1.43949 -1.01678 -3.06799 search
21 -0.252 -1.268 -1.148 -3.66095 search
23 -0.0664323 -1.06673 -1.33357 -4.70519 search
25 0.196 -0.79647 -1.596 -6.66136 search
27 0.567135 -0.25045 -1.96714 -10.5792 search
28 1.092 1.39417 -1.96714 -10.5792 search 3

fzero Example
Search for a zero in the interval [1.092, -1.9671]:

Func-count x f(x) Procedure


28 1.092 1.39417 initial
29 0.735797 0.134154 interpolation
30 0.698346 0.0389213 interpolation
31 0.68311 0.0018759 interpolation
32 0.68234 2.81398e-005 interpolation
33 0.682328 2.07838e-008 interpolation
34 0.682328 2.30482e-013 interpolation
35 0.682328 0 interpolation
Zero found in the interval [1.092, -1.96714]

xc = 6.823278038280194e-001
Our Newton’s code
Total Function evaluation=35 Total Function evaluation=14
Total no of iteration=23 Total no of iteration=7
4
fzero Example
f=inline('x^3+x-1');
option=optimset('display','iter');
[xc]=fzero(f,[0 1],option);
Func-count x f(x) Procedure
2 1 1 initial
3 0.5 -0.375 bisection
4 0.636364 -0.105935 interpolation
5 0.68491 0.00620153 interpolation
6 0.682225 -0.000246683 interpolation
7 0.682328 -5.43508e-007 interpolation
8 0.682328 1.50102e-013 interpolation
9 0.682328 0 interpolation
Zero found in the interval [0, 1]

xc = 6.823278038280194e-001
Our Bisection code
Total Function evaluation=9 Total Function evaluation=12
Total no of iteration=8 Total no of iteration=10
5

MATLAB’s roots function


• MATLAB has a built in function called roots to determine all the roots of a
polynomial - including imaginary and complex ones.

• f(x) is a polynomial of the form

f(x)=c1xn + c2xn-1 + c3xn-2 +….cnx + cn+1

• Its roots can more easily found by using r = roots(c)


– r is a column vector containing the roots
– c is a row vector containing the polynomial coefficients
c=[c1,c2,c3,…,cn+1]
• Example:
 Find the roots of
f(x)=x5-3.5x4+2.75x3+2.125x2-3.875x+1.25

 r = roots([1 -3.5 2.75 2.125 -3.875 1.25]);


6
MATLAB’s roots function

MATLAB reports all 5 roots,


r=
2.000000000000004e+000
-1.000000000000001e+000
9.999999999999987e-001 +5.000000000000002e-001i
9.999999999999987e-001 -5.000000000000002e-001i

5.000000000000000e-001

MATLAB’s function for Polynomials

• MATLAB’s poly function can be used to determine polynomial coefficients if


roots are given:

 b = poly([0.5 -1])
• Finds f(x) where f(x) =0 for x=0.5 and x=-1
• MATLAB reports b = [1.000 0.5000 -0.5000]
• This corresponds to f(x)=x2+0.5x-0.5

• MATLAB’s polyval function can evaluate a polynomial at one or more points:


 Consider a polynomial
f(x)=x5-3.5x4+2.75x3+2.125x2-3.875x+1.25
• a = [1 -3.5 2.75 2.125 -3.875 1.25];

 f=polyval(a, 1)
• This calculates f(1), which MATLAB reports as f=-0.2500
8

Potrebbero piacerti anche