C Program for Secant Method with Output

Table of Contents

This program implements Secant Method for finding real root of nonlinear function in C programming language.

In this C program, x0 & x1 are two initial guesses, e is tolerable error and f(x) is actual equation whose root is being obtained using secant line method.

C Source Code: Secant Method


/* 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();
}

C Program Output: Secant Method

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