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

Lab 6 Master Solution Manual - Secant Method

Lab 6 focuses on the Secant Method for finding roots of equations without requiring derivatives, making it advantageous for non-elementary functions. The experiment involves implementing the Secant Method in MATLAB to find roots of specific functions, analyzing results based on different initial values. The lab report template outlines the necessary sections to document objectives, procedures, results, discussions, and conclusions.

Uploaded by

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

Lab 6 Master Solution Manual - Secant Method

Lab 6 focuses on the Secant Method for finding roots of equations without requiring derivatives, making it advantageous for non-elementary functions. The experiment involves implementing the Secant Method in MATLAB to find roots of specific functions, analyzing results based on different initial values. The lab report template outlines the necessary sections to document objectives, procedures, results, discussions, and conclusions.

Uploaded by

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

Lab 6: Secant Method

Introduction:
The Newton-Raphson algorithm requires two functions evaluations per iteration, f (xk) and f
(xk). Historically, the calculation of a derivative could involve considerable effort. Moreover, many
functions have non-elementary forms (integrals, sums etc.), and it is desirable to have a method
for finding a root that does not depend on the computation of a derivative. The secant method
does not need a formula for the derivative and it can be coded so that only one new function
evaluation is required per iteration. The formula for the secant method is the same one that was
used in the Regula Falsi method, except that the logical decisions regarding how to define each
succeeding term are different.
Objectives:
The objectives of this experiment are;
1. To determine roots of an equation in single variable using Secant method.
2. To understand the MATLAB implementation of the Secant method.
3. To analyze of results using different initial values.
Preliminary Work (to be done before lab session):
1. Consider the function 𝑓(𝑥) = ln(x − 1) + cos(x − 1) = 0, find a root of the function using the
interval 1.3 ≤ x ≤ 2. Perform 10 iterations of the Secant method. Use the tolerance up to 5
decimal places.
2. Understand the algorithm, the corresponding pseudo-code and MATLAB code of the Secant
method.
Secant Method (Theoretical Explanation):
Secant method is basically the modified form of Newton Raphson method. Derivative in Newton
Raphson method is replaced by difference ratio. Secant method is also a recursive method for
finding the root for the polynomials by successive approximation. Secant Method is open method
and starts with two initial guesses for finding real root of non-linear equations. It’s similar to the
Regular-Falsi method but here we don’t need to check f(x1)*f(x2) < 0 again and again after every
approximation. In this method, the neighbor roots are approximated by secant line or chord to the
function. It’s also advantageous of this method that we don’t need to differentiate the given
function, as we do in Newton-Raphson method.
x n−1 f ( x n )−x n f (x n−1 )
x n+1= n = 1, 2, 3,….
f ( x n ) −f (x n−1 )

In Secant method if x0 and x1 are initial guesses then next approximated root x2 is obtained by
putting n =1 in above formula.
x0 f ( x 1 ) −x1 f (x 0)
x 2=
f ( x 1 )−f ( x 0 )
In the Secant method, the derivative can be approximated by a backward finite divided
difference, as in the figure.

This algorithm is first in the class of Householder's methods, succeeded by Halley's method. The
method can also be extended to complex functions and to systems of equations.
Steps for executing the Secant Method
1. Choose two initial points x0 and x1 and compute the value of f(x0) and f(x1).
2. For Loop Condition: Compute the next value of the root for n = 1 using the following
expression;
x0 f ( x 1 ) −x1 f (x 0)
x 2=
f ( x 1 )−f ( x 0 )
3. Repeat step 2 until you start getting same values for xn+1 up to 5 decimal places.
Repeat this iterative process again and again until you find the root using Stopping
Criteria.
For Stopping Criteria: For finding the root of the function, the one of the following stopping
criteria can be used.
 |f(xn+1)| < tolerance (ε)
 |f(xn+1 - xn)| < tolerance (ε)
 |f(xn+1 - xn)|/ xn+1 < tolerance (ε)
Experiment:
Software Required: MATLAB
Procedure:
1. Algorithm for Secant Method (Step Wise)
This method is similar to the Newton-Raphson method where an estimate of the root is predicted
by extrapolating a tangent of the function to the x-axis as shown in below figure. However, the
secant method uses a difference rather than a derivative to estimate the slope. The Secant
iterative updating formula can be written as;
x n−1 f ( x n )−x n f (x n−1 )
x n+1= n = 1, 2, 3,….
f ( x n ) −f (x n−1 )

Figure: Graphical description of the Secant Method.


From the above formula, an initial value is needed.
And an algorithm for Secant method involves repetition of above process i.e. we use x 1 and x2 to
find x3 and so on until we find the root within desired accuracy.
An algorithm for Secant method requires following steps in order to solve any non-linear
equation with the help of computational tools:
1. start
2. Define function as f (x)
3. Choose the input initial guess (x0 and x1), pre-specified tolerable error (e) and maximum
iteration (N)
4. Initialize iteration counter i = 1
5. There is no as such condition to fail the secant method and there will not be use any type of if
or elseif condition for this method.
Loop Start
6. Increment iteration counter i = i + 1
7. Calculate
x0 f ( x 1 ) −x1 f (x 0)
x 2=
f ( x 1 )−f ( x 0 )
8. If i >= N then print "Not Convergent” and go to step 11, otherwise go to step 9.
9. If |f(x2)| > e then set x0 = x1 , x1 = x2 and go to step 10.
Loop End
10. Print root as x2.
11. Stop
2. Pseudocode for Secant Method
Pseudocode for Secant method involves following steps in order to solve any non-linear
equation with the help of computational tools:
1. Start
2. Define function as f(x)
3. Inputs:
a. Initial guess x0, x1
b. Tolerable Error e
c. Maximum Iteration N
4. No as such condition is required to check the failure of Secant method. So make the loop.
For i = 1:n
x0 f ( x 1 ) −x1 f (x 0)
x 2=
f ( x 1 )−f ( x 0 )
If abs f(x2 – x1) < e
Break
End
x0 = x1
x1 = x2
End
6. Print root as x2
7. Stop
MATLAB Program 1:
Consider the function 𝑓(𝑥) = ln(x − 1) + cos(x − 1) = 0, find a root of the function using the
interval 1.3 ≤ x ≤ 2. Perform 10 iterations of the Secant method. Use tolerance up to 5 decimal
places.
Code Implementation:
clc
clear all
% Define the function
f = @(x) log(x-1)+cos(x-1);
x0 = 1.3;
x1 = 2;
n = 10;
e = 10^-5;
% Processing
for i=1:n
x2 = (x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
fprintf('P%d=%.5f\n',i,x2)
if abs(x2-x1) < e
break
end
x0 = x1;
x1 = x2;
end
% Print the root
fprintf('The root is approximately %.5f\n', i, x2)
Code Output:
P1=1.52061
P2=1.20436
P3=1.43813
P4=1.41088
P5=1.39683
P6=1.39777
P7=1.39775
P8=1.39775
The root is approximately 8.00000
The root is approximately 1.39775
Question:
1. How does the choice of the initial interval affect the solution?
MATLAB Program 2:
2. Consider the function 𝑓(𝑥) = sinx − e−x = 0, find a root of the function using the interval 0 ≤ x
≤ 1. Perform 10 iterations of the Secant method. Use tolerance up to 5 decimal places.
Code Implementation:
clc
clear all
% Define the function
f = @(x) sin(x) - exp(-x);
x0 = 0;
x1 = 1;
n = 10;
e = 10^-5;
% Processing
for i=1:n
x2 = (x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
fprintf('P%d=%.5f\n',i,x2)
if abs(x2-x1) < e
break
end
x0 = x1;
x1 = x2;
end
% Print the root
fprintf('The root is approximately %.5f\n', i, x2)
Code Output:
P1=0.67861
P2=0.56906
P3=0.58926
P4=0.58854
P5=0.58853
The root is approximately 5.00000
The root is approximately 0.58853
Generalized Code Format for Secant Method:
% Ingredients
f = input ('Enter your function: ');
x0 = input ('Enter the first initial guess: ');
x1 = input ('Enter the second initial guess: ');
n = input ('Enter no of iterations: ');
e = input ('Enter tolerance: ');
% Processing
for i=1:n
x2 = (x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
fprintf('P%d=%.5f\n',i,x2)
if abs(x2-x1) < e
break
end
x0 = x1;
x1 = x2;
end
% Print the root
fprintf('The root is approximately %.5f\n', i, x2)
1st Function Output:
Enter your function: @(x) log(x-1)+cos(x-1)
Enter the first initial guess: 1.3
Enter the second initial guess: 2
Enter no of iterations: 10
Enter tolerance: 1e-5
P1=1.52061
P2=1.20436
P3=1.43813
P4=1.41088
P5=1.39683
P6=1.39777
P7=1.39775
P8=1.39775
The root is approximately 8.00000
The root is approximately 1.39775
2nd Function Output:
Enter your function: @(x) sin(x) - exp(-x)
Enter the first initial guess: 0
Enter the second initial guess: 1
Enter no of iterations: 10
Enter tolerance: 1e-5
P1=0.67861
P2=0.56906
P3=0.58926
P4=0.58854
P5=0.58853
The root is approximately 5.00000
The root is approximately 0.58853
Lab Report Template (Psychomotor Domain)
Title/Cover Page
The title page of your Lab report should include the following information:
a) NUTECH Logo
b) No. and Name of the Experiment
c) Submitted To: Name of Instructor - Dr. Muhammad Waqas/ Muzahir Ali (Lab Demo)
d) Submitted By: Individual Member Name with respective registration No.s
e) Date of Experiment Performed
f) Date of Report Submission
Objective
Describe briefly what are the main goals or learning outcome to perform this lab.
Introduction
 Explain the theory behind the practical or experiment. It can include ideal diagrams used in
theory and graphs etc.
 Express the goals and assigned tasks for the laboratory using your own words. What are you
given? What are you to find?
 What are the applicable analytical representations, concepts, operations, and tools you have
learned from the text and lectures?
 What did you learn from this assignment?
Preparation and Procedure
 Approximately 1 page of text, providing answers to Laboratory Prep Problems if any.
 Often performing the lab activity is facilitated by using your Lab Prep Problem solutions. How do
these solutions relate to the laboratory goals, tasks, and expected results?
 Explain your sequence of steps used to achieve the objectives.
 Describe the steps that you took and the results that you obtained for each step. Document your
observations in as much detail as possible. Include MATLAB output, screenshots or photos as
necessary.
Results
In this portion, you will show the results/MATLAB output that you obtained for each step. Document
your observations in as much detail as possible. Include MATLAB output or graphs that you obtained
from your experiment. Graphs should be of good resolution with appropriate axes information and
legends (if necessary).
Discussion
In this portion, you will describe what is achieved during experiment. Identify how variables that you
defined in the preparation section correspond to parameters. Mention the key points learned by you and
answer all the questions asked in this section. What difficulties did you encounter, if any? Be sure to
document how you overcame these. Even difficulties that seem to have trivial solutions, e.g., MATLAB
syntax errors, should be documented so that you do not continue to repeat them in future labs. Suggest
ways to improve your results.
Conclusions
Summarize your findings and discussion in ½ page.

You might also like