Wednesday, June 8, 2011

Plotting a Sine Curve

Reusing the Wheel
We are going to use the Visualizer in the previous article, Plotting Graph

All we have to do is to prepare a new data file named data.txt, which will contain the values of sin(x) for x=0,15,30,45,60... (degree).

A few lines of the data file is shown, you may use a line editor to calculate the value of sin(x) up to 360 degree. Or you may use an excel spread sheet to generate the data.


Automatic data generation
If you don't like to use a spread sheet program, you may write a simple java program to generate the data. A simple example is shown below :

* File :
* Author :
* Description :
*   Calculate y=sin(x) for x=0,15,30,..., 360
* Tested with : JDK 1.6
class DataWriter
  public static void main(String[] args) throws Exception
    double pi_over_180 = 3.141592654/180;
    for (double degree=0; degree<=360; degree+=15)
      double radian = degree*pi_over_180;
      double sine = Math.sin(radian);
Of course you would need to redirect the program output :
java DataWriter > data.txt

Plotting the Curve

Once you have data.txt ready, you may invoke the Visualizer class to plot the curve. The Visualizer class is defined in the article Plotting Graph

java Visualizer

No comments:

Post a Comment