# Trying to plot a 1D array vs another 1D array...

• 04-13-2012, 01:07 AM
HelloDrChewz
Trying to plot a 1D array vs another 1D array...
I'm taking Java 1 right now, so I'm not very knowledgeable... That being said, I wrote some code for a physics problem, and I need to graph the results. The results are already in a one dimensional array. Preferably, I would graph the results as a scatter plot with cos(1.5*x) under it or something... I've been looking for a way to do it online, but it seems that everywhere I look, they're trying to get me to buy something. Does Java not have a way to do this already?

So far what I have is...

Code:

```public void rungeKutta()     {         y  = 1;         h  = .0001;         z  = 0;         yf = 1.0*Math.PI/1.5;         g  = Math.cos(1.5* yf);         counter = 0;                 while (t < yf)         {             double k1 = h*(-2.25*(y));             double m1 = h*(z);             double k2 = h*(-2.25*(y + m1/2));             double m2 = h*(z + k1/2);             double k3 = h*(-2.25*(y + m2/2));             double m3 = h*(z + k2/2);             double k4 = h*(-2.25*(y + m3));             double m4 = h*(z + k3);                         z += (k1 + 2*k2 + 2*k3 + k4)/6.0;             y += (m1 + 2*m2 + 2*m3 + m4)/6.0;                         double arry[] = new double [arr1.length +1];             double arrt[] = new double [arr2.length +1];             for(int i=0; i< arr1.length; i++)             {                 arry[i] = arr1[i];                 arrt[i] = arr2[i];             }             arry[arr1.length] = y;             arrt[arr2.length] = t;             arr1 = arry;             arr2 = arrt;                         t += h;             counter += 1;         }         System.out.println("Period of function: \t" + t + "\nNumber of iterations: \t" + counter +                 "\nY value: \t\t" + y + "\nError : " + Math.abs(g-y));     }```
The code is probably really inefficient. But this is my first program outside of Java class.
• 04-13-2012, 01:15 AM
HelloDrChewz
How do I plot a 1D array vs another?
I'm taking Java 1 right now, so I'm not very knowledgeable... That being said, I wrote some code for a physics problem, and I need to graph the results. I have the results in an array already. Preferably, I would graph the results as a scatter plot with cos(1.5*x) under it or something... I've been looking for a way to do it online, but it seems that everywhere I look, they're trying to get me to buy something. Does Java have a way to do this without me buying something extra?

So far what I have is...

Code:

```public void calc2() { y = 1; h = .0001; z = 0; yf = 2.0*Math.PI/1.5; g = Math.cos(1.5* yf); counter = 0; while (t < yf) { double k1 = h*(-2.25*(y)); double m1 = h*(z); double k2 = h*(-2.25*(y + m1/2)); double m2 = h*(z + k1/2); double k3 = h*(-2.25*(y + m2/2)); double m3 = h*(z + k2/2); double k4 = h*(-2.25*(y + m3)); double m4 = h*(z + k3); z += (k1 + 2*k2 + 2*k3 + k4)/6.0; y += (m1 + 2*m2 + 2*m3 + m4)/6.0; double arry[] = new double [arr1.length +1]; double arrt[] = new double [arr2.length +1]; for(int i=0; i< arr1.length; i++) { arry[i] = arr1[i]; arrt[i] = arr2[i]; } arry[arr1.length] = y; arrt[arr2.length] = t; arr1 = arry; arr2 = arrt; t += h; counter += 1; } System.out.println("Period of function: \t" + t + "\nNumber of iterations: \t" + counter + "\nY value: \t\t" + y + "\nError : " + Math.abs(g-y)); }```
The code is probably really inefficient. But this is my first program outside of Java class.
• 04-13-2012, 01:36 AM
shall
Re: How do I plot a 1D array vs another?
I'm quite a newbie at java also.

Have you looked at JFreeChart (JFreeChart). According to the website It does scatter plots.

Stephen
• 04-13-2012, 01:39 AM
HelloDrChewz
Re: How do I plot a 1D array vs another?
Yea... How do I import that and use it? I've only imported things like javax.swing.JOptionPane etc. :-/
• 04-13-2012, 02:00 AM
Norm
Re: Trying to plot a 1D array vs another 1D array...
Can you explain what you mean by "plot" and array vs an array?

What does the posted code output and what is wrong with that output and what should the output look like?
• 04-13-2012, 02:49 AM
HelloDrChewz
Re: Trying to plot a 1D array vs another 1D array...
Well, the posted code outputs the y value of the function at the end of the loop, but stores an array of y values along the way... I would like to have a graph (scatter plot) of the data in the array. I've created another array of constant increment so that if I to plot like y vs x, I'll have two arrays of equal length and dimension to set as x and y. Does that make sense?
• 04-13-2012, 02:53 AM
Norm
Re: Trying to plot a 1D array vs another 1D array...
How will you draw the graph? Rows and columns of *s or ?
• 04-13-2012, 02:54 AM
HelloDrChewz
Re: Trying to plot a 1D array vs another 1D array...
I think that might be too many rows. Java doesn't have a native graphing utility?
• 04-13-2012, 02:55 AM
HelloDrChewz
Re: Trying to plot a 1D array vs another 1D array...
or columns actually... Too many columns...
• 04-13-2012, 02:56 AM
HelloDrChewz
Re: Trying to plot a 1D array vs another 1D array...
I downloaded JFreeChart, but I don't know how to import the classes or anything.
• 04-13-2012, 02:58 AM
Norm
Re: Trying to plot a 1D array vs another 1D array...
What IDE are you using? Read its doc for how to add libraries.
• 04-13-2012, 03:01 AM
HelloDrChewz
Re: Trying to plot a 1D array vs another 1D array...
Ok... I'm using netbeans... I'll look for the API doc and go from there...
• 04-13-2012, 03:05 AM
Norm
Re: Trying to plot a 1D array vs another 1D array...
Good luck. I have not used that package myself.
• 04-13-2012, 03:07 AM
HelloDrChewz
Re: Trying to plot a 1D array vs another 1D array...
Thank you, Norm... You are awesome for helping noobs like me. You are appreciated. :-)
• 04-13-2012, 03:09 AM
Norm
Re: Trying to plot a 1D array vs another 1D array...
• 04-13-2012, 06:10 AM
shall
Re: How do I plot a 1D array vs another?
I am using the Netbeans IDE 7.1.1.

I played around with it a little and was able to plot a scatter plot using some code I found on the internet (www.jfree.org &bull; View topic - scatter plot chart using JChart api(jCharts-0.7.5.jar))

I made the program work in Netbeans by doing the following:

1. Download jfreechart-1.0.14 (JFreeChart - Browse /1. JFreeChart at SourceForge.net) and jcommon-1.0.17 (JFreeChart - Browse /3. JCommon at SourceForge.net)
2. Unzip the folders to the NetBeans 7.1.1 directory (or whatever version of Netbeans you are using.
3. In the Netbeans IDE select Tools/Libraries/ and click on Class Libraries in the tree view. Use the New Library button to add two new libraries: jfreechart-1.0.14 And jcommon-1.0.17. For each of them select the source directory, e.g. ...jfreechart-1.0.14\source.
4. In the Netbeans IDE, Create a new Java Application using File->New Project (call the project "ScatterPlotDemo").
5. Add the code found at www.jfree.org &bull; View topic - scatter plot chart using JChart api(jCharts-0.7.5.jar).
NOTE: don't remove the package line at the top of the generated Java application file that was created by Netbeans.
6. Right click on Libraries in the project tab of the Netbeans IDE and select Add JAR/Folder. Pick the ...\NetBeans 7.1.1\jfreechart-1.0.14\lib\jfreechart-1.0.14.jar. Do the same thing for ...\NetBeans 7.1.1\jcommon-1.0.17\lib\jcommon-1.0.17.jar
7. Run the program using Run->Run Main Project in the Netbeans IDE.

Tell me if you were able to get it to work.

Stephen
• 04-13-2012, 07:36 AM
DarrylBurke
Re: How do I plot a 1D array vs another?
Quote:

Originally Posted by shall
2. Unzip the folders to the NetBeans 7.1.1 directory (or whatever version of Netbeans you are using.

1. Don't add jars or anything else inside any program's own folders. A program update could wipe them out. Unzip the download anywhere on your hard disk, so long as you keep track of where you've placed it for reference in step 6.

2. All the details are moot if HelloDrChewz doesn't use NetBeans.

db
• 04-13-2012, 08:05 AM
DarrylBurke
Re: Trying to plot a 1D array vs another 1D array...
Why was this double posted? And why did you continue the discussion in each thread as if that was the only discussion on the topic? Don't you have any respect for the people who are taking time out to help you?