Ways to Implement the Secant Method:
The Secant Method in C programming can be implemented in the following ways:
- The first step is to transform the quadratic problem into a system of linear equations . For instance, take the equation 3x2 + 4x - 8 = 0 . Through manipulation, it is changed to 3(x + 2)2 = 8 . Once it is done, the equation can be recast as two linear expressions: x + 2 = sqrt(8/3) and 3x + 6 = sqrt(8/3) .
- 'a' and 'b' stand for the user's initial guesses when asked for them. The approach might not work if these assumptions are made if f(a) and f(b) are equal.
- Inputs for the required accuracy and the maximum number of iterations should be gathered. Until the target accuracy is attained or the maximum iteration count is reached, the method will iterate to improve the root estimate.
- Create an iteration loop to cycle through the algorithm. Apply the following equation to determine the next approximation, "c": c = (a f(b) - b f(a)) / (f(b) - f(a)) . Update 'a' and 'b' after that in preparation for the following iteration.
- After that, display the current iteration's number and the value of "c" in the output. Continue the loop until the accuracy is less than the desired absolute value of f(c).
Example:
Using the Secant Method in C to solve x^3 - 4
#include <stdio.h>
#include <math.h>
float f(float x) {
return (x * x * x - 4);
}
int main() {
float a, b, c, e;
int count = 1, n;
printf("Enter the values of a and b:\n");
scanf("%f%f", &a, &b);
printf("Enter the values of allowed error and maximum number of iterations:\n");
scanf("%f %d", &e, &n);
do {
if (f(a) == f(b)) {
printf("Solution cannot be found as the values of a and b are the same.\n");
return 0;
}
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
a = b;
b = c;
printf("Iteration No-%d x=%f\n", count, c);
count++;
if (count > n) {
break;
}
} while (fabs(f(c)) > e);
printf("\nThe required solution is %f\n", c);
return 0;
}
Output:
Enter the values of a and b:
1 2
Enter the values of allowed error and maximum number of iterations:
0.0001 10
Iteration No-1 x=2.333333
Iteration No-2 x=1.897810
Iteration No-3 x=1.751373
Iteration No-4 x=1.730823
Iteration No-5 x=1.732114
The required solution is 1.732114
Explanation:
In this C program, the Secant Method is applied to approximate the root of the equation x3 - 4 = 0. Users input initial guesses 'a' and 'b', as well as the number of iterations 'n' and the acceptable error 'e'. The algorithm iteratively updates 'a' and 'b' to approach the root, displaying the latest approximation after each iteration. The program stops execution when the error falls below 'e' or after 'n' iterations.
Conclusion:
In summary, the Secant Method has proven to be a reliable technique for estimating roots of quadratic equations with great accuracy. Its effectiveness lies in the unique approach of transforming quadratic challenges into linear ones and continuously improving the approximations through iterations. This piece delved into the basics of the Secant Method, its implementation in C programming, and provided a practical code illustration. Proficiency in the Secant Method equips developers with a powerful tool for numerical computation, offering a versatile solution for complex mathematical challenges. The provided instance not only showcases its practicality but also highlights its rapid convergence to accurate root approximations.