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

1. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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...

Java 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.
Last edited by HelloDrChewz; 04-13-2012 at 02:49 AM.  Reply With Quote

2. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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...

Java 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.
Last edited by HelloDrChewz; 04-13-2012 at 01:29 AM.  Reply With Quote

3. Senior Member Join Date
Apr 2012
Posts
211
Rep Power
0

## 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  Reply With Quote

4. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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. :-/  Reply With Quote

5. ## 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?  Reply With Quote

6. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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?  Reply With Quote

7. ## Re: Trying to plot a 1D array vs another 1D array...

How will you draw the graph? Rows and columns of *s or ?  Reply With Quote

8. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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?  Reply With Quote

9. Member Join Date
Apr 2012
Posts
11
Rep Power
0

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

or columns actually... Too many columns...  Reply With Quote

10. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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.  Reply With Quote

11. ## Re: Trying to plot a 1D array vs another 1D array...

What IDE are you using? Read its doc for how to add libraries.  Reply With Quote

12. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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...  Reply With Quote

13. ## Re: Trying to plot a 1D array vs another 1D array...

Good luck. I have not used that package myself.  Reply With Quote

14. Member Join Date
Apr 2012
Posts
11
Rep Power
0

## 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. :-)  Reply With Quote

15. ## Re: Trying to plot a 1D array vs another 1D array...  Reply With Quote

16. Senior Member Join Date
Apr 2012
Posts
211
Rep Power
0

## 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  Reply With Quote

17. ## Re: How do I plot a 1D array vs another? 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  Reply With Quote

18. ## 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?

db  Reply With Quote

19. Member Join Date
Apr 2012
Posts
11
Rep Power
0

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

Yea. I'm new here... I didn't know where to post it. Sorry about that... I was honestly not expecting this kind of response... This community is amazing. I'm used to the trolling type of message boards...

Stephen, I'm following your instructions right now... I'll let you know how it goes. P.S. You guys are awesome! I'm telling everyone I know about this website.  Reply With Quote

20. Member Join Date
Apr 2012
Posts
11
Rep Power
0

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

Oh my god. You guys are awesome! It works like a charm. Thank you Norm, Stephen and Darryl. You guys are life savers! I cannot thank you guys enough.  Reply With Quote

graph from matrix 