EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Secant MATLAB
 

Secant MATLAB

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Secant MATLAB

 

 

Introduction to Secant MATLAB

Secant method is used to find the root of an equation in mathematics. In this topic, we are going to discuss Secant MATLAB. This method can be used to find the root of a polynomial equation (f(x) = 0) if the following conditions are met:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

  1. Root must lie in the interval defined by [a, b]
  2. The function must be continuous in the above interval
  3. The product f(a) * f(b) must be less than zero.

The secant method requires 2 guesses to be made initially. It estimates the intersection point of the function and the X-axis as correctly as possible. For this, it creates a secant line using the initial guesses and keeps on repeating this process iteratively.

Examples of Secant MATLAB

Let us now understand the code of the secant method in MATLAB:

Example #1

In this example, we will take a polynomial function of degree 3

Code:

Z = input ('Enter the input function and set right hand side equal to zero:','s');
[Asking user to enter the function] [Enter the input function as x.^3 - x - 1] f = inline (Z);
[Calling inline command to create the function in MATLAB] A (1) = input ('Enter the initial value for guess interval:');
[Asking user to enter ‘Initial value’ which is used as the first value for 1st iteration] [Enter the first value as 1] A (2) = input ('Enter the last value for guess interval:');
[Asking user to enter ‘End value’ which is used as the last value for 1st iteration] [Enter the end value as 2] allowedError = input ('Enter the error allowed:');
[Asking user to enter tolerable error] [Pass the allowed error as 0.001] iter = 0;
[Initializing the iterations] for i = 3 : 500
A (i) = A (i-1) - (f (A ( i - 1))) * ((A (i - 1) - A (i - 2)) / (f (A (i - 1)) - f(A (i - 2))));
iter = iter+1;
if abs ((A (i)-A (i - 1)) / A (i)) * 100 <allowedError
root = A (i)
iter = iter
break
end
end
outStr = ['Required root for the input equation is: ', num2str (A (i)), '']

Explanation of the ‘if-else’ loop above:

  1. With initial guesses, a secant line is constructed to the function passing through (A(1), f (A(1)) and (A(2), f (A(2))). The line’s equation is:
  2. y = [f (A(2)) – f (A(1)) / A(2) – A(1)] * (A – A(2)) + f (A(2))
  3. ‘y’ is substituted as zero to get the value of ‘A’
  4. ‘A’ is now considered as new end value
  5. This iterative process then carries on until we get the root

This is how our output will look like in MATLAB:

Output:

Secant MATLAB output 1

We have obtained the root of our input function as 1.3247 in 6 iterations.

Example #2

In this example, we will take another polynomial function of degree 3.

Code:

Z = input ('Enter the input function and set right hand side equal to zero:','s');
[Asking user to enter the function] [Enter the input function as x.^3 – x.^2 - 1] f = inline (Z);
[Calling inline command to create the function in MATLAB] A (1) = input ('Enter the initial value for guess interval:');
[Asking user to enter ‘Initial value’ which is used as the first value for 1st iteration] [Enter the first value as -1] A (2) = input ('Enter the last value for guess interval:');
[Asking user to enter ‘End value’ which is used as the last value for 1st iteration] [Enter the end value as 3] allowedError = input ('Enter the error allowed:');
[Asking user to enter tolerable error] [Pass the allowed error as 0.001] iter = 0;
[Initializing the iterations] for i = 3 : 500
A (i) = A (i-1) - (f (A ( i - 1))) * ((A (i - 1) - A (i - 2)) / (f (A (i - 1)) - f(A (i - 2))));
iter = iter+1;
if abs ((A (i)-A (i - 1)) / A (i)) * 100 <allowedError
root = A (i)
iter = iter
break
end
end
outStr = ['Required root for the input equation is: ', num2str (A (i)), '']

Output:

Secant MATLAB output 2

We have obtained the root of our input function as 1.4656 in 13 iterations.

Example #3

In this example, we will take a polynomial function of cos and sine

Code:

Z = input ('Enter the input function and set right hand side equal to zero:','s');
[Asking user to enter the function] [Enter the input function as cos(x) + 2*sin(x) + x^2] f = inline (Z);
[Calling inline command to create the function in MATLAB] A (1) = input ('Enter the initial value for guess interval:');
[Asking user to enter ‘Initial value’ which is used as the first value for 1st iteration] [Enter the first value as 0] A (2) = input ('Enter the last value for guess interval:');
[Asking user to enter ‘End value’ which is used as the last value for 1st iteration] [Enter the end value as -0.1] allowedError = input ('Enter the error allowed:');
[Asking user to enter tolerable error] [Pass the allowed error as 0.001] iter = 0;
[Initializing the iterations] for i = 3 : 500
A (i) = A (i-1) - (f (A ( i - 1))) * ((A (i - 1) - A (i - 2)) / (f (A (i - 1)) - f(A (i - 2))));
iter = iter+1;
if abs ((A (i)-A (i - 1)) / A (i)) * 100 <allowedError
root = A (i)
iter = iter
break
end
end
outStr = ['Required root for the input equation is: ', num2str (A (i)), '']

Output:

output 3

We have obtained the root of our input function as -0.6593 in 6 iterations.

Conclusion – Secant MATLAB

  • Secant method is used to find the root of any polynomial function
  • As there is no direct function for the secant rule in MATLAB, we define the code or logic for it manually.

Recommended Articles

This is a guide to Secant MATLAB. Here we discuss the Examples of Secant MATLAB along with the codes and outputs. You may also have a look at the following articles to learn more –

  1. Curve Fitting Matlab
  2. Matlab loglog()
  3. MATLAB cylinder()
  4. Matlab Sine Wave

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW