PHYS 252

Always hand in:
  1. written solutions to any questions
  2. a paper print-out of well-commented code. Include a multiline comment at the top of your code with (i) the assignment name, (ii) your name, and (iii) the date you handed in all elements of the assignment
  3. paper print-out of output (graph or text)
  4. also, e-mail me (y-lithwick@northwestern.edu) the code with your name and the exercise number in the subject line

Assignment #2
[5 points, due 2pm, April 3]

    1. Write a program that calculates n!, the factorial of n. Output 8!
    2. Now output n! for n=0,...,25
    3. For large n, there is an approximation to n!, given by n! S(n), where S(n) is called Stirling's approximation, and is given by . Plot the fractional accuracy of Stirling's approximation as a function of n. It will be clearer if you plot the y-axis on a log scale. How high in n can you go and what prevents you from going further?

      [Helpful tip for evaluating S(n): The math.h header file contains all kinds of useful math functions and utilities that you can use in this code. For example, pow(x,y) =xy, and M_PI = 3.141... Note: for M_PI in Visual C++, we need to include the line #define _USE_MATH_DEFINES before #include < math.h >]

      [Another helpful tip: as you code, check each step by running test cases. For math, you can compare test cases against the output of other programs, such as Mathematica or the Wolfram Alpha website, or Google, Maple, Matlab, Octave, etc. ]

    1. Write a function called integral(n,deltax) that numerically evaluates the following integral with stepsize deltax, . Use this function to output I(n) for n=0,..,10. What can you say about I(n)?
    2. Plot the relative error in your numerical approximation to I(0.5) as a function of deltax. You should use a log-log plot. What deltax do you need to ensure 0.1% accuracy?
  1. [This question is not for credit:] Continue working through the online C tutorial.