Lesson12
Re: Lesson12
- 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());
- }
- }
Re: Lesson12
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());
}
}
Re: Lesson12
- 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
- }
- }
Re: Lesson12
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
}
}
Re: Lesson12
The screenshot of ProjectileApp program.
- Attachments
-
- ProjectileApp.png (31.65 KiB) Viewed 4197 times