Lesson12

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

Lesson12

Post by admin »

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

Re: Lesson12

Post by admin »

  1. package projectileapp;
  2. import org.opensourcephysics.controls.*;
  3. import org.opensourcephysics.frames.*;
  4. public class ProjectileApp extends AbstractSimulation {
  5. PlotFrame plotFrame = new PlotFrame("Time", "x,y", "Position versus time");
  6. Projectile projectile = new Projectile();
  7. PlotFrame animationFrame = new PlotFrame("x", "y", "Trajectory");
  8. public ProjectileApp() {
  9. animationFrame.addDrawable(projectile);
  10. plotFrame.setXYColumnNames(0, "t", "x");
  11. plotFrame.setXYColumnNames(1, "t", "y");
  12. }
  13. public void initialize() {
  14. double dt = control.getDouble("dt");
  15. double x = control.getDouble("initial x");
  16. double vx = control.getDouble("initial vx");
  17. double y = control.getDouble("initial y");
  18. double vy = control.getDouble("initial vy");
  19. projectile.setState(x, vx, y, vy);
  20. projectile.setStepSize(dt);
  21. double size = (vx*vx+vy*vy)/10; // estimate of size needed for display
  22. animationFrame.setPreferredMinMax(-1, size, -1, size);
  23. }
  24. public void doStep() {
  25. plotFrame.append(0, projectile.state[4], projectile.state[0]); // x vs time data added
  26. plotFrame.append(1, projectile.state[4], projectile.state[2]); // y vs time data added
  27. animationFrame.append(0, projectile.state[0], projectile.state[2]); // trajectory data added
  28. projectile.step(); // advance the state by one time step
  29. }
  30. public void reset() {
  31. control.setValue("initial x", 0);
  32. control.setValue("initial vx", 10);
  33. control.setValue("initial y", 0);
  34. control.setValue("initial vy", 10);
  35. control.setValue("dt", 0.01);
  36. enableStepsPerDisplay(true);
  37. }
  38. public static void main(String[] args) {
  39. SimulationControl.createApp(new ProjectileApp());
  40. }
  41. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson12

Post by admin »

Code: Select all

package projectileapp;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.frames.*;
public class ProjectileApp extends AbstractSimulation {
  PlotFrame plotFrame = new PlotFrame("Time", "x,y", "Position versus time");
  Projectile projectile = new Projectile();
  PlotFrame animationFrame = new PlotFrame("x", "y", "Trajectory");

  public ProjectileApp() {
    animationFrame.addDrawable(projectile);
    plotFrame.setXYColumnNames(0, "t", "x");
    plotFrame.setXYColumnNames(1, "t", "y");
  }
  public void initialize() {
    double dt = control.getDouble("dt");
    double x = control.getDouble("initial x");
    double vx = control.getDouble("initial vx");
    double y = control.getDouble("initial y");
    double vy = control.getDouble("initial vy");
    projectile.setState(x, vx, y, vy);
    projectile.setStepSize(dt);
    double size = (vx*vx+vy*vy)/10; // estimate of size needed for display
    animationFrame.setPreferredMinMax(-1, size, -1, size);
  }
  public void doStep() {
    plotFrame.append(0, projectile.state[4], projectile.state[0]); // x vs time data added
    plotFrame.append(1, projectile.state[4], projectile.state[2]); // y vs time data added
    animationFrame.append(0, projectile.state[0], projectile.state[2]); // trajectory data added
    projectile.step(); // advance the state by one time step
  }
  public void reset() {
    control.setValue("initial x", 0);
    control.setValue("initial vx", 10);
    control.setValue("initial y", 0);
    control.setValue("initial vy", 10);
    control.setValue("dt", 0.01);
    enableStepsPerDisplay(true);
  }
  public static void main(String[] args) {
    SimulationControl.createApp(new ProjectileApp());
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson12

Post by admin »

Add the file Projectile (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: Lesson12

Post by admin »

  1. package projectileapp;
  2. import java.awt.*;
  3. import org.opensourcephysics.display.*;
  4. import org.opensourcephysics.numerics.*;
  5. public class Projectile implements Drawable, ODE {
  6. static final double g = 9.8;
  7. double[] state = new double[5]; // {x,vx,y,vy,t}
  8. int pixRadius = 6; // pixel radius for drawing of projectile
  9. EulerRichardson odeSolver = new EulerRichardson(this);
  10. public void setStepSize(double dt) {
  11. odeSolver.setStepSize(dt);
  12. }
  13. public void step() {
  14. odeSolver.step(); // do one time step using selected algorithm
  15. }
  16. public void setState(double x, double vx, double y, double vy) {
  17. state[0] = x;
  18. state[1] = vx;
  19. state[2] = y;
  20. state[3] = vy;
  21. state[4] = 0;
  22. }
  23. public double[] getState() {
  24. return state;
  25. }
  26. public void getRate(double[] state, double[] rate) {
  27. rate[0] = state[1]; // rate of change of x
  28. rate[1] = 0; // rate of change of vx
  29. rate[2] = state[3]; // rate of change of y
  30. rate[3] = -g; // rate of change of vy
  31. rate[4] = 1; // dt/dt = 1
  32. }
  33. public void draw(DrawingPanel drawingPanel, Graphics g) {
  34. int xpix = drawingPanel.xToPix(state[0]);
  35. int ypix = drawingPanel.yToPix(state[2]);
  36. g.setColor(Color.red);
  37. g.fillOval(xpix-pixRadius, ypix-pixRadius, 2*pixRadius, 2*pixRadius);
  38. g.setColor(Color.green);
  39. int xmin = drawingPanel.xToPix(-100);
  40. int xmax = drawingPanel.xToPix(100);
  41. int y0 = drawingPanel.yToPix(0);
  42. g.drawLine(xmin, y0, xmax, y0); // draw a line to represent the ground
  43. }
  44. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson12

Post by admin »

Code: Select all

package projectileapp;
import java.awt.*;
import org.opensourcephysics.display.*;
import org.opensourcephysics.numerics.*;
public class Projectile implements Drawable, ODE {
  static final double g = 9.8;
  double[] state = new double[5]; // {x,vx,y,vy,t}
  int pixRadius = 6;              // pixel radius for drawing of projectile
  EulerRichardson odeSolver = new EulerRichardson(this);
  public void setStepSize(double dt) {
    odeSolver.setStepSize(dt);
  }
  public void step() {
    odeSolver.step(); // do one time step using selected algorithm
  }
  public void setState(double x, double vx, double y, double vy) {
    state[0] = x;
    state[1] = vx;
    state[2] = y;
    state[3] = vy;
    state[4] = 0;
  }
  public double[] getState() {
    return state;
  }
  public void getRate(double[] state, double[] rate) {
    rate[0] = state[1]; // rate of change of x
    rate[1] = 0;        // rate of change of vx
    rate[2] = state[3]; // rate of change of y
    rate[3] = -g;       // rate of change of vy
    rate[4] = 1;        // dt/dt = 1
  }
  public void draw(DrawingPanel drawingPanel, Graphics g) {
    int xpix = drawingPanel.xToPix(state[0]);
    int ypix = drawingPanel.yToPix(state[2]);
    g.setColor(Color.red);
    g.fillOval(xpix-pixRadius, ypix-pixRadius, 2*pixRadius, 2*pixRadius);
    g.setColor(Color.green);
    int xmin = drawingPanel.xToPix(-100);
    int xmax = drawingPanel.xToPix(100);
    int y0 = drawingPanel.yToPix(0);
    g.drawLine(xmin, y0, xmax, y0); // draw a line to represent the ground
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson12

Post by admin »

The screenshot of ProjectileApp program.
Attachments
ProjectileApp.png
ProjectileApp.png (31.65 KiB) Viewed 812 times
Post Reply