Skip to content

Secant Method

by kaliadevansh on June 12th, 2012

Secant Method is a method to find the roots of a function f.
For coding, we define the function f in a separate code and then use it accordingly.
The C code for this method is:

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(2*x-log10(x)-6);
}
void main()
{
float x,x1,a,b,err=0.00005;
int itr=1,n;
clrscr();
printf("enter the values of a,b and maximum iterations\n");
scanf("%f%f%d",&a,&b,&n);
x=b-(((b-a)/(f(b)-f(a)))*f(b));
printf("iteration no. %d\tx=%f\n",itr,x);
itr++;
while(itr<n)
{
a=b;
b=x;
x=b-(((b-a)/(f(b)-f(a)))*f(b));
printf("iteration no. %d\tx=%f\n",itr,x);
if(fabs(x1-x)<err)
{
printf("\nafter %d iterations, the value of root is %f\n",itr,x);
break;
}
else
itr++;
x1=x;
}
if(itr==n)
printf("\nsolution does not exist as iterations are not sufficient");
getch();
return 0;
}

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS