You are on page 1of 30

LAB 4 :Using MATLAB for root

location
CES 513- ONLINE LAB LEARNING SESSION
Week 5 (13/4/2020 – 17/4/2020)
(SYAHRUL FITHRY BIN SENIN)
SENIOR LECTURER
FACULTY OF CIVIL ENGINEERING
UiTM PULAU PINANG
10/4/2020 Prepared by SFS 2020 1
Lesson Learning Outcomes
• to write correct and runnable MATLAB script to program the Bisection
Method algorithm (CO2:PO5)
• to write correct and runnable MATLAB script to program the Newton-
Raphson Method (CO2:PO5)
• to plot the graph of equation for estimation of nonlinear equation
solution (CO2:PO5)

10/4/2020 Prepared by SFS 2020 2


Introduction
• In civil engineering, a lot of
problems requires engineers to
seek root solution from non-linear
equations.

• Example1

• Open channel dimension


determination

10/4/2020 Prepared by SFS 2020 3


Introduction …..

The dimension of the open channel can be determined by solving equation Q above and using non-linear
equation root solving
10/4/2020 Prepared by SFS 2020 4
Example 2
• To solve the following nonlinear equation of bending moment, M(x),
of a cantilever beam under triangle load,
𝟓𝟓
𝑴𝑴 𝒙𝒙 = 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏 − 𝒙𝒙𝟑𝟑
𝟔𝟔

What is the value of distance, x, that will give the bending moment to be 220 lb-ft?

10/4/2020 Prepared by SFS 2020 5


What is the interested parameter here?
• Let x to be the variable of interest.

• The objective is to find the value of x (root) which satisfies the


following equation M(x) = 220 or

M(x) – 220 = 0

𝟓𝟓 𝟑𝟑
𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏 − 𝒙𝒙 − 𝟐𝟐𝟐𝟐𝟐𝟐 = 𝟎𝟎
𝟔𝟔
10/4/2020 Prepared by SFS 2020 6
𝟓𝟓 𝟑𝟑
𝒇𝒇 𝒙𝒙 = 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏 − 𝒙𝒙 − 𝟐𝟐𝟐𝟐𝟐𝟐
Strategy INITIAL GUESS
𝟔𝟔

xl Xu F(xl) F(XU)
0 1 +VE
0 1.5 +ve
0 2 -ve

10/4/2020 Prepared by SFS 2020 7


X
X f(x)

10/4/2020 Prepared by SFS 2020 8


f(x)

10/4/2020 Prepared by SFS 2020 9


f(x)

10/4/2020 Prepared by SFS 2020 10


f(x)

10/4/2020 Prepared by SFS 2020 11


f(x)

10/4/2020 Prepared by SFS 2020 12


f(x)

10/4/2020 Prepared by SFS 2020 13


Bisection Method Algorithm:
• Start
• Read x1, x2, e
*Here x1 and x2 are initial guesses
e is the absolute error i.e. the desired degree of accuracy
• Compute: f1 = f(x1) and f2 = f(x2)
• If (f1*f2) > 0, then display initial guesses are wrong and STOP the program.
Otherwise continue.
• x = (x1 + x2)/2 ---------------STEP 1

• If ( [ (xnew – xold)/xnew ] < e ), then display x and STOP the program


* Here [ ] refers to the modulus sign. *
• Else, f = f(x)
• If ((f*f1) > 0), then x1 = x and f1 = f.
• Else, x2 = x and f2 = f.
• Goto STEP 1

*Now the loop continues with new values.*
• Stop AND DISPLAY THE x values and its iteration number
10/4/2020 Prepared by SFS 2020 14
Bisection Method Using Octave/MATLAB

10/4/2020 Prepared by SFS 2020 15


clear all
clc

f = @(x) 120*x –(5/6)*x^3-220;

ea = 100;
es = 1;

xL = 0;
xU = 2;
% xL = the lower limit');
% xU = the upper limit');

sign1 = f(xL)*f(xU);

10/4/2020 Prepared by SFS 2020 16


if sign1 < 0
disp(' ')
disp(' There is root within this bracket')
else
disp(' ')
disp(' There is no root within this bracket')

end
disp(' ')
disp( ' iter xL xU xm ea')
disp( '________________________________________________')
disp(' ‘)

iter = 0;
xm =10/4/2020
0.5*(xL+xU); Prepared by SFS 2020 17
while ea > es

iter = iter +1;

sign2 = f(xL)*f(xm);

if sign2 < 0

xmold = xm;
xU = xm;
xL = xL;

xmnew = 0.5*(xL+ xU);

10/4/2020 Prepared by SFS 2020 18


if iter== 1
ea = ea;
else
ea = abs((xmnew-xmold)/xmold)*100;
end

% disp( [iter xL xU xm ea])

elseif sign2 > 0


xmold = xm;
xL = xm;
xU = xU;
xmnew = 0.5*(xL+ xU);

10/4/2020 Prepared by SFS 2020 19


if iter == 1

ea = ea;
else
ea = abs((xmnew-xmold)/xmold)*100;
end

end

fprintf(‘%i %3f %3f %3f %3f \n’, iter, xL, xU,xm, ea)
end

10/4/2020 Prepared by SFS 2020 20


disp (' ')

fprintf('The iteration no is %i and the root is = %5f \n', iter, xm)

10/4/2020 Prepared by SFS 2020 21


Newton-Raphson Method

10/4/2020 Prepared by SFS 2020 22


Introduction
• Starting from initial guess x1, the Newton Raphson method uses
below formula to find next value of x, i.e., xn+1 from previous value xn.

10/4/2020 Prepared by SFS 2020 23


10/4/2020 Prepared by SFS 2020 24
Newton-Raphson Method Algorithm:
• Start
• Read initial guess value, x0 and % specified relative error, es
• Compute: dy/dx of the non-linear function, f’(xn)
• Starting from iteration n= 0, compute the first new guess value of root, x1
using the formula given in introduction section
Eqn (1)

• Compute the absolute relative error, ea, using the following equation
ea= (xn+1 – xn)/xn+1 x 100

• If ea < es, estimate the new guess root, x2 from Eqn (1) and repeat this
process until ea < es
• Stop AND DISPLAY THE root values and its iteration number
10/4/2020 Prepared by SFS 2020 25
Newton-Raphson Method Using Octave/MATLAB

10/4/2020 Prepared by SFS 2020 26


clc
clear all

x0 =10;
es = 1;
ea = 100;

x = x0;

df =@(x) 120 - 15/6*x^2;


f =@(x) 120*x -5/6*x^3-220;

iter = 0;

disp( 'iter x Ea')


disp('_______________')
disp(' ')

10/4/2020 Prepared by SFS 2020 27


while ea > es
iter = iter+1;
xold =x;
x = x -(f(x)/df(x));
xnew =x;
if iter == 1
ea = ea;
else
ea = abs((xnew-xold)/xnew)*100;
endif

fprintf('%i %3f %3f \n', iter, x, ea)


end

disp(' ')
fprintf('The root of this equation is %5f by iteration of %i with error of %3f \n',x,iter,ea)

10/4/2020 Prepared by SFS 2020 28


10/4/2020 Prepared by SFS 2020 29
Ungraded self-Learning Activities (2 hours max)
• Bisection (1 hour 15 minits) and Newton-Raphson ( 45 minutes)
Using Octave, solve Problem 6.20 using the Bisection and Newton-Raphson Method. Compare the
solution of both methods in terms of its absolute error and no of iteration required. You may to
assume the lower guess and upper guess value of the root.

10/4/2020 Prepared by SFS 2020 30

You might also like