ce206_roots of equations

18
CE 206: Engineering CE 206: Engineering Computation Sessional Finding Roots of Equations

Upload: tonmoyahmed06

Post on 03-Nov-2014

24 views

Category:

Documents


0 download

DESCRIPTION

numerical

TRANSCRIPT

Page 1: CE206_Roots of Equations

CE 206: Engineering CE 206: Engineering Computation Sessionalp

Finding Roots of Equations

Page 2: CE206_Roots of Equations

Solution Methods Overview

•Bisection/Half-interval Search•Method of false position/Regula Falsi

Bracketing Methods•Method of false position/Regula Falsi

•Secant Method•Newton Raphson

Methods

Newton Raphson•Iteration Method•Many more….

Open Methods

Many more….

Al b i ti 02 bAlgebraic equation: Transcendental equation:

02 =++ cbxax05cos1 =−+ xx

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Page 3: CE206_Roots of Equations

Bracketing Methods: Bisection

%100 new

oldnewa x

xx −=ε

Location of the root lies within the interval where the sign change occurs

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

change occursThe absolute error is reduced by a factor of 2 for each iteration.

Page 4: CE206_Roots of Equations

MATLAB code: bisection methodfunction [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit)

% [root,fx,ea,iter]=bisect('func',xl,xu,es,maxit):% uses bisection method to find the root of func% input:% func = name of function% xl, xu = lower and upper guesses% es = desired relative error (default = 0.0001) Comment ( )% maxit = maximum allowable iterations (default = 50)% output:% root = real root% f f ti l t t

Commentlines

% fx = function value at root% ea = approximate relative error (%)% iter = number of iterations

if nargin<3 error('at least 3 input arguments required') endif nargin<3,error('at least 3 input arguments required'),endtest = feval('func',xl)*feval('func',xu);if test>0,error(‘roots cannot be found'),endif nargin<4|isempty(es), es=0.0001;end

Checking whether the roots are within these bounds or not

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

if nargin<5|isempty(maxit), maxit=50;end

Page 5: CE206_Roots of Equations

MATLAB code: bisection method (ctd)

iter = 0; xr = xl; ea = 100;while (1)xrold = xr;

The variables that change their values within the code needs to be initialized

xx −;xr = (xl + xu)/2;iter = iter + 1;if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;endtest feval('func' xl)*feval('func' xr);

%100new

oldnewa x

xx −=εCalculating approx. 

error

test = feval('func',xl)*feval('func',xr);if test < 0xu = xr;

elseif test > 0xl = xr;

elseea = 0;

endendif ea <= es | iter >= maxit,break,end

endroot = xr; fx = feval('func',xr);

Stopping criteria

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Page 6: CE206_Roots of Equations

func.m file definitionfunction [value] = func(x)

value = x^3-10*x^2+5; 510 23 +− xxEvaluating the function

[root,fx,ea,iter]=bisect('func',0.6,0.8)

root =0.7346

Root of the function after 19 iterations

fx =0.4430

ea =5 1929e 005 ea<0 0001 was the stopping criterion5.1929e-005

iter =19

ea<0.0001 was the stopping criterion

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Page 7: CE206_Roots of Equations

Bracketing Methods: Regula Falsithe location of the intercept of the straight line (xr):

xr = xu −f (xu)(xl − xu)f (xl ) − f (xu)

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

Page 8: CE206_Roots of Equations

Newton-Raphson MethodBased on forming the tangent line to the f(x) curve at some guess x, then following the tangent line to where it crosses the x-axis.

0)()(1ii

ii xx

xfxf−

−=′

+

)()(

1i

iii xf

xfxx′

−=+

εa =xi+1 − xi

xi+1

100%

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

xi+1

Page 9: CE206_Roots of Equations

MATLAB code: newtraph.mfunction [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit)

if nargin<3,Derivative of the function (needs to be defined)

error('at least 3 input arguments required')endif nargin<4|isempty(es),es=0.0001;endif i <5|i t ( it) it 50 dif nargin<5|isempty(maxit),maxit=50;enditer = 0;while (1)xrold = xr;

)()(

1i

iii xf

xfxx′

−=+

xrold xr;xr = xr - feval('func',xr)/feval('dfunc',xr);iter = iter + 1;if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; endif ea <= es | iter >= maxit, break, end

endroot = xr;

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

Page 10: CE206_Roots of Equations

Secant Methodh d i i i d b b k d fi i the derivative approximated by a backward finite

divided difference: f(x)

f( )

f ' (xi) ≅f (xi−1) − f (xi)

xi−1 − xi

f(xi)

i−1 i

xi+1 = xi −f (xi) xi−1 − xi( )f (xi 1) − f (xi)

f(xi-1)

f (xi−1) f (xi)

Note: Requires two initial guesses for x

xxixi-1

Assignment:Can you modify newtraph.m function file and make it

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

y y papplicable for the secant method?

Page 11: CE206_Roots of Equations

MATLAB’s fzero functionMATLAB’s fzero provides the best qualities of both bracketing methods and open methods.

U i i i i l Using an initial guess:x = fzero(function, x0)[x, fx] = fzero(function, x0)

Using an initial bracket:

Location of the root

Evaluated at the root

Function handle to the function being evaluated

Initial guess

Initial guesses that bracket the rootUsing an initial bracket:x = fzero(function, [x0 x1])[x, fx] = fzero(function, [x0 x1])

Initial guesses that bracket the root

Using create or edit fzero options using optimsetoptions = optimset('param1',value1,'param2',value2,...)

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

options optimset( param1 ,value1, param2 ,value2,...)

Page 12: CE206_Roots of Equations

fzero example

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

– Sets options to display each iteration of root finding Sets opt o s to d sp ay eac te at o o oot d g process.

– Other optimset properties: MaxIter, TolX

[x, fx] = fzero(@(x) x^10-1, 0.5, options)U f t fi d t f f( ) 10 1 t ti ith – 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

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

Page 13: CE206_Roots of Equations

MATLAB’s roots function R t f l i lRoots of polynomials:For an nth order equation, there are n real or complex roots.

nf ++++ 2)( nnn xaxaxaaxf ++++= K2

210)(x = roots(c)

x is a column vector containing the rootsx is a column vector containing the rootsc is a row vector containing the polynomial coefficients

Example:Find the roots off(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])

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

