Secant Method

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;
}

kaliadevansh has written 15 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>