1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double taylor(double x,double accuracy);
double xval;
double acc;
double nx;
double modulation;
int main()
{
FILE*fp;
if((fp=fopen("output.txt","wb"))==NULL)
{
puts("can't open file");
exit(1);
}
puts("enter range of X values like so (x1,x2) : ");
scanf("(%lf,%lf)",&xval,&nx);
if(xval>nx){
double swap;
swap=nx;
nx=xval;
xval=swap;
}
//printf("%lf,%lf",xval,nx);
modulation=(nx-xval)/15;
puts("enter accuracy : ");
scanf("%lf",&acc);
int i=0;
printf("X Accuracy Taylor Library ");
while((nx)>(xval+modulation)){
// while((nx)>(xval+modulation))
fprintf( fp, "rows = %d, columns = %d\n", xval, acc) ;
// fputs("xval",fp);
printf("\n%10.7lf%10.7lf%10.7lf%10.7lf",xval, acc,taylor(xval, acc),log(xval));
xval+=modulation;
i++;
}
// FILE*fp;
//if((fp=fopen("output.txt","wb"))==NULL)
//{
//puts("can't open file");
//exit(1);
//}
//fprintf("\n%10.7lf%10.7lf%10.7lf%10.7lf",xval, acc,taylor(xval, acc),log(xval));////tbc
fclose(fp);
//printf("Ln(x) is equal to: %lf",taylor(xval, acc));
system ("PAUSE");
}
double taylor(double xv,double accuracy){
if(xv<0){
puts("X value must be positive please try again");
exit(1);
}
double numer;
double divide;
double summation1=0;
double summation2;
int n=1;
double lnx=0;
while(fabs(lnx-log(xv))>accuracy){
numer=pow ( ((xv-1)/(xv+1)),((2*n)-1) );
divide= numer/((2*n)-1);
summation2=summation1+divide;
summation1=summation2;
lnx=2*summation2;
n++;
//printf("%lf\n",lnx);
}
HUGE_VAL=lnx;
return HUGE_VAL;
}
|