Tutorial Python 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Calculo Numérico I

Raíces de ecuaciones:
Métodos Abiertos

Dr. Luis Sanchez


Objectives
• Recognizing the difference between bracketing and open
methods for root location.
• Understanding the fixed-point iteration method and how
you can evaluate its convergence characteristics.
• Knowing how to solve a roots problem with the Newton-
Raphson method.
• Knowing how to implement both the secant and the
modified secant methods.
• Knowing how to use MATLAB’s fzero function to estimate
roots.
• Learning how to manipulate and determine the roots of
polynomials with MATLAB.
Open Methods
• Open methods differ from bracketing methods, in
that open methods require only a single starting
value or two starting values that do not necessarily
bracket a root.
• Open methods may diverge as the computation
progresses, but when they do converge, they usually
do so much faster than bracketing methods.
Graphical Comparison of Methods

a) Bracketing method
b) Diverging open method
c) Converging open method - note speed!
1. Simple Fixed-Point Iteration
• Rearrange the function f(x)=0 so that x is on the left-
hand side of the equation: x=g(x)
• Use the new function g to predict a new value of x -
that is, xi+1=g(xi)
• The approximate error is given by:

x i1  x i
a  100%
x i1


Example

• Solve f(x)=e-x-x
• Re-write as x=g(x) by isolating x
(example: x=e-x)
• Start with an initial guess (here, 0)
I xi |a| % |t| % |t|i/|t|i-1
0 0.0000 100.000
1 1.0000 100.000 76.322 0.763
2 0.3679 171.828 35.135 0.460
3 0.6922 46.854 22.050 0.628
4 0.5005 38.309 11.755 0.533

• Continue until some tolerance


is reached
Convergence
Convergence of the simple
fixed-point iteration
method requires that the
derivative of g(x) near the
root has a magnitude less
than 1.

a) Convergent, 0≤g’<1
b) Convergent, -
1<g’≤0
c) Divergent, g’>1
d) Divergent, g’<-1
2. Newton-Raphson Method
(Also known as Newton’s Method)

Given an initial guess of the root x0, Newton-Raphson


method uses information about the function and its
derivative at that point to find a better guess of the
root.

Assumptions:
• f(x) is continuous and the first derivative is known
• An initial guess x0 such that f’(x0)≠0 is given
Newton Raphson Method
- Graphical Depiction -
If the initial
guess at the root
is xi, then a
tangent to the
function of xi that
is f’(xi) is
extrapolated
down to the x-
axis to provide
an estimate of the
root at xi+1.
Derivation of Newton’s Method

The Newton-Raphson method can be derived on the basis of this geometrical


interpretation. As the first derivative at x is equivalent to the slope:
Newton’s Method

Given f ( x), f ' ( x), x0


Assumpution f ' ( x0 )  0
______________________
for i  0: n
f ( xi )
xi 1  xi 
f ' ( xi )
end
Example
Find a zero of the function f(x)  x 3  2 x 2  x  3 , x0  4
f ' (x)  3x 2  4 x  1
f ( x0 ) 33
Iteration 1 : x1  x0   4 3
f ' ( x0 ) 33
f ( x1 ) 9
Iteration 2 : x2  x1   3   2.4375
f ' ( x1 ) 16
f ( x2 ) 2.0369
Iteration 3 : x3  x2   2.4375   2.2130
f ' ( x2 ) 9.0742
Example
k (Iteration) xk f(xk) f’(xk) xk+1 |xk+1 –xk|

0 4 33 33 3 1

1 3 9 16 2.4375 0.5625

2 2.4375 2.0369 9.0742 2.2130 0.2245

3 2.2130 0.2564 6.8404 2.1756 0.0384

4 2.1756 0.0065 6.4969 2.1746 0.0010


Example:
Use the Newton-Raphson method to estimate the
root of

the true value of the root: 0.56714329


Example
Implement a .M file to use the Newthon-Raphson method to
determine the mass of the bungee jumper with a drag coefficient of
0.25kg/m to have a velocity of 36m/s after 4 s of free fall. The
acceleration of gravity is 9.81m/s2.
Matlab Code for Newton Raphson Method
function [root,ea,iter] = newtraph(func,dfunc,xr,es,maxit,varargin)
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3
error('at least 3 input arguments required');
end
if nargin<4||isempty(es)
es=0.0001;
end
if nargin<5||isempty(maxit)
maxit = 50;
end
iter = 0;
while (1)
xrold = xr;
xr = xr-func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0
ea = abs((xr-xrold)/xr)*100;
end
if ea <= es | iter >= maxit,
break;
end
end
root = xr;

>> y = @(m) sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;


>> dy=@(m) 1/2*sqrt(9.81/(m*0.25))*tanh((9.81*0.25/m)^(1/2)*4)-
9.81/(2*m)*sech(sqrt(9.81*0.25/m)*4)^2;
>> newtraph(y,dy,140,0.00001)
Drawbacks
Four cases where the Newton-Raphson method exhibits poor
convergence.

a) Inflection point ( f ″ (x) = 0) occurs in the vicinity of a root.


b) The Newton-Raphson technique to oscillate around a local maximum or
minimum.
c) Initial guess that is close to one root can jump to a location several roots away.
d) Solution shoots off horizontally and never hits the x axis.
Secant Methods
• A potential problem in implementing the Newton-
Raphson method is the evaluation of the derivative -
there are certain functions whose derivatives may be
difficult or inconvenient to evaluate.
• For these cases, the derivative can be approximated
by a backward finite divided difference:

f (x i1 )  f (x i )
f (x i ) 
'

x i1  x i


Secant Methods
• Substitution of this approximation for the derivative
to the Newton-Raphson method equation gives:
f (x i )x i1  x i 
x i1  x i 
f (x i1)  f (x i )

• Note - this method requires two initial estimates of x


but does not require an analytical expression of the

derivative.
Secant Method
Assumption s :
Two initial points xi and xi 1
such that f ( xi )  f ( xi 1 )
New estimate (Secant Method) :
( xi  xi 1 )
xi 1  xi  f ( xi )
f ( xi )  f ( xi 1 )
Secant Method - Flowchart

x0 , x1 , i  1

( xi  xi 1 )
xi 1  xi  f ( xi ) ;
f ( xi )  f ( xi 1 )
i  i 1

NO Yes
xi 1  xi   Stop
Modified Secant Method
In this modified Secant method, only one initial guess is needed :
f ( xi   xi )  f ( xi )
f ' ( xi ) 
 xi

f ( xi )  xi f ( xi )
xi 1  xi   xi 
f ( xi   xi )  f ( xi ) f ( xi   xi )  f ( xi )
 xi
Problem : How to select  ?
If not selected properly, the method may diverge .
MATLAB’s fzero Function
• MATLAB’s fzero provides the best qualities of both
bracketing methods and open methods.
• Using an initial guess:

x = fzero(function, x0)
[x, fx] = fzero(function, x0)
• function is a function handle to the function being evaluated
• x0 is the initial guess
• x is the location of the root
• fx is the function evaluated at that root

• Using an initial bracket:

x = fzero(function, [x0 x1])


[x, fx] = fzero(function, [x0 x1])
• As above, except x0 and x1 are guesses that must bracket a sign
change
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.
fzero Example
• options = optimset(‘display’, ‘iter’);
• Sets options to display each iteration of root finding
process
• [x, fx] = fzero(@(x) x^10-1, 0.5, options)

• Uses fzero to find roots of f(x)=x10-1 starting with an


initial guess of x=0.5.

• MATLAB reports x=1, fx=0 after 35 function


counts
Polynomials
• MATLAB has a built in program called roots to
determine all the roots of a polynomial - including
imaginary and complex ones.
• x = roots(c)
• x is a column vector containing the roots
• c is a row vector containing the polynomial coefficients
• Example:
• Find the roots of

f(x)=x5-3.5x4+2.75x3+2.125x2-3.875x+1.25
x = roots([1 -3.5 2.75 2.125 -3.875 1.25])
Polynomials (cont)
• 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:
• a = [1 -3.5 2.75 2.125 -3.875 1.25];
If used as coefficients of a polynomial, this corresponds to f(x)=x5-
3.5x4+2.75x3+2.125x2-3.875x+1.25
• polyval(a, 1)
• This calculates f(1), which MATLAB reports as -0.2500

You might also like