Home : Map : Chapter 2 : Java : Tech : Physics :
Demo 2: Harmonic Motion - Predictor-Corrector
JavaTech
Course Map
Chapter 2

Introduction
Essentials
Structure

Keywords
Primitive Types
Comments
Literals
Expressions
Operators
Statements
Casts & Mixing
Strings
Console Output 
   Demo
Exercises

    Supplements
Conditional: if-else
Repetitions
Flow Control

Java vs C/C++
JVM Instructions 1

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

For the this demonstration of the Euler method, we will look at the motion of a mass on spring:

   

The Euler, or predictor, formulas give

   
   

The corrector values thus become:

   
   

The fallowing demo program compares the Predictor-Corrector values to analytical values.

Note: The program also illustrates several more Java language components such as the Math class functions and the modulo operator.

Spring_Applet1.java
(Output goes to browser's Java console.)

public class Spring_Applet1 extends java.applet.Applet
{
  public void init()
  {   
    // Put code between this line
    //-----------------------------------------------

      int N = 100;
      int N_PRINT = 10;
      int N_CYCLES = 2;

      // One cycle per 100 steps
      double dt = 2.0 * Math.PI / N;

      double x = 1.0;
      double v = 0.0;
      double t = 0.0;

      double xe,ve,xc,vc,xa,va;

      for(int i=0; i < N_CYCLES*N; i++)
      {
        t += dt;

        // Euler Method == Predictor
        xe = x + v * dt;
        ve = v - x * dt;

        // Corrector
        xc = x + 0.5 * (v + ve) * dt;
        vc = v - 0.5 * (x + xe) * dt;

        // Analytical values for k/m = 1.0;
        xa = Math.cos((i+1)*dt);
        va = - Math.sin((i+1)*dt);

        // Compare to analytical values
        if((i % N_PRINT) == 0)
        {
         double xdif = Math.abs(xc - xa);
         System.out.println("x error = "+xdif);
         double vdif = Math.abs(vc - va);
         System.out.println("v error = "+vdif);
        }

        // Use Corrected values for next point.
        x = xc;
        v = vc;
      }

    //-----------------------------------------------
 
  // and this line.
  }

  // Paint message in Applet window.
  public void paint(java.awt.Graphics g)
  {
g.drawString("Spring_Applet1",10,20); }
}

Application version:
Spring_App1.java

 

The output of the program will show gradually increasing discrepancies of the numerical values from the analytical values as the cycles continue.

Exercise: Modify the Spring demo program to carry out many cycles, e.g. 20 or 100. Does the discrepancy with the analytical value become more and more significant?

Latest update: Dec.12.2003

            Tech
Arithmetic Ops
Math Class
More on Integers
FP : Overview
FP : Java  
  
Demo 1
More Mix/Cast
  Demo 2
Exercises

           Physics
Differential Eq.
Euler Method
  
Demo 1
Predictor-Corrector
  
Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.