Lesson11
Posted: Thu Sep 03, 2020 3:00 pm
Create the project FallingParticleODEApp.
Easy English Study
https://easy-english-study.com/forum/
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());
}
}
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
}
}