University of Nevada Las Vegas
Howard R. Hughes College of Engineering
School of Computer Science
My Home Page

Runge Kutta Files

  1. Computation of y when dy/dx = y2 and y = -1 when x = 1, using RK4.
    • The code shown is of the function which computes y(x+h) given y(x) and given that dy/dx = y2. The rest of the program is not shown.
    • Each run computes values of y between x = 1 and x = 2, for intervals of length h.
    • This problem can be solved in closed form: the solution is y = -1/x. Thus, the final value of y should be -0.5
    • The first run shows the case when h = 0.2.
    • The first run shows the case when h = 0.1.
    • The first run shows the case when h = 0.04.
    • Fragment of code and results of computation.
  2. Computation of the orbit of a planet around the Sun. The initial position of the Sun is at the origin, and the planet begins at (1,0), with an initial velocity of (0,1). The gravitational constant has been adjusted so that the planet's orbit will be circular.
    • The closed form solution is that, at time t, the planet will be located at (cos(t), sin(t)) and its velocity will be (-sin(t), cos(t)).
    • In each of the five runs, we consider the time interval from t = 0 to t = 10. For each run, we print the parameters at intervals of 0.2; there are thus fifty-one lines of output.
    • The first entries in each output line give the position, the third and fourth the two components of velocity, the fifth the angular momentum alpha, the cross product of the radius vector and the velocity, which theoretically cannot change. The next three lines are the kinetic energy, defined to be half the square of the speed, the potential energy, defined to be the negative reciprocal of the distance to the Sun, and the total energy, which is the sum of the kinetic and potential energies. The total energy cannot change. Since the orbit is circular, neither the kinetic energy nor the potential energy will change.
    • The time to complete one orbit is 2 pi, approximately 7.28. Thus, the planet should end up in the third quadrant.
    • Letting h = 0.1:
      input file
      output file
    • Letting h = 0.004:
      input file
      output file
    • Letting h = 0.001:
      input file
      output file
    • Letting h = 0.0001:
      input file
      output file
    • Letting h = 0.00001:
      input file
      output file
  3. We now give the planet an initial velocity of (0,0.5). The orbit will be an ellipse, and the period will be shorter than before. The planet will complete slightly more than 3 and a half orbits.
    • Note that the angular momentum is 0.5, while the total energy is -0.875. Theoretically, neither of these quantities can change.
    • Letting h = 0.00001:
      input file
      output file
  4. Why do the two planetary examples have such great error compared to the first example? because