Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何循环和存储物理方程中的值_Java_Loops_For Loop_Physics - Fatal编程技术网

Java 如何循环和存储物理方程中的值

Java 如何循环和存储物理方程中的值,java,loops,for-loop,physics,Java,Loops,For Loop,Physics,所以我想在这些步骤中做这些方程 q1*q3*k/(r^2)=F1_3(这是介于1和3之间的电力) q2*q3*k/r^2)=F1_2(粒子1和2之间的力) 从那里我可以找到两个电荷之间的净作用力,f1_3+f1_2=fnet 对于净力,我会用a=fnet/m(m是质量)来求加速度。(现在上面的一切我都可以做,但现在我弄糊涂了) 取刚得到的加速度,求出速度。v=-(at)是时间间隔(我从第6步的方程中得到了方程aver,初始方程是x(.05)=at+v 取那个速度和先前的加速度,找到新的位置:x=

所以我想在这些步骤中做这些方程

  • q1*q3*k/(r^2)=F1_3(这是介于1和3之间的电力)

  • q2*q3*k/r^2)=F1_2(粒子1和2之间的力)

  • 从那里我可以找到两个电荷之间的净作用力,f1_3+f1_2=fnet

  • 对于净力,我会用a=fnet/m(m是质量)来求加速度。(现在上面的一切我都可以做,但现在我弄糊涂了)

  • 取刚得到的加速度,求出速度。v=-(at)是时间间隔(我从第6步的方程中得到了方程aver,初始方程是x(.05)=at+v

  • 取那个速度和先前的加速度,找到新的位置:x=1/2at^2+v*t+x

  • x的值成为粒子3的新位置,现在我回到顶部,以计算电力,然后是加速度等,冲洗并重复

  • 这些步骤是我想反复重复的,我希望它是1微秒或10e-6,从0.0-1.0微秒到0.0-2.0微秒,我希望它以每一个间隔存储位置。问题是,它必须再次使用我在前几步中列出的方程重新计算力的值d然后回去找那个职位。我不知道是否应该用“for”但是我不知道怎么去循环,我有扫描器的方法在上面,因为稍后当我完成代码的时候,我想能够输入电荷的位置和大小,然后它会使用方程,每次一步

    我想做的是不可能的吗

    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;
    }        
    }
    

    是的,这在物理模拟中非常常见。通常你定义一个时间步的制作方式,然后在整个时间内循环。对于涉及粒子的模拟,你通常需要存储位置和速度来计算运动

    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] = ...
        }
    }
    
    在你的例子中,你需要计算每个粒子的力(我们不需要,因为重力的值已经知道了),然后用类似于我用线性积分的方法来应用它

    velocity = force*change_in_time
    position = velocity*change_in_time
    

    请注意,
    force
    velocity
    position
    在我看来是可能的。请详细说明您的具体位置好吗?我以前从未真正使用过数组,所以我不确定您的意思是什么?我将在哪里实施我曾经想过的步骤?据我所知,您说我必须是否每年更新位置和速度?我真的不明白你想用这些代码做什么,或者我错过了什么?可能是示例吗?@Cosmik11添加了一个示例。
    velocity = force*change_in_time
    position = velocity*change_in_time