Java Program for Damped Harmonic Oscillator Solutions | Ptplot Package

In summary, the conversation was about a program to compute and display solutions for a damped harmonic oscillator using JAVA and ptplot package. The instructions were to read in values from the terminal, plot the amplitude vs time, and reprompt for new values. The expert suggested using an infinite while loop with the option to exit the program or continue with new values. They also advised incorporating the different cases within the plotNewSeries method and suggested trying to draw graphs on top of each other within that method.
  • #1
CAF123
Gold Member
2,948
88

Homework Statement


I have to write a JAVA program that will compute and display (via the ptplot package) the solutions for the damped harmonic oscillator (x against t) in some given range for t. The solutions vary depending on how gamma and omega are related. The instructions given were:
1) Read in values of omega, gamma and the number of points to plot from the terminal
2) Calculate and plot the amplitude vs time using the ptplot package
3) Reprompt for new values of omega, gamma and no. of points
4) Add the results of this new calculation to the existing graph as a new series

The Attempt at a Solution


Writing up how to draw the graph is fine, I am just getting a bit confused about how to go about coding the loop (i.e the bit about reprompting for new values)
I remember someone saying they had to enclose the code in a 'while' loop, but I am not sure what the boolean condition would be here.
What I have written so far is something like:
while (some boolean statement) {
// In my program, I have the code that should be under this while statement correctly, here I will just write what I am doing - Ask user for values of omega,gamma and no.of points and parse into double form

if (gamma>2omega) {
//Here I write in the code to draw this graph etc..

After this I get stuck- How to reprompt user for more values?
What is the boolean statement?
How to plot graphs on existing graphs?

Any help here would be greatly appreciated and if anything is unclear, I will explain again.

Many thanks
 
Physics news on Phys.org
  • #2
I would suggest an infinite while loop with one of the following two options:

(1) at the start of your program execution you tell the user some key to press to end the program (say "X" for example) at anytime, and if you read such an input at anytime in your while loop, you exit processing with a System.exit(0); statement

(2) After each graphing operation, you ask the user if they would like to continue and prompt them to enter (Y/n). If "n" then exit the program, if yes, then go to next iteration.

Option 2 would go something like this:

Code:
Scanner input = new Scanner(System.in);
while(true) {
    System.out.println("Please enter Omega");
    processOmegaInput(input);
    System.out.println("Please enter Gamma");
    processGammaInput(input);
    System.out.println("Please enter number of points");
    processPointsInput(input);
    plotNewSeries(omega, gamma, points);
    System.out.println("Would you like to continue? (Y/n)");
    switch(input.next().toLowerCase()) {
      case "y":
        continue;
      case "n":
        System.exit(0);
      default:
        System.out.println("Invalid choice - Exiting program");
        System.exit(0);
    };
}
 
  • #3
What should the (boolean) condition be on the while loop? (I.e where you write while (true)) what should I write in place of true?

Should I incorporate the three cases (three cases being γ< 2ω, γ=2ω, γ>2ω) in the while loop separated by two 'else if' statements.
(to be more clearer, if the first case is not satisfied, go onto the next case ( in an else if )and then the next case (in an else if)
And then only then, after I have written all the code for how to draw the graphs for each of the three cases, do I prompt the user for new values?
I presume then if the user says yes, then the while loop gets run again (is this why we have the while (true) loop?) until the user types no?

Finally, how do I draw the graphs on top of each other? I have a piece of code in my notes about drawing graphs from multidatasets but I am not sure where this should be incorporated into the program.

Many thanks and again, if anything is unclear, I will explain.
 
  • #4
CAF123 said:
What should the (boolean) condition be on the while loop? (I.e where you write while (true)) what should I write in place of true?

What's wrong with simply leaving it as while(true)? What does that do?

Should I incorporate the three cases (three cases being γ< 2ω, γ=2ω, γ>2ω) in the while loop separated by two 'else if' statements.
(to be more clearer, if the first case is not satisfied, go onto the next case ( in an else if )and then the next case (in an else if)

In my above example, you would need to define and implement 4 methods: processOmegaInput, processGammaInput, processPointsInput, and plotNewSeries. To me, the logical place to code for how to plot Series for different input values (like γ< 2ω) would be inside the plotNewSeries method. That is where I suggest you put your if statements or switch statement.
And then only then, after I have written all the code for how to draw the graphs for each of the three cases, do I prompt the user for new values?
I presume then if the user says yes, then the while loop gets run again (is this why we have the while (true) loop?) until the user types no?

With each iteration of the while loop, the user get prompted for new values, the user input gets checked (you will need to write code to do this inside each of the 3 process___input methods, in case the user tries to tell you that Gamma="Steven Harper", for example), and then a new Series gets plotted using the values input. You want the while loop (and the program) to continue so long as the user keeps telling it to continue, and that's what the point of the switch statement in my example.

Finally, how do I draw the graphs on top of each other? I have a piece of code in my notes about drawing graphs from multidatasets but I am not sure where this should be incorporated into the program.
Try incorporating the code in your notes inside the plotNewSeries method and post your complete code if you get stuck.
 
  • #5


As a scientist, it is important to have a clear understanding of what you are trying to achieve with your program and the steps needed to achieve it. In this case, it seems like you are trying to create a program that will plot the solutions for a damped harmonic oscillator using the ptplot package. To do this, you will need to follow the instructions given, which include reading in values for omega, gamma, and the number of points to plot, and then using those values to calculate and plot the amplitude vs time using the ptplot package.

As for the loop, you are correct in thinking that you will need to use a while loop to continuously prompt the user for new values and add them to the existing graph. The boolean statement for the loop could be something like "while user input for new values is 'yes' or 'y'". This way, the loop will continue to run as long as the user wants to input new values.

To plot graphs on existing graphs, you will need to use the addSeries() method from the ptplot package. This method allows you to add a new series (graph) to an existing plot. You can use this method within your loop to continuously add new graphs to the existing plot.

In terms of coding, it may be helpful to break down the steps into smaller tasks and focus on one at a time. For example, start with reading in the values and plotting the first graph, then work on adding the loop and prompting for new values, and finally add the code for plotting new graphs on the existing plot.

Overall, it is important to have a clear understanding of the problem and to break it down into smaller, manageable tasks. Don't be afraid to do some research and consult resources such as documentation for the ptplot package or other similar programs. Good luck with your program!
 

FAQ: Java Program for Damped Harmonic Oscillator Solutions | Ptplot Package

What is Java?

Java is a popular programming language that is used to create software applications for a wide range of devices and systems. It was first released by Sun Microsystems in 1995 and is now maintained by Oracle Corporation.

What is SHM in Java?

SHM stands for Shared Memory and is a mechanism that allows multiple processes to access the same memory space. In Java, SHM can be used to share data between different threads or processes within the same program.

How do I create a program with SHM in Java?

To create a program with SHM in Java, you will need to use the java.nio.MappedByteBuffer class. This class allows you to map a region of shared memory and access it using different threads or processes.

What are the benefits of using SHM in Java?

There are several benefits of using SHM in Java, including improved performance, reduced memory usage, and easier communication between multiple processes. SHM can also help to avoid data duplication and synchronization issues.

Are there any drawbacks to using SHM in Java?

While SHM can offer many benefits, there are some potential drawbacks to consider. For example, if not implemented properly, SHM can lead to race conditions and other synchronization problems. It also requires careful management and can be challenging to debug if issues arise.

Similar threads

Replies
12
Views
2K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
2
Views
1K
Replies
16
Views
13K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
4
Views
1K
Back
Top