Friday, July 20, 2012

C Program to find ln(x)





Program.no:-2.

/* To find ln(x) */

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define f0(x) log(x)

#define f1(x) 1/x

#define f2(x) -1/(x*x)

#define f3(x) 2/(x*x*x)

void main()

{

float tv,t,x,h,v,i=0,et=100,ea=100;

clrscr();

puts("Enter value of x & h to find ln(x)");

scanf("%f %f",&x,&h);

tv=f0(x+h);

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

puts("Iter. ln(x+h) \tEa\t\tEt");

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

v=f0(x);

printf("%2.0f\t%f",i++,v);

t=v;

v=v+h*f1(x);

ea=(v-t)*100/v;et=(tv-v)*100/tv;

printf("\n%2.0f\t%f\t%f\t%f",i++,v,ea,et);

t=v;

v=v+h*h*f2(x)/2;

ea=(v-t)*100/v;et=(tv-v)*100/tv;

printf("\n%2.0f\t%f\t%f\t%f",i++,v,ea,et);

t=v;

v=v+h*h*h*f3(x)/(3*2);

ea=(v-t)*100/v;et=(tv-v)*100/tv;

printf("\n%2.0f\t%f\t%f\t%f",i++,v,ea,et);

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

getch();

}



















Output of Program:-




First:-

Enter value of x & h to find ln(x)

1 3

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

Iter. ln(x+h) Ea Et

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

0 0.000000

1 3.000000 100.000000 -116.404259

2 -1.500000 300.000000 208.202133

3 7.500000 120.000000 -441.010651

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




Second:-

Enter value of x & h to find ln(x)

3 1




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

Iter. ln(x+h) Ea Et

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

0 1.098612

1 1.431946 23.278353 -3.293046

2 1.376390 -4.036325 0.714442

3 1.388736 0.888986 -0.176110



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


No comments:

Post a Comment