Exercise 1: Warming up

  1. Describe computer hardware and software you may need to work efficiently as a physicist. Explain your selection. Evaluate probable cost.
  2. Install a C/C++ compilers on your computer (if you don't have one). See the list of available compilers on the "Software for Computational Physics" web page.
  3. Write a program to calculate the diameter, circumference, and area of a circle with a given radius. If you have troubles with your code you may use a sample program circle.cpp
  4. Write a program to solve the quadratic equation ax2 + bx + c = 0 by using the quadratic formula to obtain real roots.
  5. Extra credit for 420, required for 520. Modify the quadratic equation program to allow for complex roots.


Exercise 2: Roots of Nonlinear Equations

  1. Write programs to find a single root of a function f(x) on an interval [a,b] using the bisectional method and/or the false position method. Test you programs on an equation that you may easily solve analytically.
  2. Write programs that implement the Newton's method and/or the method of secants to find a root of a single variable function f(x) around some value of x. Test you programs on an equation that you may easily solve analytically.
  3. Apply the programs developed above to find a single root for
    (a) f(x) = exp(x)*ln(x) - cos(x2) = 0 between x = 1.0 and x = 4.0
    (b) f(x) = ln(x2+2)*cos(x)+sin(x) = 0 between x = 0.0 and x = 4.0
    (c) f(x) = exp(x) - sin(pi*x/3) = 0 between x = -5.0 and x = 2.0, where pi=3.1415926...
    (d) f(x) = x2 - 6x +9 = 0 on [-5.0,+5.0]
    Analyze advantages and disadvantages of the implemented methods. Report how many iterations did it take to get a tolerance of about 1.0e-6.
  4. Write a program that implements the brute force method (with bisectional or/and Newton's methods inside) to find multiple roots of a function f(x) on [a,b]. Find roots for
    (e) f(x) = ln(x2+2)*cos(x)+sin(x) = 0 between x = -10.0 and x = 10.0
    (f) f(x) = x3 - 5x2 + 7x - 3 = 0 on [-10.0, + 10.0]
    (g) f(x) = sin(x2)*cos(x+1)/(x2+1) = 0 on [+1.0,+6.0].

Submit: programs (electronically) and your report (carbon copy)

Solutions


Exercise 3: Interpolation

  1. Write a program that implements the first order (linear) interpolation. Test your program on a simple example.
  2. Write a program (or you may use a program from the Web, or from the C/C++ library page) that implements n-point Lagrange interpolation. Treat n as an input parameter.
  3. Calculate values of following functions in 11 uniform points in [0.0,5.0] interval.
    f(x) = sin(x2/1.5)
    f(x) = ecos(x)
    f(x) = 1.0/[(x2-2)2+1.0]
    Apply the polinomial interpolation to calculate values of the functions in 101 uniform points in [0.0,5.0] interval. Study quality of n-point Lagrange interpolation by plotting graphs and by calculating the average deviation of interpolated values from f(x) functions.
  4. Extra credit for 420, required for 520. Apply a spline interpolation code from the C/C++ library page for functions above.

Submit: programs (electronically) and your report (carbon copy)

Solutions: f(x) = sin(x2/1.5), f(x) = ecos(x) , f(x) = 1.0/[(x2-2)2+1.0], and a table.


Exercise 4: Integration

  1. Write programs that calculate an integral with a given integrand f(x) in the region [a, b] by the trapezoid approximation, and the Simpson's rule. (you may use examples from the lecture notes, or from the C/C++ library page) .
  2. Evaluate following integrals

    using the trapezoid approximation, and the Simpson's rule. Study the convergence of results as the number of intervals increases (you may start from n=4 and increase number of intervals by 2 (n=n*2) on the each next integration) . Which of the integrals tend to be more difficult to integrate (require many function evaluations). Explain.
    You may study the sensitivity of results for the last integral to small changes in the upper limit
  3. Use quanc8.cpp program (see example dquanc8.cpp) to calculate integrals above. Compare the efficiency of the program to the trapezoid approximation, and the Simpson's rule programs.
  4. Evaluate numerically 2-D integrals using twice one of the programs (trapezoid, Simpson's or quanc8)

  5. Extra credit for 420, required for 520. The integral definition of the error function is

    Calculate a table of erf (x) for x = 0.0, 0.1, 0.2, .... 1.9, 2.0. Compare your table with published values.
  6. Extra credit. Write a program that calculates an integral from f(x) in the region [a, b] using Gauss quadartures for 24 points (the lecture notes have an example for 8 points). Coefficients for Gauss quadratures (up to 96 points) can be found in Abramowitz and Stegun "Handbook of Mathematical Functions", or on the web.
    Calculate the integrals in the part 2, and study the efficiency and accuracy of the Gauss method for these integrals.

Submit: programs (electronically) and your report (carbon copy)

Solutions


Exercise 5: Random Numbers and Integration

  1. Study quality of a built-in random number generator "rand": Here is an example how to generate random numbers with rand() in C++ (random1.cpp)
    # Evaluate 3-rd moment of the random number distribution
    # Evaluate near-neighbor correlation
    # Plot 2D distribution for two random sequences xi and yi
    # Plot 2D distribution for correlation (xi, xi+5)
    How do the 3-rd moment and near-neighbor correlation depends on the number of random numbers in your study? You may start from 4 random numbers and increase by 2 on each step.
  2. Use the Monte Carlo integration (mean value method) to evaluate 1D integrals from homework "Integration". Study dependences of your results on number of random points
  3. Evaluate 6-D integral using the Monte Carlo integration
  4. Extra credit for 420, required for 520. Write a program that generates a non-uniform set of random numbers using von Neuman rejection or Metropolis sampling. Apply the program for the normal distribution
  5. Extra credit for 420, required for 520. Find a good RNG program from available libraries. Run it and test it.

Submit: programs (electronically) and your report (carbon copy)

Solutions


Exercise 6: Ordinary Differential Equations

  1. Write a program that numerically solves an initial value problem for a first order ODE using the explicit Euler method, the modified Euler method (predictor-corrector), and the 4-th order Runge-Kutta method.
  2. Solve following ODEs by the explicit Euler method, the modified Euler method (predictor-corrector), and the 4-th order Runge-Kutta method

    Compare the methods and study effects of the step size.
  3. Population growth of many species can be modeled as

    where N is the population, aN represents the birthrate, and bN^2 represents the death rate due to various causes (limited resources, disease, …). If N(0)=1,000, a=0.1, and b=0.0002, calculate N(t) for t=0.0 to 100.0 years
  4. Consider a predator-prey model with rabbits and foxes. The following system of differential equations (Lotka-Volterra model) describes the change in populations of rabbits (r) and foxes (f)

    where kr is the prey birth rate, kfr is prey death proportionality constant, kf is the predator death rate, and krf is the predator birth fraction. If r(0)=100 and f(0)=15, kr=2.0, kf=1.0, kfr=0.02, krf=0.01, calculate the population of rabbits and foxes as a function of time from t=0.0 till t=12.0.

Submit: programs (electronically) and your report (carbon copy)