Lesson11

Post Reply
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Lesson11

Post by admin »

Create the project FallingParticleODEApp.
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson11

Post by admin »

  1. package fallingparticleodeapp;
  2. import org.opensourcephysics.controls.*;
  3. import org.opensourcephysics.numerics.*;
  4. public class FallingParticleODEApp extends AbstractCalculation {
  5. public void calculate() {
  6. // gets initial conditions
  7. double y0 = control.getDouble("Initial y");
  8. double v0 = control.getDouble("Initial v");
  9. // creates ball with initial conditions
  10. FallingParticleODE ball = new FallingParticleODE(y0, v0);
  11. // creates ODE solver
  12. ODESolver solver = new Euler(ball); // note how particular algorithm is chosen
  13. // sets time step dt in the solver
  14. solver.setStepSize(control.getDouble("dt"));
  15. while(ball.state[0]>0) {
  16. solver.step();
  17. }
  18. control.println("final time = "+ball.state[2]);
  19. control.println("y = "+ball.state[0]+" v = "+ball.state[1]);
  20. }
  21. public void reset() {
  22. control.setValue("Initial y", 10); // sets default input values
  23. control.setValue("Initial v", 0);
  24. control.setValue("dt", 0.01);
  25. }
  26. public static void main(String[] args) { // creates a calculation control structure for this class
  27. CalculationControl.createApp(new FallingParticleODEApp());
  28. }
  29. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson11

Post by admin »

Code: Select all

package fallingparticleodeapp;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.numerics.*;
public class FallingParticleODEApp extends AbstractCalculation {
  public void calculate() {
    // gets initial conditions
    double y0 = control.getDouble("Initial y");
    double v0 = control.getDouble("Initial v");
    // creates ball with initial conditions
    FallingParticleODE ball = new FallingParticleODE(y0, v0);
    // creates ODE solver
    ODESolver solver = new Euler(ball); // note how particular algorithm is chosen
    // sets time step dt in the solver
    solver.setStepSize(control.getDouble("dt"));
    while(ball.state[0]>0) {
      solver.step();
    }
    control.println("final time = "+ball.state[2]);
    control.println("y = "+ball.state[0]+" v = "+ball.state[1]);
  }
  public void reset() {
    control.setValue("Initial y", 10); // sets default input values
    control.setValue("Initial v", 0);
    control.setValue("dt", 0.01);
  }
  public static void main(String[] args) { // creates a calculation control structure for this class
    CalculationControl.createApp(new FallingParticleODEApp());
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson11

Post by admin »

Add the file FallingParticleODE (java application) to the project.
Add osp.jar to the project.
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson11

Post by admin »

  1. package fallingparticleodeapp;
  2. import org.opensourcephysics.numerics.*;
  3. public class FallingParticleODE implements ODE {
  4. final static double g = 9.8;
  5. double[] state = new double[3];
  6. public FallingParticleODE(double y, double v) {
  7. state[0] = y;
  8. state[1] = v;
  9. state[2] = 0; // initial time
  10. }
  11. public double[] getState() { // required to implement ODE interface
  12. return state;
  13. }
  14. public void getRate(double[] state, double[] rate) {
  15. rate[0] = state[1]; // rate of change of y is v
  16. rate[1] = -g;
  17. rate[2] = 1; // rate of change of time is 1
  18. }
  19. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson11

Post by admin »

Code: Select all

package fallingparticleodeapp;
import org.opensourcephysics.numerics.*;
public class FallingParticleODE implements ODE {
  final static double g = 9.8;
  double[] state = new double[3];
  public FallingParticleODE(double y, double v) {
    state[0] = y;
    state[1] = v;
    state[2] = 0; // initial time
  }
  public double[] getState() { // required to implement ODE interface
    return state;
  }
  public void getRate(double[] state, double[] rate) {
    rate[0] = state[1]; // rate of change of y is v
    rate[1] = -g;
    rate[2] = 1;        // rate of change of time is 1
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson11

Post by admin »

The screenshot of FallingParticleODEApp program.
Attachments
FallingParticleODEApp.png
FallingParticleODEApp.png (12.48 KiB) Viewed 564 times
Post Reply