Lab 02 Secant and System of Non-Linear Equations
Lab 02 Secant and System of Non-Linear Equations
Jalozai Campus
Department of Industrial Engineering
Numerical Analysis & Computer Applications Lab
Lab No. : 02
Submitted to: Dr. Iltaf
Submitted by: Umair Ali Shah
Semester: 5th Semester
Registration ID: 18JZIND0105
Submission Date: Dec 13, 2020
Lab No. 02
To Find the Roots of Non-Linear Equation Using Newton’s Method and
Secant Method
Objectives
To write MATLAB codes for Secant Method and Newton’s System of Non-linear
Equations.
Implementation of these methods on engineering case studies using solution of nonlinear
models.
Introduction
In this lab we will develop MATLAB codes to find the roots of non-linear equations using
system of nonlinear equations and secant method. We will then solve different problems using
these codes.
Theory
Secant Method
Secant method is basically Newton's method without explicitly computing the derivative at each
iteration. The secant is faster but may not converge at all. Secant method starts with two initial
approximation xo and x1 (they may not bracket the root) and then calculates the x2 by the same
formula as in Regula-Falsi method but proceeds to the next iteration without bothering about any
root bracketing.
𝑥𝑖−1 − 𝑥𝑖
𝑥𝑖+1 = 𝑓(𝑥𝑖 )
𝑓(𝑥𝑖−1 ) − 𝑓(𝑥𝑖 )
In this lab we are only concerned with bivariate or two variable functions. Our task is to find
values of x and y such that their functions will be
𝑓(𝑥, 𝑦) = 0
𝑔(𝑥, 𝑦) = 0
Let xo, yo are initial approximation and h, k are the quantities need to be determined such that
𝑓(𝑥0 + ℎ, 𝑦0 + 𝑘)
𝑔(𝑥0 + ℎ, 𝑦0 + 𝑘)
Then
−𝑓𝑔𝑦 + 𝑔𝑓𝑦
ℎ=
𝐽
−𝑔𝑓𝑥 + 𝑓𝑔𝑥
𝑘=
𝐽
Where 𝐽 is called Jacobian and is equal to
𝑓 𝑓𝑦
𝐽 = |𝑔𝑥 𝑔𝑦 |
𝑥
Now iteration process starts here and we need to find h, k and 𝐽 for each iteration.
𝑥𝑖+1 = 𝑥𝑖 + ℎ
𝑦𝑖+1 = 𝑦𝑖 + 𝑘
MATLAB Programs
MATLAB Code for Secant Method
% Secant Method by Umair Ali Shah
clear all
clc
% Write function here
syms x
f(x)=x^3+3*x^2-1;
Q=(-3:3);
f(Q);
subplot(1 ,2 ,1); % Multiple plots together
plot(Q, f(Q), '-k', Q, f(Q), '.r'); % Plot to get rough idea of the interval
line([0 0], ylim); %y-axis
line(xlim, [0 0]); %x-axis
title('Function Plot')
xlabel('X')
ylabel('F(X)')
r(1)=input('Enter the first guess : ');
r(2)=input('Enter the second guess : ');
arr=zeros(1,100);% Pre-alloacation of approximations of root
ae=input('Enter the allowed error like 0.01 or 1e-2 for accurate upto two
decimal places: ');
fprintf('Iteration No. Approximate Root F(x)\n')
for i=2:100
% Secant Method Formula
r(i+1) = r(i) - (f(r(i))).*((r(i-1) - r(i))./(f(r(i-1)) - f(r(i))));
arr(i)= double(r(i+1));
fprintf('%d %15.10f %1.1e\n',i-1,double(r(i+1)),double(f(r(i+1))))
if abs(f(r(i+1)))<=ae% Stoppin Criteria
fr=r(i+1); % Final Approximation
break
end
end
nonz=arr(arr~=0); % Exemption of raw data
nonz=(abs(fr-nonz))*100/abs(fr); % Relative Percent Error
subplot(1 ,2 ,2);
plot( 1:1:length(nonz), nonz, '*-'); % Plot of Relative Percent Error
vs Iteration Number
line([0 0], ylim); %y-axis
line(xlim, [0 0]); %x-axis
title('Relative Percent Error vs Iteration')
xlabel('Iteration Number')
ylabel('Relative Percent Error %')
% end of program
In the above program we have to write function then it will display a graph of the function which
will help to choose the nearest interval. The program will ask for allowed error, first and second
guess. Program will then display the iteration number, root, function value, and graph of relative
percent error vs iteration number.
Problems
Problem 1
Determine the real root of f(x) = x3 − 6x2 + 11x − 6.1 using secant method.
Solution
Enter the function x^3-6*x^2+11*x-6.1 in the script.
Run the MATLAB file for the Secant Method.
Enter the allowed error 0.0001 for root accurate up to four decimal places.
It will display a graph which will help to choose initial interval as shown in figure below.
Figure 2.1: Function Graph for Problem 1
From fig. 2.1 it can be seen that root lies between 0 and 1.
Write first guess 0 and second 1 and press enter.
Results
Enter the first guess : 0
Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 0.0001
1 0.8333 5.8e-01
2 0.6044 -3.2e-01
3 0.6863 4.7e-02
4 0.6760 3.0e-03
5 0.6753 -3.1e-05
Required root 0.6753 is accurate up to 5 decimal places and achieved in the 5th iteration.
Figure 2.2: Relative Percent Error vs Iteration Number Graph for Problem 1
Problem 2
Find the solution of
𝑓(𝑥, 𝑦) = 6𝑥 2 + 4𝑦 2 + 7𝑥𝑦 − 5𝑦 − 1
g(x, y) = 3x 2 + 2y 2 − 5xy + 9
Solution
Enter the function f(x,y)= x^2+x*y-10,g(x,y)= y+3*x*y^2-57 in the script.
Run the MATLAB file for the Newton’s Method for System of Non-linear Equations.
Enter the allowed error 0.0001 for root accurate up to four decimal places.
It will display a graph which will help to choose initial interval as shown in figure below.
Figure 2.3: Function Graph for Problem 2
From fig. 2.1 it can be seen that root lies on x=2 and y=3.
We will write x=1 and y=2 to iterate the process.
Results
Enter the initial guess for x : 1
Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 1E-4
Figure 2.4: Relative Percent Error vs Iteration Number Graph for Problem 2
Problem 3
Solve the equations
sin x + 2 sin y = 1,
Solution
Enter the function f(x,y)= sin(x*pi/180)+2*sin(y*pi/180)-1,g(x,y)=
2*sin(3*x*pi/180)+3*sin(3*y*pi/180)-0.3 in the script.
Run the MATLAB file for the Newton’s Method for System of Non-linear Equations.
Enter the allowed error 1e-4 for root accurate up to four decimal places.
It will display a graph which will help to choose initial interval as shown in figure below.
Figure 2.5: Function Graph for Problem 3
Results
Enter the initial guess for x : 61
Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 1e-4
Required root x=62.1088, y=3.3297 is accurate up to 10 and 8 decimal places respectively and
achieved in the 2nd iteration.
Figure 2.6: Relative Percent Error vs Iteration Number Graph for Problem 3
Engineering Problem
The Millikan oil-drop experiment for the determination of the ratio of the charge to the mass of
an electron leads to the equation (Stokes' Law)
Solution
Enter the function v-(2/9).*g.*((x^2)./n).*(u-u1).*(1+(0.000617./(p*x))) in
the script.
Run the MATLAB file for the Secant Method.
Enter the allowed error 1e-8 for root accurate up to eight decimal places.
It will display a graph which will help to choose initial interval as shown in figure below.
Figure 2.7: Function Graph for Engineering Problem
From the above figure we can see that root lies between 0 and 0.05 but we cannot write 0
as guess as it causes solution to be undefined.
Write 1e-5 as first guess and 0.05 as second and press enter.
Results
Enter the first guess : 1e-5
Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 1e-8
1 0.00001009 4.6e-03
2 0.00001017 4.6e-03
3 0.00015851 -2.4e-02
4 0.00003430 3.2e-03
5 0.00004918 1.7e-03
6 0.00006685 -6.2e-04
7 0.00006223 6.5e-05
8 0.00006267 2.0e-06
9 0.00006269 -6.7e-09
Required root 0.00006269 is accurate up to 9 decimal places and achieved in the 9th iteration.
For the given data radius of oil drop will be 0.00006269 units.
Figure 2.8: Relative Percent Error vs Iteration Number Graph for Engineering Problem
Conclusion
From this lab we concluded that we can solve long iterative questions in MATLAB within
seconds. In engineering problems we need root with high accuracy which is almost impossible to
do manually. MATLAB gives us root with high accuracy within seconds.