Page 14: CE206_Roots of Equations

Exercise 120 ki /ft20 kips/ft

150 kip‐ft

15 kips

Using singularity functions, the shear, bending moment and displacement l h i l d b b d f ll

5 ft 2 ft 1 ft 2 ft

along the simply supported beam can be expressed as follows:57815520020)( 011 −−−−−−= xxxxV

0122 xxxxxxM 577150815510010)( 0122 +−+−+−+−−=

xxxxxxxu 25.2386

5777586

155650

65)( 32244 −+−+−+−+−−=

Using any root location technique, find the points where shear and bending moment equal to zero and where the displacement is maximum.

6666

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

g q pAlso draw the shear force, bending moment and displacement diagrams

Page 15: CE206_Roots of Equations

Exercise 1: solution hints20 ki /ft20 kips/ft

150 kip‐ft

15 kips

57815520020)( 011 −−−−−−= xxxxV

5 ft 2 ft 1 ft 2 ft

)(

function f = V(x)f=20*(sing(x,0,1)-sing(x,5,1))-15*sing(x,8,0)-57;

In addition, the singularity function can be set up asfunction s = sing(x, a, n)if x > a ⎫⎧ >− axwhenax n

n )(s = (x - a) ^ n;

elses = 0;

end

⎭⎬⎫

⎩⎨⎧

≤>

=−axwhenaxwhenax

ax n

0)(

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

Solution: V(x) = 0 at x = 2.85 ft, M(x) =  0 at x = 5.814 ft,U(x) =  0 at x = 3.9357 ft 

Page 16: CE206_Roots of Equations

Exercise 2The saturation concentration of dissolved oxygen in freshwater can be calculated with the equation:

111075 1062194981024381106423086105757011 ××××

Osat = Saturation concentration of DO (mg/L)T T 273 15 h T (°C)

43210621949.8102438.110642308.610575701.134411.139ln

aaaasat TTTT

O ×−

×+

×−

×+−=

Ta = T + 273.15 where T = temperature (°C)

Fill up the following table (use bisection method): express your results for Fill up the following table (use bisection method): express your results for an absolute error of 0.05 °C

Osat (mg/L) Temperature (°C)Osat (mg/L) Temperature ( C)8 ??10 ??12 ??

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

12 ??

Page 17: CE206_Roots of Equations

Exercise 2: solution hintsFirst determine how many iterations you need for the specified absolute error using the formula (see your CE 205 notes):

Bracket range

6439.905.0

40loglog 2,

0

2 =⎟⎠⎞

⎜⎝⎛=⎟

⎟⎠

⎞⎜⎜⎝

⎛ Δ=

daExn

10 iterations should be

Use bisect.m to compute the roots for maximum 10 iterations and also find the real roots using fzero function Compare results

Absolute error 10 iterations should be enough

find the real roots using fzero function. Compare results.

Osat Approximation Exact Error8 26 75781 26 78017 0 0224

Less than 

8 26.75781 26.78017 0.022410 15.35156 15.38821 0.036612 7.46094 7.46519 0.0043

0.05

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

Page 18: CE206_Roots of Equations

Concluding remarksNeed to be judicious in choosing your initial guess (or initial brackets) of the root.

A physical insight into the problem may helpA physical insight into the problem may helpApply common logic

Some examples:- Water level in a spherical tank cannot exceed the diameter- Temperature in natural water bodies usually within a specific p y p

range, (e.g. cannot be negative)- deflection of a beam is maximum where the du/dx is zero- Water level in a tank cannot exceed tank depth (or cannot be

negative)- etc.

Di i A i t #2

CE 206: Engg. Computation Sessional Dr. Tanvir Ahmed

Discussion on Assignment #2