Results 1 to 20 of 20
  1. #1
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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 03:49 AM.

  2. #2
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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 02:29 AM.

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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

  4. #4
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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. :-/

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

  6. #6
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

  8. #8
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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?

  9. #9
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

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

    or columns actually... Too many columns...

  10. #10
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

  12. #12
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

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

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

  14. #14
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

    Default 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. :-)

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

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

  16. #16
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How do I plot a 1D array vs another?

    Quote Originally Posted by shall View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

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

  20. #20
    HelloDrChewz is offline Member
    Join Date
    Apr 2012
    Posts
    11
    Rep Power
    0

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

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  2. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  4. drawing scattered plot from 2D array
    By yeahwa in forum Java 2D
    Replies: 16
    Last Post: 06-07-2010, 09:38 PM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •