Mumtahina M.

asked • 10/08/17

C program for the approximation of ln(x+1) = x-(1/2)x^2 + (1/3)x^3 - (1/4)x^4 +...+ (((-1)^(n+1))*x)/n

 Write a program, powerseries.c, to check the accuracy of this formula. Your program must
prompt the user for the value of x (where |x| < 1) and the degree, n, of the polynomial to use.
Your program should then write the values of x, n, the approximation, the actual value and
the percent relative error to the output file powerseriesout.txt.

1 Expert Answer

By:

Andy C. answered • 10/08/17

Tutor
4.9 (27)

Math/Physics Tutor

Mumtahina M.

Hi. Thank you for the reply. But I just coded this on c++, and the answers it gives are wrong. Can you see if there are any errors?
Report

10/09/17

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

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.