Updated March 4, 2023
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:
- Root must lie in the interval defined by [a, b]
- The function must be continuous in the above interval
- 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:
- 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:
- y = [f (A(2)) – f (A(1)) / A(2) – A(1)] * (A – A(2)) + f (A(2))
- ‘y’ is substituted as zero to get the value of ‘A’
- ‘A’ is now considered as new end value
- This iterative process then carries on until we get the root
This is how our output will look like in MATLAB:
Output:
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:
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:
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 –