Help with solution to Taylor series

My lecturer complains that there is many error inefficiencies with this code. Any help would be greatly apprciated
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;
              
              
       }
      



#include <stdio.h>
#include <stdlib.h>

struct complex{
int real ;
int imag ;
};

int realadd( struct complex c , struct complex d);
int imagadd( struct complex x , struct complex y);

int main()
{
struct complex a , b ;
printf("Enter a and b where a + ib is the first complex number.\n");
printf("a = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.imag);
printf("Enter c and d where c + id is the second complex number.\n");
printf("c = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.imag);

printf("answer = %d + %di", realadd( a , b), imagadd(a ,b));
getch();


}

int realadd( struct complex c , struct complex d)
{

return c.real + d.real;
}


int imagadd( struct complex x , struct complex y)
{

return x.imag + y.imag;
}




Last edited on
1
2
3
4
5
6
7
8
9
10
printf("Enter a and b where a + ib is the first complex number.\n");
   printf("a = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.\n");
   printf("c = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);
Usually it is a requirement for using Taylor series to approximate the value of a function, that the <math.h> or <cmath> libraries are not used. Well, you might use that library in order to compare your end result with the expected result. But it should not be used in the actual calculation.

I see that you have both pow() and log() functions used within the taylor() function. So first of all, I would recommend you try to re-write that function so it doesn't depend upon either of those library functions.
Topic archived. No new replies allowed.