Web Analytics Made Easy -
StatCounter java: some mathematical problems - CodingForum

Announcement

Collapse
No announcement yet.

java: some mathematical problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • java: some mathematical problems

    i've been trying so long and i could not solve any of the problems (below) that i've been having for some time. if any of you could help out with any of the problems, that'd be absolutely AMAZING.

    1. http://www.sci.usq.edu.au/staff/leighb/graph/Top.html
    that's the website from which i got my plotting codes/classes for a plot that supposedly takes a set of data from a different calculating program (example 3 code is the one where you can load data). i'm looking at the graph code example3 and it's not obvious to me how to call up the data for the graph. is it where it says "DATA"? if the data file is called output.txt would i just substitute "output.txt" for "DATA"?

    2. how would i make it so that my outout.txt file from a calculating program is composed of two columns (so basically a set of data, x coordinates and y coordinates)?

    3. in the following equation, i want P to be varying b/t two given constants Pmax and Pmin. how would i do that?

    public double I0(double Vb, double P, double Pbath) {
    return ((Pbath-P)/Vb);
    }

    that's it for now... wow i'm hopelessly lost... ahh.

    thanks so much for everything.

  • #2
    well let's look at the source code for some answers

    1)
    This is the source code
    Code:
    String data  = getParameter("DATA");
    ...
    data1 = dynamic.loadDataSet(new URL(getDocumentBase(),data), graph);
    This is a snippit of the html page where the applet loads:
    Code:
    <param name=title
           value="Spectrum of a giant elliptical Galaxy in the Virgo cluster">
    <param name=data
           value="elliptical.data">
    As you can see, the String data is really "elliptical.data". So if you replace this or if you place it directly in the loadDataSet part then yes, you will load your data.


    2 You are kind of ambigious on this fact. Is the dataset something other then two columns? What is the dataset after it comes out of the calculating program?
    Maybe you should consider just writing a quick program to open the file and format it to another file the way you want it to be formated.

    3
    If you are calling the function IO several times then you could do this by initiating a boolean variable. So for example

    Code:
    boolean minmax = true;
    double Pmax = 99.99;
    double Pmin = 1.00;
    double Vb = some_double;
    double Pbath = somedouble;
    
    while(true)
    {
      if(minmax)
        IO(Vb, Pmax, Pbath);
      else
        IO(Vb, Pmin, Pbath);
    }
    Now if you wanted it to vary WITHIN the function then you would have to create a static boolean variable in that class and do about the same thing, changing P.

    Hope this help (hope I understood the questions).
    "To iterate is human, to recurse divine." -L. Peter Deutsch

    Comment


    • #3
      thanks so much for those tips.

      1. "As you can see, the String data is really "elliptical.data". So if you replace this or if you place it directly in the loadDataSet part then yes, you will load your data."

      data1 = dynamic.loadDataSet(new URL(getDocumentBase(),data), graph);

      i'm not quite sure how i can replace that above with my .txt datafile (it's ok if the data's in .txt format instead of .data right?)...

      2. "Is the dataset something other then two columns? What is the dataset after it comes out of the calculating program?"

      well basically the calculating program calculates some values based on a value that is also changing. it ties into my third question really; i'm varying the value P from Pmin to Pmax and then i am calculating bunch of I0 values which are based on the P variable. so i'd like the output.txt file to have simply two columns, first column all the P values and the second column R0/Rn's, and R0 values are dependent upon those varying I0 values. right now the following is my code for that but it seems like it won't display P and R0/Rn side by side...

      FileOutputStream out;
      PrintStream p;
      out = new FileOutputStream("output.txt");
      p = new PrintStream( out );
      p.println("P R0/Rn");
      p.println(P + "\r\n" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\r\n");
      p.close();

      3. the below is what i wrote after your suggestion:

      public double I0(double Vb, double P, double Pbath, double Pmax, double Pmin) {
      boolean minmax = true;
      while(true)
      {
      if(minmax)
      return ((Pbath-Pmax)/Vb);
      else
      return ((Pbath-Pmin)/Vb);
      }
      }

      public double R0(double Vb, double I0){
      return (Vb/I0);
      }

      it compiles fine but it won't give me a set of values... it only gives me one pair of values when i want a bunch of pairs. what am i doing wrong?

      thank you so much.

      Comment


      • #4
        i'm not quite sure how i can replace that above with my .txt datafile (it's ok if the data's in .txt format instead of .data right?)...
        Opps! Yes, you can use .txt. As far as the program is concerned it's just going to read it line by line regardless..so it may end up looking something like this:

        Code:
        data1 = dynamic.loadDataSet(new URL(getDocumentBase(),[b]"data.txt"[/b]), graph);
        3. the below is what i wrote after your suggestion:
        opps again. It seems I did not write solid code to you the first time through. Since the boolean variable is not changing you will always hit true, here is the first change. Bolded is where the problem of one set occurs.

        Code:
        public double I0(double Vb, double P, double Pbath, double Pmax, double Pmin) 
        {
          boolean minmax = true; 
          while(true)
          {
            if(minmax)
            {
              minmax = false;
              [b]return ((Pbath-Pmax)/Vb);[/b]
            }
            else
            {
              minmax = true;
              [b]return ((Pbath-Pmin)/Vb);[/b]
            }
          }
        }
        
        public double R0(double Vb, double I0){
        return (Vb/I0);
        }
        The return statments break out of the method completely, taking you out of the loop. Which is okay if you just want to run the method once and then switch it. However if you want to do it this way you'll need to make the boolean variable static so that it's scope is not of just the method but of the entire program. (yay static's). (btw I'm assuming that you are calling the method as many times as you need to and return that one data pair).

        So here is the real solution (as I currently see it ^_^ )
        Code:
        [b]private static boolean minmax = true;[/b]
        public double I0(double Vb, double P, double Pbath, double Pmax, double Pmin) 
        {
          if(minmax)
          {
            minmax = false;
            return ((Pbath-Pmax)/Vb);
          }
          else
          {
            minmax = true;
            return ((Pbath-Pmin)/Vb);
          }
        }
        
        public double R0(double Vb, double I0){
        return (Vb/I0);
        }
        Let me look at the file reader a bit longer before I comment on that. (personally I use filewriter to make my life slightly eaiser).
        "To iterate is human, to recurse divine." -L. Peter Deutsch

        Comment


        • #5
          Okay on sixth read I got what you were doing..

          Code:
          FileOutputStream out;
          PrintStream p;
          out = new FileOutputStream("output.txt");
          p = new PrintStream( out );
          p.println("P R0/Rn");
          p.println(P + "\r\n" + R0(Vb,[b]I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))[/b]/Rn + "\r\n");
          p.close();
          which called the IO thing we've been bugging with this entire time. Well with the P+"\r\n" the P value and the RO/Rn value will be on two different lines

          Code:
          P
          RO/Rn
          I think what you really want is P+"\t"+other stuff so...
          Code:
          FileOutputStream out;
          PrintStream p;
          out = new FileOutputStream("output.txt");
          p = new PrintStream( out );
          p.println("P R0/Rn");
          p.println(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\r\n");
          p.close();
          This code here will take the on P value it has at the time and the other values it has at the time and print it as such

          Code:
          P                 RO/Rn
          So at this point we will have one of the points read, formated and printed out onto the file (as long as I didn't make anymore silly mistakes! hehe )
          "To iterate is human, to recurse divine." -L. Peter Deutsch

          Comment


          • #6
            sorry i wasn't so clear, but it definitely worked. thank you

            #3 is still a bit of a problem though. i'm getting two columns and all that, but there's only one set of values even if i have that varying boolean code in... basically it gives me the resulting value only when P is Pmax. I want to produce, say, 100 data points b/t Pmax and Pmin (which are 4 and 1.171, respectively). how would i do this?

            you've been so much help.

            [[edit1]]

            i figured it out why it'll only give me one set of data... i've previously declared P as a fixed constant. my bad. now that i got rid of it, i'm faced with a situation where i have not declared P anything anywhere, so in this line:

            p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\r\n");

            where P currently stands, i don't know what to substitute for P, since in the boolean code there's no mention of P either:

            private static boolean minmax = true;
            public double I0(double Vb, double Pbath, double Pmax, double Pmin)
            {
            if(minmax) private static boolean minmax = true;
            public double I0(double Vb, double Pbath, double Pmax, double Pmin)
            {
            if(minmax)
            {
            minmax = false;
            return ((Pbath-Pmax)/Vb);
            }
            else
            {
            minmax = true;
            return ((Pbath-Pmin)/Vb);
            }
            }
            {
            minmax = false;
            return ((Pbath-Pmax)/Vb);
            }
            else
            {
            minmax = true;
            return ((Pbath-Pmin)/Vb);
            }
            }

            (i know previously we had double P with other doubles like Vb up there somwhere but i thought it would be unnecessary since we mention nothing of the sort in the actual code itself so i got rid of it.)

            now i'm faced with that problem... what can i do?

            [[edit2]]

            oh and also: in terms of the graphing code, after getting those two columns P and R0/Rn (saved as output.txt) i tried calling it up in the graphing code to graph, P column hopefully being the x values and R0/Rn as y values. i even got rid of the very first line where it's not a set of data... is it because it's in two columns? the elliptical.data that i looked at was also in two columns so i don't think it'd be any problem... i'm a bit confused.
            Last edited by NewAtJava; Aug 1, 2005, 04:05 PM.

            Comment


            • #7
              Yes, on number three you will only get 1 value returned.

              Maybe this?

              Code:
              FileOutputStream out;
              PrintStream p;
              out = new FileOutputStream("output.txt");
              p = new PrintStream( out );
              p.println(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n");
              p.println(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n");
              p.close();
              I've changed the fact that it's doubled and the \r is removed and there is only a \n.

              I'm thinking the \r may have thrown it off as far as graphing is concerned which is why I removed it.

              To be perfectly honest I wouldn't do the writing in this manor. I would use File Writer and BufferedWriter. That's more of a personal preference though and it would go something like this:

              Code:
                BufferedWriter out = new BufferedWriter(new FileWriter("output.txt", true));
                out.write(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n")
              out.write(P + "\t" + R0(Vb,I0(Vb,P,Pbath(G,beta,T0,Tb),Pmax,Pmin))/Rn + "\n")
              out.close();
              But I've heard people not liking that for various reasons (apparently notepad doesn't like the \n character in windows but wordpad loves it).

              Hope this helps some more ^_^
              "To iterate is human, to recurse divine." -L. Peter Deutsch

              Comment


              • #8
                ah the BufferedWriter works too. but either way, it still doesn't solve the problem of plugging in values between Pmax and Pmin for P etc that i listed when editing the response above... not only do i want to plug in Pmax (4.0) and Pmin(1.171) for P, i'd also like the values between them, i.e. 3.0. maybe increments of 0.1, let's say. how would i do that?

                Comment


                • #9
                  incrementing and decrementing is easy with a for loop.

                  Code:
                  for(double i = Pmin; i < Pmax; i+.01)
                  {
                     // Do something with the increment of i
                  }
                  the variable i will then be incremented by .01 until it reaches Pmax. You can use this then to generate different points in your dataset.


                  The boolean code doesn't include P because there is no need to use P in order to generate the RO/Rn equation. P is only used in that one instance that you want to create the point set in the file.

                  I guess I'm kind of confused on the mathmatical part of this (four years of calculus seems to have failed me). You have your Pmin which is the mininmum P value (obviously) and your Max is your Max value. So My new assumption is that P is a function or a variable? If it's a function you can use the incrementation to just plug it back into function P. If it's a variable then I'm unsure what you mean completely.

                  Sorry I'm a bit confused. ^_^
                  "To iterate is human, to recurse divine." -L. Peter Deutsch

                  Comment


                  • #10
                    private static boolean minmax = true;
                    public double I0(double Vb, double Pbath, double Pmax, double Pmin)
                    {
                    if(minmax)
                    {
                    minmax = false;
                    for(double i = Pmin; i < Pmax; i+.01) {
                    return ((Pbath-i)/Vb);
                    }
                    }
                    else
                    {
                    minmax = true;
                    for(double i = Pmin; i < Pmax; i+.01) {
                    return ((Pbath-i)/Vb);
                    }
                    }
                    }

                    that's what i tried to do and it returned me two errors saying that i+.01 wasn't a statement...

                    my P was temporary, if you could say that, and it's just a variable. like x. where i plug in values b/t Pmax and Pmin. basically the equation involves a variable that should be changed from Pmin to Pmax in the increments of say 0.1.

                    thank you so much!!
                    Last edited by NewAtJava; Aug 2, 2005, 12:11 PM.

                    Comment


                    • #11
                      As far as the for loop goes it seems I'm thinking a different language..

                      Do a while loop instead

                      Code:
                      double i = Pmin;
                      while(i < Pmax)
                      {
                         // Let i do it's job
                         i += .01; // <--interval
                      }
                      I think you want to put this in the calling method, not the method itself. So where you call IO to print.

                      Code:
                      FileOutputStream out;
                      PrintStream p;
                      out = new FileOutputStream("output.txt");
                      p = new PrintStream( out );
                      double i = Pmin;
                      while(i < Pmax)
                      {
                        p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),i))/Rn + "\r\n");
                        i += .01;
                      }
                      p.close();
                      And change your IO code to
                      Code:
                      public double I0(double Vb, double Pbath, double i) 
                      {
                        if(minmax)
                        {
                          minmax = false;
                          return ((Pbath-i)/Vb);
                        }
                        else
                        {
                          minmax = true;
                          return ((Pbath-i)/Vb);
                        }
                      }
                      I think
                      "To iterate is human, to recurse divine." -L. Peter Deutsch

                      Comment


                      • #12
                        that works well, except now the output is skipping a line instead of line after line... basically instead of this:

                        1 2
                        3 4
                        5 6

                        it comes out as:

                        1 2

                        3 4

                        5 6

                        for some reason... hmm.

                        i have a different bit of a question... i want to work with some complex numbers for one of the equations written below:

                        public double sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) {
                        return ((-1./(I0*R0))*(1./(2+betaI))*((1-tau_plus/tauI)/omega*tau_plus*i)*((1-tau_minus/tauI)/omega*tau_minus*i));
                        }

                        and the i in there is i in complex numbers, square root of -1. and since java doesn't have a built-in thing for complex numbers, i imported a code found online here:

                        http://www.cs.princeton.edu/introcs/...plex.java.html

                        i saved the code as Complex.java under a folder called classes.

                        i found that i first had to put this line on the top of the Complex.java code to import to my thing:

                        package classes;

                        then i put the import classes.Complex; line on top of my calculating program. then i changed the equation code above to this:

                        public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) { // power-to-current responsivity
                        return ((-1./(I0*R0))*(1./(2+betaI))*((1-tau_plus/tauI)/Complex(1.,omega*tau_plus))*((1-tau_minus/tauI)/Complex(1.,omega*tau_minus)));
                        }

                        cuz i thought that's how i should be using the Complex class, but it didn't work. what am i doing wrong?

                        thank you for everything.

                        Comment


                        • #13
                          Originally posted by NewAtJava
                          that works well, except now the output is skipping a line instead of line after line... basically instead of this:

                          1 2
                          3 4
                          5 6

                          it comes out as:

                          1 2

                          3 4

                          5 6

                          for some reason... hmm.
                          Yeah cause I copy and pasted incorrectly:

                          Code:
                          FileOutputStream out;
                          PrintStream p;
                          out = new FileOutputStream("output.txt");
                          p = new PrintStream( out );
                          double i = Pmin;
                          while(i < Pmax)
                          {
                            p.println(P + "\t" + R0(Vb,I0(Vb,Pbath(G,beta,T0,Tb),i))/Rn + "\n");
                            i += .01;
                          }
                          p.close();
                          is the fix (deleted the \r)

                          public Complex sI(double omega, double I0, double R0, double betaI, double tau_plus, double tau_minus, double tauI) { // power-to-current responsivity
                          return ((-1./(I0*R0))*(1./(2+betaI))*((1-tau_plus/tauI)/Complex(1.,omega*tau_plus))*((1-tau_minus/tauI)/Complex(1.,omega*tau_minus)));
                          }
                          What a statement. I'm not sure what you're doing here to be honest. It looks like you are creating from the constructor complex. The problem is you are doing math function on an Object (which without a default isn't entirly possible). It looks like if you're going to do any multiplication/addition/division with a complex Number you'll need to make everything complient with it (which looks like a huge pain).

                          But if you're going to create a complex object you must remember to use the new operator. My suggestion is to put it into a variable for easy early use, (so
                          Code:
                          Complex num1 = new Complex(1.,omega*tau_plus);
                          Complex num2 = new Complex(1.,omega*tau_minus);
                          Then go ahead through. You may have to consider doing it so that it's like,

                          Code:
                          num1.divide(something)
                          Right about now you're getting into that theoretical math which I don't know that much about. But if you give me the errors it's giving you, I may still be able to help a bit ^_^
                          "To iterate is human, to recurse divine." -L. Peter Deutsch

                          Comment


                          • #14
                            if i were to do this:

                            Complex num1 = new Complex(1.,omega*tau_plus);
                            Complex num2 = new Complex(1.,omega*tau_minus);

                            would that be a separate thing from other public double equations? cuz when i put it separately it's saying that it doesn't recognize omega and such, "cannot resolve symbol" and stuff... where do i define them as doubles?

                            [[edit]]

                            i don't think the Complex.java code that i showed you has any dividing operation... unless i'm blind. which is possible. but i found a more extensive Complex code:

                            http://www.netlib.org/java/
                            (the first item)

                            but i can't figure out how to use this one...
                            Last edited by NewAtJava; Aug 2, 2005, 02:51 PM.

                            Comment


                            • #15
                              Originally posted by NewAtJava
                              if i were to do this:

                              Complex num1 = new Complex(1.,omega*tau_plus);
                              Complex num2 = new Complex(1.,omega*tau_minus);

                              would that be a separate thing from other public double equations? cuz when i put it separately it's saying that it doesn't recognize omega and such, "cannot resolve symbol" and stuff... where do i define them as doubles?
                              You would place these two inside the method. Not outside of it.

                              Code:
                               public Complex times(Complex b) {
                                      Complex a = this;
                                      double real = a.re * b.re - a.im * b.im;
                                      double imag = a.re * b.im + a.im * b.re;
                                      Complex prod = new Complex(real, imag);
                                      return prod;
                                  }
                              Can't you just modify this to make it divide (replace the * with / )? Just curious on that effect. I'll have to read up on the code you just posted for the complex numbers, my computer is having trouble opening it (cause it's made of evil )
                              "To iterate is human, to recurse divine." -L. Peter Deutsch

                              Comment

                              Working...
                              X