Andy C. answered 10/08/17
Tutor
4.9
(27)
Math/Physics Tutor
#include < stdio.h >
#include < math.h >
void Go()
{
int N=-1;
double x;
char sign_flag=1;
double curTerm;
double sum=0;
int iLoop;
double powX;
FILE * fptr;
double percentError;
double actualResult;
while ((N<0) || (N>32767))
{
printf(" Please input the number of terms in the series :>");
scanf("%d",&N);
}
x = -2;
while ((x<-1) || (x>1))
{
printf(" Please input the value of x between -1 and 1 :>");
scanf("%lf",&x);
}
powX = x;
for (iLoop=0; iLoop
{
curTerm = powX/(iLoop+1) ;
printf(" iLoop = %12ld \n",iLoop);
printf( " powX = %12.12lf \n",powX);
printf(" curTerm = %12.12lf \n",curTerm);
printf(" sum thus far is %12.12lf \n ",sum);
if (sign_flag==1)
{
sum = sum + curTerm;
sign_flag = 0;
}
else
{
sum = sum - curTerm;
sign_flag = 1;
}
powX = powX * x;
}
actualResult = log(1+x);
if (actualResult!=0)
{
percentError = (actualResult - sum)/actualResult;
}
else
{
percentError = sum;
}
printf(" N = %d \n", N);
printf( " x = %12.6lf \n",x);
printf(" Approximation is %12.12f \n",sum);
printf(" Actual value is %12.12f \n",actualResult);
printf(" error %12.12lf \n ",percentError);
fptr = fopen("powerseriesout.txt","w") ;
if (fptr != NULL)
{
fprintf(fptr," N = %d \n", N);
fprintf(fptr, " x = %12.6lf \n",x);
fprintf(fptr," Approximation is %12.12f \n",sum);
fprintf(fptr," Actual value is %12.12lf \n",actualResult);
fprintf(fptr," error %12.12lf \n ",percentError);
fclose(fptr);
}
}
void main()
{
Go();
}
#include < math.h >
void Go()
{
int N=-1;
double x;
char sign_flag=1;
double curTerm;
double sum=0;
int iLoop;
double powX;
FILE * fptr;
double percentError;
double actualResult;
while ((N<0) || (N>32767))
{
printf(" Please input the number of terms in the series :>");
scanf("%d",&N);
}
x = -2;
while ((x<-1) || (x>1))
{
printf(" Please input the value of x between -1 and 1 :>");
scanf("%lf",&x);
}
powX = x;
for (iLoop=0; iLoop
curTerm = powX/(iLoop+1) ;
printf(" iLoop = %12ld \n",iLoop);
printf( " powX = %12.12lf \n",powX);
printf(" curTerm = %12.12lf \n",curTerm);
printf(" sum thus far is %12.12lf \n ",sum);
if (sign_flag==1)
{
sum = sum + curTerm;
sign_flag = 0;
}
else
{
sum = sum - curTerm;
sign_flag = 1;
}
powX = powX * x;
}
actualResult = log(1+x);
if (actualResult!=0)
{
percentError = (actualResult - sum)/actualResult;
}
else
{
percentError = sum;
}
printf(" N = %d \n", N);
printf( " x = %12.6lf \n",x);
printf(" Approximation is %12.12f \n",sum);
printf(" Actual value is %12.12f \n",actualResult);
printf(" error %12.12lf \n ",percentError);
fptr = fopen("powerseriesout.txt","w") ;
if (fptr != NULL)
{
fprintf(fptr," N = %d \n", N);
fprintf(fptr, " x = %12.6lf \n",x);
fprintf(fptr," Approximation is %12.12f \n",sum);
fprintf(fptr," Actual value is %12.12lf \n",actualResult);
fprintf(fptr," error %12.12lf \n ",percentError);
fclose(fptr);
}
}
void main()
{
Go();
}
Andy C.
Actually, it believe it is the formulas used
in the power series and why it is even used to begin with.
Power series is never an accurate approximation.
The formula should involve f(x), but as stated, it is nowhere
to be found. Please verify the correct power.
Bisection method and Newton's Method are much more accurate.
Report
10/09/17
Andy C.
Any approximation must sample the function being approximated at least once.
f(x) appears nowhere in the power series expansion.
Report
10/09/17
Andy C.
OK IT IS FIXED!!!! I got 4 digits of accuracy for x=0.5 using N=10 terms in the series.
Yes, it is the MacLaurin series expansion for ln(x+1)
Part of the problem was that I was not enforcing the radius of convergence for
this MacLaurin series. The value input for X must be between -1 and 1.
So any X outside of (-1,1) will cause it to diverge. Poor approximations will soon follow.
I have furnished a while loop which strictly enforces this condition.
I humbly admit the error of using ln(x) as the actual value rather than ln(x+1)
which did not help matters either. Usually comparing apples and oranges doesn't work ;-)
You can comment out the printf statements inside the for loop, as they are there
for debugging. (That is between the for loop and where it says actualResult = log(x+1).
I would recommend leaving the other printf statements alone.
The title of your posting says C program rather than C++.
No worries. Change the header from stdio.h to iostream.h.
Then you can change all of the printfs to cout <<
and
the \n to << endl;
The problem with that is you have to do some cout.setPrecision calls to
specify the number of digits to display, which is more easily done in C
by just specifying the precision after the percent sign.
You may also have to change the file I/O calls if converting to C++
Finally note the caveat in the percent error calculation,
(actual - approximation)/actual
If by chance, the actual result is zero, then the formula will give division by zero.
If you swap the values for approximation and actual in this case, you will get an error of 1 or 100%
So I plugged the approximation as the error in this case. This may not be what you want, but it's a reasonable
substitution, especially if the approximation is good when actual error is zero.
Thank you for the challenging problem!
Report
10/10/17
Mumtahina M.
10/09/17