java - Pendulum simulation only performing one period -
i making simulation of pendulum, performs 1 swing before sending close random positions bob at. essentially, not go backwards.
i have tried change direction using goingforward boolean, still doesnt work.
public class animationpane extends jpanel { // start changeable variables private double startangle = -60.0; // degrees private double mass = 1; // kilogrammes private int radius = 10; // m private double gravity = 9.80665; // m/s^2 // on earth: 9.80665 // end changeable variables private bufferedimage ball; private bufferedimage rope; private int pointx = 180; private int pointy = 50; private double endangle = math.abs(startangle); // absolute value of startangle private double angle = startangle; // current angle private double circum = (2 * math.pi * radius); // m private double distance = 0; // m private double velocity = 0; // m/s private double totalenergy = ((radius) - (math.cos(math.toradians(angle)) * radius)) * gravity * mass + 0.00001; private double previouse; private int xpos = 0; // program private int ypos = 0; // program private boolean goingforward = true; private double height = 0; public animationpane() { try { ball = imageio.read(new file("rsz_black-circle-mask-to-fill-compass-outline.png")); timer timer = new timer(100, new actionlistener() { @override public void actionperformed(actionevent e) { double anglerad = math.toradians(math.abs(angle)); double potentiale = ((radius) - (math.cos(anglerad) * radius)) * gravity * mass; double pe = new double(potentiale); height = (radius - (math.cos(anglerad) * radius)); double kinetice = totalenergy - pe; if (kinetice <= 0 || angle >= endangle) { if (goingforward == true) { goingforward = false; } else { goingforward = true; } kinetice = 0.1; angle = 60; } velocity = math.sqrt(2 * kinetice / mass); double ratio = distance / circum; if (goingforward == true) { distance = distance + (velocity / 10); angle = startangle + (360 * ratio); } else { distance = distance - (velocity / 10); angle = startangle - (360 * ratio); } double angles = math.toradians(angle); double xdouble = math.sin(angles) * (radius * 10); double x = new double(xdouble); xpos = x.intvalue() + 150; double ydouble = math.cos(angles) * (radius * 10); double y = new double(ydouble); ypos = y.intvalue() + 50; repaint(); } }); timer.setrepeats(true); timer.setcoalesce(true); timer.start(); } catch (ioexception ex) { } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.drawline(xpos + 20, ypos + 20, pointx, pointy); g.drawimage(ball, xpos, ypos, this); } }
i appreciate getting work. thank you
i can't follow code. if had thought problem is, distance
variable, , consequent use in ratio
: seems defined being 0 when pendulum starts. when switch direction of pendulum, think needs rebased zero... somehow.
you should separate out gui code simulation. can make print out x , y coordinates while debugging. instance, can write values csv file, can visualize them in matlab (or whatever) ensure simulation looks right on time.
i make 2 suggestions simulation:
- don't model in terms of energy, scalar quantity, meaning have use additional state "direction flag" model way going;
- you going need additional state variable. storing x , y redundant, since these can derived directly angle (and r, although constant). modelling second-order system, need second state variable models movement of system @ instant, position. example, model angle , angular velocity on time.
and, of course, don't mess around degrees - stick radians :)
(on matter of velocity, variable of name speed - don't know direction moves in, , information highly relevant dynamics of system).
the derivation of method using angle , angular velocity quite straightforward, although maths rusty enough caution use without checking carefully!
the rotational form of newton's second law of motion is:
torque = moment of inertia * angular acceleration
- the torque on bob given
-mgr sin angle
(m
mass,g
gravity,r
length of pendulum,angle
angular displacement of pendulum vertical). minus sign because force tends return bob vertical; - the moment of inertia of point mass
mr^2
- reasonable approximation if pendulum long compared radius of bob.
therefore angular acceleration -g sin theta / r
. should simple harmonic motion - acceleration proportional distance equilibrium (for small theta, sin theta
approximately theta
) , directed towards it.
you can put numerical scheme simulate pendulum. example, using euler's method:
new angle = old angle + dt * old angular velocity new angular velocity = old angular velocity vel - dt * g * sin(angle) / r
where dt
time step.
you can, of course, use higher-order methods runge-kutta reduce numerical errors in simulation, (slightly) more involved.
Comments
Post a Comment