Secant Method Using C

Earlier in Secant Method Algorithm and Secant Method Pseudocode , we discussed about an algorithm and pseudocode for computing real root of non-linear equation using Secant Method. In this tutorial we are going to implement this method using C programming language.

Secant Method C Program


/* Program: Finding real roots of nonlinear
   equation using Secant Method
   Author: CodeSansar
   Date: November 18, 2018 */

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

/* Defining equation to be solved.
   Change this equation to solve another problem. */
#define    f(x)    x*x*x - 2*x - 5

void main()
{
	 float x0, x1, x2, f0, f1, f2, e;
	 int step = 1, N;
	 clrscr();
	 /* Inputs */
	 printf("\nEnter initial guesses:\n");
	 scanf("%f%f", &x0, &x1);
	 printf("Enter tolerable error:\n");
	 scanf("%f", &e);
	 printf("Enter maximum iteration:\n");
	 scanf("%d", &N);
	
	 /* Implementing Secant Method */
	 printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
	 do
	 {
		  f0 = f(x0);
		  f1 = f(x1);
		  if(f0 == f1)
		  {
			   printf("Mathematical Error.");
			   exit(0);
		  }
		  
		  x2 = x1 - (x1 - x0) * f1/(f1-f0);
		  f2 = f(x2);
		  
		  printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2, f2);
		  
		  x0 = x1;
		  f0 = f1;
		  x1 = x2;
		  f1 = f2;
		  
		  step = step + 1;
		  
		  if(step > N)
		  {
			   printf("Not Convergent.");
			   exit(0);
		  }
	 }while(fabs(f2)>e);
	
	 printf("\nRoot is: %f", x2);
	 getch();
}

Output: Secant Method Using C

Enter initial guesses:
1
2
Enter tolerable error:
0.00001
Enter maximum iteration:
10

Step            x0              x1              x2              f(x2)
1               1.000000        2.000000        2.200000        1.248001
2               2.000000        2.200000        2.088968        -0.062124
3               2.200000        2.088968        2.094233        -0.003554
4               2.088968        2.094233        2.094553        0.000012
5               2.094233        2.094553        2.094552        0.000001

Root is: 2.094552

Recommended Readings

  1. Secant Method Algorithm
  2. Secant Method Pseudocode
  3. Secant Method Using C with Output
  4. Secant Method Using C++ with Output