1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Calculating Exponent Using Taylor Series To Given Precision

  1. Mar 17, 2012 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant 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...

    3. 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.
  2. jcsd
  3. Mar 17, 2012 #2


    User Avatar
    Homework Helper

    Can you show an attempt to calculate the factorial, even if not done in a subroutine, and an attempt to calculate xi?
  4. Mar 17, 2012 #3
    Yeah, here's the code I use for the factorial, in the first part:

    REAL FUNCTION rfactor1(iup1)
    INTEGER, INTENT(IN) :: iup1
    INTEGER :: icurr1
    REAL :: rans1
    DO icurr1=1,iup1
    END DO
    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:

    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
    END DO


    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...
  5. Mar 17, 2012 #4


    User Avatar
    Homework Helper

    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 (Text):

    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 (Text):

    double precision sum, term, x
          x = ???
          sum = 1
          term = 1
          do i = 2,n
              term = term * ???
              sum = sum + term
          end do
    Last edited: Mar 17, 2012
  6. Mar 17, 2012 #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.

  7. Mar 17, 2012 #6
    Yeah, using double precision makes sense to avoid issues where the factorials get large, which they do pretty quickly...
  8. Mar 18, 2012 #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!
  9. Mar 18, 2012 #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.
  10. Mar 18, 2012 #9
    This works:

    DO WHILE (rdiffer2>dx)
    END DO

    Thanks heaps, well done!
  11. Mar 18, 2012 #10


    User Avatar
    Homework Helper

    You can combine the xi and n! calculations into one term:

    Code (Text):

          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: Mar 18, 2012
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook