1

so i want to do these equations in these steps

  1. q1*q3*k/(r^2)= F1_3(this is the electric force between 1 and 3)

  2. q2*q3*k/r^2)= F1_2(force between particle 1 and 2)

  3. from there i can find the net force between the two charges with f1_3+f1_2=fnet

  4. with the net force i would then find the acceleration using a=fnet/m (with m being the mass. (now everything above i was okay with doing but now heres where i get confused)

  5. take the acceleration just found and find velocity. v=-(at) being time intervals (i got that equation aver deriving it from the equation in step 6 and the initial equation was x(.05)=at+v

  6. take that velocity and previous acceleration and find new position: x=1/2at^2+v*t+x

  7. the value for x becomes the new position for particle 3 and now i go back to to the top in order to calculate electric force then acceleration etc rinse and repeat.

Those steps is what i would like to repeat over and over now my intervals I wanted it to be 1 micro second or 10e-6 so from 0.0-1.0 microsecond 0.0-2.0 microseconds and i want it to store the position at every interval. the thing is it would have to recalculate the values of the force using the equations i listed in the first few steps again and then go back to find the position. i don't know if should use the "for" to loop it but i don't know how to really go about it. i have the scanner method at the top cause later on when I'm done with the code i wanted to be able to input the position of the charges and the magnitude of them and then it would use the equations but one step at a time.

Is what I'm trying to do not possible?

import java.util.Scanner;
import javax.swing.JFrame;
public class Firstgui {

private static Scanner in = new Scanner(System.in);

public static void main(String[] args) {
    //declare variables
    double postionq1= 0.0; // position of q1 is at origin i just put it here for reference 
    double distanceq3_q1=.01;  //q3 is placed between q1 and q2
    double distanceq3_q2= .014-distanceq3_q1; //distance from q3 to q2
    double q1=5e-6;
    double q2=-4e-6;
    double q3=-2e-6;
    double mq1=3e-5;
    double k= 8.99e9;
    double F1_3 = Force(q1, q3, k, distanceq3_q1);
    double F2_3= -Force(q2, q3, k, distanceq3_q2);
    double Fnet=F1_3+F2_3;  
    System.out.println(F1_3);
    System.out.println(F2_3);
    System.out.println(Fnet);
    System.out.println("particle 3 position from 0.0-1micro-seconds is  " + position(acceleration(Fnet,mq1), 0.000001 , velocity( 0.000001 , acceleration(Fnet,mq1)),.01));
    // this print line above is the final the position of q3 a 1 microsecond
    //now with that value that it prints how would i use that for the next
    //position of q3 and recalculate the fnet then acceleration etc.
}   
public static double Force(double q1, double q2, double k, double r) { 
    double electricforce=(q1*q2*k)/(Math.pow(r, 2));
    return electricforce;
}
public static double acceleration(double f, double m) {
        double acell=f/m;
        return acell;
}
public static double position(double a, double t, double v, double x ) {
    double postion=.5*a*(t*t)+(v*t)+x; // a- acceleration through out out this time interval is constant 
    return postion;
}
public static double velocity(double t, double a) {
    double v=-(t*a); // a- acceleration through out out this time interval is constant 
    return v;
}        
}
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
Cosmik11
  • 25
  • 1
  • 7

1 Answers1

1

Yes, it is very common for physics simulations. Usually you define how one time step is made, and then you loop over all the whole time. For simulations involving particles, you usually need to store both position and velocity to calculate the motion.

public static void main(String[] args) {
    int nbrSteps = 1000;
    // Store the data
    PointArray points = // The data would look like this. Particle at index 0 corresponds with the data {x0, y0, z0}
                           0: {x0, y0, z0},
                           1: {x1, y1, z1},
                           ...
    VelocityArray velocities = // And here, very similarly
                                  0: {vx0, vy0, vz0},
                                  1: {vx1, vy1, vz1},
                                  ...                     
    for (int i = 0; i < nbrSteps; i++) {
        takeStep(points, velocities);
        // You probably want to record the simulation somehow. Either you save your 
        // data to disk, or you render it on the screen. Both calls could be put here.
    }
}

public static void takeStep(PointArray points, VelocityArray velocities) {
    for (int i = 0; i < points.length(); i++) {
        const double timestep = 1e-6;
        // Here you implement the steps you described, and update position and velocity accordingly
        velocities[i] = ...
        points[i] = ...
    }
}

Edit: As an example we could simulate gravity. We could change takeStep by this

public static void takeStep(PointArray points, VelocityArray velocities) {
    for (int i = 0; i < points.length(); i++) {
        const double timestep = 1e-6;
        // Doing something simple, lets simulate gravity
        const double gravity = -9.82;
        velocities[i].y += f_gravity*timestep; 
        points[i] = velocity[i]*timestep;
    }
}

For your case, you would need to calculate the force for each particle (that we didn't need as the value of gravity is already known) and then apply it similarly to how I did it with linear integration. That means that

velocity = force*change_in_time
position = velocity*change_in_time

Note here that force, velocity and position are vectors.

pingul
  • 3,351
  • 3
  • 25
  • 43