Calculating Exponent Using Taylor Series To Given Precision

In summary, the student was struggling to write a program to take two input values, the number to be raised to an exponent, and the number of iterations to use in approximating it. The program had to run until a condition was met, and the student's most recent attempt was to write a loop that would calculate the factorial of the counter variable.
  • #1
Bravus
23
0

Homework Statement



The course is Computational Physics, but in a sense this is a pretty straight computer science or even mathematical challenge.

The first part of the assignment - the relatively easy part - was to write a Fortran program to take two variables - the number to which e should be raised, and the number of iterations of the Taylor series to use in approximating it.

The Taylor series includes the factorial of the iteration number, so a subroutine had to be written to calculate the factorial.

Got all that up and running, without too much trouble.

Homework Equations



ex ≈ Ʃ xi/i! where x is the number to evaluate. i runs from 0 to n-1, where n is the number of iterations specified.

And then there's the definition of the factorial...

The Attempt at a Solution



The second, harder (bonus marks) part of the question is to write a program that takes the same input for the number to be exponentiated, but takes another number, dx. The program should keep evaluating the Taylor Series approximation until it is less than dx away from the true value, obtained using Fortrans exp(x) intrinsic function.

The main problem I'm having is how to calculate the factorial in a loop when the upper bound keeps changing. I don't need you to write out detailed Fortran code, just some suggestions or pseudocode about how to do this calculation with these specifications.
 
Physics news on Phys.org
  • #2
Bravus said:
The main problem I'm having is how to calculate the factorial in a loop when the upper bound keeps changing.
Can you show an attempt to calculate the factorial, even if not done in a subroutine, and an attempt to calculate xi?
 
  • #3
Yeah, here's the code I use for the factorial, in the first part:

REAL FUNCTION rfactor1(iup1)
IMPLICIT NONE
INTEGER, INTENT(IN) :: iup1
INTEGER :: icurr1
REAL :: rans1
!
rans1=1
DO icurr1=1,iup1
rans1=rans1*icurr1
END DO
rfactor1=rans1
!
END FUNCTION rfactor1

The thing is, the variable iup1 is something the user enters, so it knows how many iterations to run. In the part I'm stuck on, the loop has to run instead until a condition is met. I've tried a DO WHILE loop, but it seems to run crazy.

In terms of 'attempts to calculate xi', it's as simple in Fortran as x**1, but I assume you meant something more complex.

What I need is a loop within a loop: the inner one calculates the factorial of the counter variable of the outer loop. Sounds like it shouldn't be that hard to do, but it runs crazy, often getting into endless loops, or else delivering silly answers that are large and negative or on the order of 10-34.

Here's my most recent attempt:

rexact2=exp(rnum2)
icount2=1 (Taylor series starts at 0 but next line takes care of that)
rapprox2=1 (Preload with zeroth term)
rdiffer2=100 (bit of a kludge, but just designing the loop to start initialised and run at least once)
DO WHILE (rdiffer2>rdx2)

DO ix=1,icount2
rxfact2=rxfact2*icount2
END DO

rapprox2=rapprox2+((rnum2**icount2)/(rxfact2))
icount2=icount2+1

END DO

rnum2 is the value of x, the number entered. rdx2 is the difference number entered - i.e. the goal is to get the estimate, rapprox2, to differ from rnum2 by less than rdx2.

A few dodgy things there mathematically, meant to get the code to run, but still not sure why it's running crazy...
 
  • #4
This seems a bit more complicated that it needs to be, and I'm not sure why you chose those names for the variables. You should use double precision variables to reduce truncation error when summing up a large count of numbers.

Here is an example loop for factorial:

Code:
double precision nfac
...
      nfac = 1
      do i = 2,n
          nfac = nfac * i
      end do

Note that you could include calculation of xi in this same loop as well calculation of the sum. Can you try this and show what that code would look like? You could also just calculate a summation term directly:

Code:
double precision sum, term, x
...
      x = ?
      sum = 1
      term = 1
      do i = 2,n
          term = term * ?
          sum = sum + term
      end do
 
Last edited:
  • #5
Cool, will have a bash and get back to you with results.

The variable names are because it's an assignment in a course and the lecturer/tutor wants that style... I agree, I'd far rather use simpler ones.

Thanks.
 
  • #6
Yeah, using double precision makes sense to avoid issues where the factorials get large, which they do pretty quickly...
 
  • #7
Aha! Yes, that'll work. Didn't get what you did... and then I did! Was thinking I had to run a loop from 1 to i each time to calculate the factorial, but of course I can just keep building on the factorial each time.

And yeah, forget the lecturer... I'll go for simpler variables, stop confusing myself!
 
  • #8
The challenge, though, is that I don't know n. There is no specific number of times to work through the loop, it just needs to run until the difference between the approximation and the actual value is smaller than a certain number.
 
  • #9
This works:

rexact2=exp(x)
i=1
dxfact2=1
rdiffer2=100
!
DO WHILE (rdiffer2>dx)
!
dxfact2=dxfact2*i
rapprox2=rapprox2+((x**i)/(dxfact2))
rdiffer2=abs(rapprox2-rexact2)
i=i+1
!
END DO
!

Thanks heaps, well done!
 
  • #10
You can combine the xi and n! calculations into one term:

Code:
      rexact2 = exp(x)
      dxterm = 1
      rapprox2 = 1
      rdiffer2 = 100
      i = 1
!
      DO WHILE (rdiffer2 > dx)
!
          dxterm = dxterm * x / i
          rapprox2 = rapprox2 + dxterm
          rdiffer2 = abs(rapprox2 - rexact2)
          i = i + 1
!
      END DO
 
Last edited:

1. How do you calculate exponents using Taylor series to a given precision?

The Taylor series is a mathematical method for representing a function as a sum of infinitely many terms. To calculate an exponent using Taylor series, we use the formula: ex = 1 + x + (x2)/2! + (x3)/3! + ... + (xn)/n!, where n is the desired precision. This means we will use n terms in the series to approximate the value of ex.

2. What is the purpose of calculating exponents using Taylor series?

Calculating exponents using Taylor series allows us to approximate the value of ex to a desired precision. This is useful in many fields of science and engineering, where precise calculations are necessary.

3. How do you determine the precision of a Taylor series approximation?

The precision of a Taylor series approximation is determined by the number of terms used in the series. The more terms we use, the closer the approximation will be to the actual value. We can also specify the desired precision by setting a value for n in the formula ex = 1 + x + (x2)/2! + (x3)/3! + ... + (xn)/n!.

4. Are there any limitations to using Taylor series for calculating exponents?

One limitation of using Taylor series for calculating exponents is that it can only approximate real numbers. It cannot be used for complex numbers. Additionally, the series may converge very slowly for some values of x, making it less efficient for larger values.

5. Can Taylor series be used to calculate other mathematical operations?

Yes, Taylor series can be used to approximate many mathematical functions, such as trigonometric functions, logarithms, and square roots. However, the series may need to be modified for each specific function. For example, the Taylor series for calculating logarithms is ln(1+x) = x - (x2)/2 + (x3)/3 - ... + (-1)n+1(xn)/n.

Similar threads

Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
6
Views
2K
Replies
16
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
14K
Replies
4
Views
2K
  • Calculus and Beyond Homework Help
Replies
12
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
Back
Top