0% found this document useful (0 votes)
418 views13 pages

Lab 02 Secant and System of Non-Linear Equations

This lab contains matlab codes to to find the solution of the system of equations using Secant and System of Non-linear Equations

Uploaded by

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

Lab 02 Secant and System of Non-Linear Equations

This lab contains matlab codes to to find the solution of the system of equations using Secant and System of Non-linear Equations

Uploaded by

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

University of Engineering and Technology Peshawar

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 ) − 𝑓(𝑥𝑖 )

We do not need to bracket the successive approximations for root.

Newton Method for System of Non-linear Equations


A system of nonlinear equations is a system of two or more equations in two or more variables
containing at least one equation that is not linear. Newton’s method is an algorithm for finding
the roots of differentiable functions that uses iterated local linearization of a function to
approximate its roots.

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 = 𝑦𝑖 + 𝑘

A close initial approximation will result in faster convergence.

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.

MATLAB Code for Newton’s Method for System of Non-linear Equations


%Newton's Method for System of Non-linear Equations by Umair Ali Shah
clear all;
clc;
format short
% Write function here in the sequence as shown
syms x y;
f(x,y)= x^2+x*y-10;
g(x,y)= y+3*x*y^2-57;
subplot(1 ,2 ,1); % Multiple plots together
hold on
w=ezplot(f,[0, 4, 0, 4]);
set(w,'Color','blue')
s=ezplot(g,[0, 4, 0, 4]);
set(s,'Color','green')
legend('f(x,y)','g(x,y)')
title('Function Plot')
hold off
% Derivatives of functions with respect to x and y
fx(x,y)=diff(f(x,y), x);
fy(x,y)=diff(f(x,y), y);
gx(x,y)=diff(g(x,y), x);
gy(x,y)=diff(g(x,y), y);
x(1)=input('Enter the initial guess for x : ');
y(1)=input('Enter the initial guess for y : ');
ae=input('Enter the allowed error like 0.01 or 1e-2 for accurate upto two
decimal places: ');
xer=zeros(1,100);
yer=zeros(1,100);
fprintf('Iter. No. X Y f(x, y) g(x, y)\n')
for i=1:100
M=[fx(x(i), y(i)), fy(x(i), y(i)); gx(x(i), y(i)), gy(x(i), y(i))];
Jac(i)=det(M);
if Jac(i)==0
disp('Solution of this equation does not exist')
break
end
h(i)=(-f(x(i), y(i))*gy(x(i),y(i))+g(x(i), y(i))*fy(x(i), y(i)))/Jac(i);
k(i)=(-g(x(i), y(i))*fx(x(i),y(i))+f(x(i), y(i))*gx(x(i), y(i)))/Jac(i);
x(i+1)=x(i)+h(i);
xer(i)=double(x(i+1));
y(i+1)=y(i)+k(i);
yer(i)=double(y(i+1));
p(i)=double(f(x(i+1), y(i+1))); % Assifning values to a new variable to
display in command window
q(i)=double(g(x(i+1), y(i+1)));
fprintf('%d %5.4f %5.4f %1.1e
%1.1e\n',i,double(x(i+1)),double(y(i+1)),p(i),q(i))
if abs(f(x(i+1), y(i+1)))<=ae && abs(g(x(i+1), y(i+1)))<=ae
xtr=double((x(i+1)));
ytr=double((y(i+1)));
break
end
end
nonzx=(abs(xtr-xer(1:i)).*100)./xtr;
nonzy=(abs(ytr-yer(1:i)).*100)./ytr;
subplot(1,2,2);
plot(1:length(nonzx),nonzx,1:length(nonzy),nonzy,1:length(nonzx),nonzx,'.r',1
:length(nonzy),nonzy,'.r');
legend('x','y')
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 second guess : 1

Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 0.0001

Iteration No. Approximate Root F(x)

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

using Newton’s Method for system of non-linear equations.

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 initial guess for y : 2

Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 1E-4

Iter. No. X Y f(x, y) g(x, y)

1 2.2000 4.2000 4.1e+00 6.4e+01

2 1.9553 3.3022 2.8e-01 1.0e+01

3 1.9955 3.0108 -1.0e-02 2.8e-01

4 2.0000 3.0000 -2.8e-05 -1.8e-04

5 2.0000 3.0000 1.9e-11 1.9e-10


Required root x=2, y=3 is accurate up to 11 and 10 decimal places respectively and achieved in
the 5th iteration.

Figure 2.4: Relative Percent Error vs Iteration Number Graph for Problem 2

Problem 3
Solve the equations

sin x + 2 sin y = 1,

2 sin 3x + 3 sin 3y = 0.3.

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

 From above figure lets x=61 and y=2.

Results
Enter the initial guess for x : 61

Enter the initial guess for y : 2

Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 1e-4

Iter. No. X Y f(x, y) g(x, y)

1 62.1077 3.3246 -1.9e-04 -6.8e-04

2 62.1088 3.3297 -6.2e-10 -1.8e-08

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)

v = (2 / 9) g {(r2) / (n)} (μ – μ1) [1 + {(.000617) / (pr)}].

Find r, the radius of the oil-drop, for an experiment in which

g = 980, n = 1.832 × 10–4, μ1 = .0012, μ = .9052, p = 72, v = .00480.

Use the secant method to find the radius.

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 second guess : 0.05

Enter the allowed error like 0.01 or 1e-2 for accurate upto two decimal places: 1e-8

Itr. No. Approx. Root F(x)

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.

You might also like