0% found this document useful (0 votes)
29 views30 pages

Matlab Class 07

The document outlines various numerical methods for finding roots of functions, including the Bisection Method, Newton-Raphson Method, Secant Method, Fixed Point Iteration Method, and False Position Method. It provides examples and step-by-step procedures for each method, illustrating how to apply them to specific equations with defined initial guesses and tolerances. The document serves as a guide for students in a computer programming and applications course focused on numerical analysis techniques.

Uploaded by

Jose Aguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views30 pages

Matlab Class 07

The document outlines various numerical methods for finding roots of functions, including the Bisection Method, Newton-Raphson Method, Secant Method, Fixed Point Iteration Method, and False Position Method. It provides examples and step-by-step procedures for each method, illustrating how to apply them to specific equations with defined initial guesses and tolerances. The document serves as a guide for students in a computer programming and applications course focused on numerical analysis techniques.

Uploaded by

Jose Aguirre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

ME 202- Computer Programming and Applications

Class 07
Class 07

Root Findings
(a) Newton-Raphson Method
(b) Bisection Method
(c) Secant Method
(d) Fixed Point Iteration Method
(e) False Position Method

Louay Alroomi, Ph.D.


Assistant Professor
Department of Mechanical Engineering
San Diego State University
Email: [email protected]
Bisection method
Goal: Find the root of a function f.
What value of x makes f(x) = 0? (Where does f(x) cross the x-axis?)
Initial Guess: Between what two values of x does f(x) cross the x-axis?
When you get a negative product of f(x) values, it means f(x) crosses the x-axis.
f(xLeft) f(xRight) < 0

-1 2
Bisection method
• Now we will take the average of and and call it
• Between what two values do you get a negative product of y-values?
• and
Bisection method
• Replace with and leave the same
Bisection method
• Take the average of and again and call it
• Between what two values do you get a negative product of y-values?
• and
Bisection method
• Replace with and leave the same
• you get a negative product of y-values

• When to stop?
- Define a tolerance (how close needs to be to the root, 0)
Ex:
Initial Guess: [xLeft xRight] where f(xLeft) f(xRight) < 0

-1 2
Bisection method

-1 0.5 2 0.5 2

? No

1.25
0.5 2 1.25 2
1.625

? No ? Yes  (approximated root)


Ex:
Solve the following equation using Bisection method with
the initial guesses of [0.5, 2] and the tolerance of 0.2
𝟐
( )
𝒇 𝒙 =𝒙 +𝟐 𝒙 − 𝟑 Actual Roots = -3, 1
xL f(xL) xR f(xR) f(xL)f(xR) xN f(xN) |f(xN)| < 0.2 ?

Initial Range 0.50 -1.75 2.00 5.00 -8.75

1st xN 1.25 1.06 No


0.50 -1.75 1.25 1.06 -1.86

2nd xN 0.88 -0.48 No


0.88 -0.48 1.25 1.06 -0.51

3rd xN 1.06 0.25 No


0.88 -0.48 1.06 0.25 -0.12

4th xN 0.97 -0.12 Yes


Ex: Solve the following equation using Bisection method with the initial guesses of [0.5, 2] and the tolerance of 0.2.
𝟐
𝒇 ( 𝒙 ) =𝒙 +𝟐 𝒙 − 𝟑 Actual Roots = -3, 1
Command tool:
function apprRoot = myBisection(f, x1, x2,
tol)
f = @(x) x^2 + 2*x -3;
x1 = 0.5; Function apprRoot = myBisection(func, x1, x2, tol)
x2 = 2; % func = function’s handle
tol = 0.2; % x1 = initial guess xleft, x2 = initial guess xright, tol = toler
if f(x1)*f(x2) >= 0
output("wrong range\n"); >> func = @(x) x^2 + 2*x -3;
return; >> myFalsePosition(func, 0.5, 2, 0.2)
end ans =
xnew = (x1 + x2) /2; %calculate 0.9688
bisection point
fprintf("x1 = %0.2f, x2 = %0.2f, xnew =
%0.2f, y(xnew) = %0.2f\n", x1, x2, xnew,
f(xnew));
while abs(f(xnew)) > tol %tolerance
if f(x1)*f(xnew) < 0
x2 = xnew;
elseif f(xnew)*f(x2) < 0
x1 = xnew;
end 11
Ex: Find the rood for the following function with the initial guesses of [0, 1] and the tolerance of 0.0001 for 30 iterations:

f = @(x) 2^x-5*x+2
a = 0;
b = 1;
n = 30;
e = 0.0001;
if f(a)*f(b)<0
for i = 1:n
c = (a+b)/2
fprintf('p%d = %.4f\n',i,c);
if abs(c-b)<e || abs(c-a)<e
break
end
if f(a)*f(c)<0
b = c;
elseif f(b)*f(c)<0
a = c;
end
end
else
disp('No root between given brackets')
end
Ex: Use Newton-Raphson method to find the root of the following equation with the initial guess of [0] for 10 iterations:
if err<epsilon %checking the amount of
error at each iteration
close all; break
clear all; end
syms x; x0=y;
f=exp(-x)-x; %Enter the Function here end
g=diff(f); %The Derivative of the Function y = y - rem(y,10^-n); %Displaying upto
n=input('Enter the number of decimal required decimal places
places:'); fprintf('The Root is : %f \n',y);
epsilon = 5*10^-(n+1) fprintf('No. of Iterations : %d\n',i);
x0 = input('Enter the intial
approximation:');
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the
value of function at x0
f0_der=vpa(subs(g,x,x0)); %Calculating
the value of function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
Ex: Use secant Method to estimate the root of the following equation with the initial guess of [0,1] for 10 iterations:
x0=x1;
clc;
x1=y;
close all;
end
clear all;
y = y - rem(y,10^-n); %Displaying upto
syms x;
required decimal places
f=exp(-x)-x; %Enter the Function here
fprintf('The Root is : %f \n',y);
n=input('Enter the number of decimal
fprintf('No. of Iterations : %d\n',i);
places:');
epsilon = 5*10^-(n+1)
x0 = input('Enter the 1st approximation:');
x1 = input('Enter the 2nd approximation:');
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the
value of function at x0
f1=vpa(subs(f,x,x1)); %Calculating the
value of function at x1
y=x1-((x1-x0)/(f1-f0))*f1; %[x0,x1] is the
interval of the root
err=abs(y-x1);
if err<epsilon %checking the amount of
error at each iteration
break
end
Ex: Use secant Method to estimate the root of the equation f(x) = x^2-6 with the initial guess of [2,1]:
function SecantMethod
%SecantMethod example while i <= N
%to approximate solution to x = cos(x), we p = p1 - (q1*((p1 - p0)/(q1 - q0)));
let f(x) = x - cos(x) %actual Secant Method (finds x
axis intercept between two guesses)
error = 0.0001; %predefine some
tolerance. ifabs(p - p1) < error
N = 100; %number of steps %stopping criterion
i = 2; fprintf('Solution is %f \n',
double(p))
p0 = 2; %the two guesses return
p1 = 1; end

syms 'x' i = i + 1;
f(x) = x^2 - 6; %what we are p0 = p1; %update all the
solving for f(x) = 0 new parameters, po, p1, q0, q1
p1 = p;
q0 = f(p0); %two coressponding q0 = q1;
values of the function evaluated at po and q1 = f(p);
p1
q1 = f(p1); end

fprintf('Solution did not converge within


%d iterations \n', N)
Fixed Point Iteration Method
Ex: Use fixed-point iterative method to find the root of the Ex: Use fixed-point iterative method to find the root for the
equation f(x) = cos(x)/1.3 with the initial guess xo = 0.2: equation f(x) = 2^x-5x+2 with the initial guess xo = 0

g = @(x)(2^x+2)/5;
% cos(x) - 1.3 x = 0; rearrange as x =
xo = 0;
cos(x)/1.3
e = 0.0001; %tolerance
x0 = 0.2; % Initial guess
n = 20; % number of iteration
f = @(x) cos(x)/1.3;
for i = 1:n
tol = 10^-10;
x1 = g(xo)
flag = true;
fprintf('x%d = %.4f\n',i,x1)
its = 0;
if abs(x1-xo)<e
while flag == true && its<100
break
x = f(x0);
end
if abs(x-x0)<tol
xo = x1;
flag = false;
end
end
x0 = x;
its = its+1;
end
disp([x, x-f(x), its])
False position method
• Uses triangles to calculate
𝒇 ( 𝒙 𝒍𝒆𝒇𝒕 ) 𝒇 ( 𝒙 𝒓𝒊𝒈𝒉𝒕 )
=
𝒙 𝒏𝒆𝒘 − 𝒙 𝒍𝒆𝒇𝒕 𝒙 𝒏𝒆𝒘 − 𝒙 𝒓𝒊𝒈𝒉𝒕

𝒙 𝒓𝒊𝒈𝒉𝒕 𝒇 ( 𝒙 𝒍𝒆𝒇𝒕 ) − 𝒙 𝒍𝒆𝒇𝒕 𝒇 ( 𝒙 𝒓𝒊𝒈𝒉𝒕 )


𝒙 𝒏𝒆𝒘 =
𝒇 ( 𝒙 𝒍𝒆𝒇𝒕 ) − 𝒇 ( 𝒙 𝒓𝒊𝒈𝒉𝒕 )
Ex:
Solve the following equation using False Position method
with the initial guesses of [0.5, 2]𝑥and 𝑓the tolerance
(𝑥 )−𝑥 𝑓 (𝑥
of 0.2.
) 𝑟𝑖𝑔 h 𝑡 𝑙𝑒𝑓𝑡 𝑙𝑒𝑓𝑡 𝑟𝑖𝑔 h 𝑡
𝟐 𝑥𝑛𝑒𝑤 =
𝒇 ( 𝒙 ) =𝒙 +𝟐 𝒙 − 𝟑 𝑓 ( 𝑥 𝑙𝑒𝑓𝑡 ) − 𝑓 ( 𝑥 𝑟𝑖𝑔 h 𝑡 )

xL f(xL) xR f(xR) f(xL)f(xR) xN f(xN) F(xN) < 0.2 ?

Initial Range 0.50 -1.75 2.00 5.00 -8.75

1st xN 0.89 -0.43 No


0.89 -0.43 2.00 5.00 -2.16

2nd xN 0.98 -0.09 Yes


Ex:
Solve the following equation using False Position method with the
with the initial guesses of [-5, 5] and tolerance of 0.2.
𝟐
𝑥 𝑟𝑖𝑔 h 𝑡 𝑓 ( 𝑥𝑙𝑒𝑓𝑡 ) − 𝑥 𝑙𝑒𝑓𝑡 𝑓 (𝑥 𝑟𝑖𝑔 h 𝑡 )
𝒇 ( 𝒙 ) =𝒙 +𝟐 𝒙 − 𝟑 𝑥𝑛𝑒𝑤 =
𝑓 ( 𝑥 𝑙𝑒𝑓𝑡 ) − 𝑓 ( 𝑥 𝑟𝑖𝑔 h 𝑡 )

xL f(xL) xR f(xR) f(xL)f(xR) xN f(xN) F(xN) < 0.2 ?


Initial Range -5.00 12.00 5.00 32.00 384.00

Modified Range -2.00 -3.00 2.00 5.00 -15.00

1st xN -0.50 -3.75 No


-0.50 -3.75 2.00 5.00 -18.75

2nd xN 0.57 -1.53 No


0.57 -1.53 2.00 5.00 -7.65

3rd xN 0.91 -0.37 No


0.91 -0.37 2.00 5.00 -1.76

4th xN 0.98 -0.07 Yes


fzero function

Example:
g = @(x) cos(x)
fzero(g,2) % finds a zero near 2
fzero(g,-2) % finds a zero near -2
Ex: Use False Position Method to find the root for the function f(x) = x^2+2*x-3 with the initial guess [0.5,2] and tolerance 0.2:
Command Tool:
Function apprRoot = myFalsePosition(func, x1, x2, tol)
% func = function’s handle
% x1 = initial guess xleft, x2 = initial guess xright, tol = tolerance

Example run:
>> func = @(x) x^2 + 2*x -3;
>> myFalsePosition(func, 0.5, 2, 0.2)
function apprRoot = myFalsePosition(f, x1, x2, tol) ans =
if f(x1)*f(x2) >= 0
output("wrong range\n"); 0.9773
return;
end
xnew = (x1 + x2) /2; %calculate bisection point
fprintf("x1 = %0.2f, x2 = %0.2f, xnew = %0.2f, y(xnew) = %0.2f\n", x1, x2, xnew, f(xnew));
while abs(f(xnew)) > tol %tolerance
if f(x1)*f(xnew) < 0
x2 = xnew;
elseif f(xnew)*f(x2) < 0
x1 = xnew;
end
xnew = (x1 + x2) / 2;
fprintf("x1 = %0.2f, x2 = %0.2f, xnew = %0.2f, y(xnew) = %0.2f\n", x1, x2, xnew, f(xnew));
end
apprRoot = xnew;
end
Another way to describe False Position Method:

clc
x0 = 0.5;
x1 = 2;
tolerance=0.2;
f =@(x) x^2 + 2*x -3;
for i=0:inf
x2= x1 - (f(x1)*
(x1-x0)/(f(x1)-f(x0)))
c = f(x2)
absolute_c= abs(c);
if absolute_c < tolerance
break
end
if f(x0)*c <0
x1=x2;
continue
else
x0=x2;
continue
end
end
roots function

Example:

r = roots([1 2 -3])
Ex:
Ex:

Ex:

Ex:
Ex:

You might also like