Friday, July 20, 2012

C Program For Secant method 2





/*Secant Method*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<alloc.h>

#include<process.h>

#define f(x) sin(x)+cos(1+x*x)-1

double deri(double);

void main()

{

double i,p,it=50;

double v,xo,x1,ea,x_1;

char ch;

clrscr();

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

puts("------- Finding Root Using Secant Method --------");

puts("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");

puts("-------------------------------------------------");

puts("Enter Value of X-1,X0\n");

scanf("%lf %lf",&x_1,&xo);

puts("------------------------------------------------");

puts("\nEnter no. of Iteration\n");

scanf("%lf",&it);

puts("------------------------------------------------");

i=0;

puts("Iteration \tXi\t\tEa");

while(1)

{

i++;

x1=xo-(f(xo)*(x_1-xo))/(f(x_1)-f(xo));

ea=(x1-xo)*100/x1;

if(ea<0) /*-- Eliminating error due to --*/

ea=ea*(-1); /*-- Negative sign of Ea --*/

if(f(x1)==0||i==it)

{

printf("\n%3d\t\t%5.6lf\t%5.4lf",(int)i,x1,ea);

puts("\n---------------------------------------------");

getch();

exit(0);

}

printf("\n%3d\t\t%5.6lf\t%5.4lf",(int)i,x1,ea);

x_1=xo;

xo=x1;

}

getch();

}
















Output Of Program:-

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

------- Finding Root Using Secant Method --------

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

-------------------------------------------------

Enter Value of X-1,X0

1 3

------------------------------------------------

Enter no. of Iteration

10

------------------------------------------------

Iteration Xi Ea




1 3.509634 14.5210

2 4.057609 13.5049

3 3.964200 2.3563

4 3.399639 16.6065

5 3.489194 2.5667

6 9.558526 63.4965

7 12.883433 25.8076

8 13.638773 5.5382

9 14.151677 3.6243

10 16.520063 4.3364

--------------------------------------------






















































































No comments:

Post a Comment