Matlab Class 07
Matlab Class 07
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
-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
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
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
𝒇 ( 𝒙 𝒍𝒆𝒇𝒕 ) 𝒇 ( 𝒙 𝒓𝒊𝒈𝒉𝒕 )
=
𝒙 𝒏𝒆𝒘 − 𝒙 𝒍𝒆𝒇𝒕 𝒙 𝒏𝒆𝒘 − 𝒙 𝒓𝒊𝒈𝒉𝒕
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: