Results 1 to 20 of 20
- 04-13-2012, 01:07 AM #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...
The code is probably really inefficient. But this is my first program outside of Java class.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)); }Last edited by HelloDrChewz; 04-13-2012 at 02:49 AM.
- 04-13-2012, 01:15 AM #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...
The code is probably really inefficient. But this is my first program outside of Java class.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)); }Last edited by HelloDrChewz; 04-13-2012 at 01:29 AM.
- 04-13-2012, 01:36 AM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- 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
- 04-13-2012, 01:39 AM #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. :-/
- 04-13-2012, 02:00 AM #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?If you don't understand my response, don't ignore it, ask a question.
- 04-13-2012, 02:49 AM #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?
- 04-13-2012, 02:53 AM #7
Re: Trying to plot a 1D array vs another 1D array...
How will you draw the graph? Rows and columns of *s or ?
If you don't understand my response, don't ignore it, ask a question.
- 04-13-2012, 02:54 AM #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?
- 04-13-2012, 02:55 AM #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...
- 04-13-2012, 02:56 AM #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.
- 04-13-2012, 02:58 AM #11
Re: Trying to plot a 1D array vs another 1D array...
Read the API doc for the package you downloaded.
What IDE are you using? Read its doc for how to add libraries.If you don't understand my response, don't ignore it, ask a question.
- 04-13-2012, 03:01 AM #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...
- 04-13-2012, 03:05 AM #13
Re: Trying to plot a 1D array vs another 1D array...
Good luck. I have not used that package myself.
If you don't understand my response, don't ignore it, ask a question.
- 04-13-2012, 03:07 AM #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. :-)
- 04-13-2012, 03:09 AM #15
Re: Trying to plot a 1D array vs another 1D array...
Thanks. Glad to have had at least one happy customer today.
If you don't understand my response, don't ignore it, ask a question.
- 04-13-2012, 06:10 AM #16
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- 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 • 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 • 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 #17
Re: How do I plot a 1D array vs another?
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-13-2012, 08:05 AM #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?
I've merged the two threads. Please don't do this again.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-13-2012, 04:37 PM #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.
- 04-13-2012, 06:12 PM #20
Member
- Join Date
- Apr 2012
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
drawing scattered plot from 2D array
By yeahwa in forum Java 2DReplies: 16Last Post: 06-07-2010, 08:38 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks