当前位置: 首页 > 知识库问答 >
问题:

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

齐兴运
2023-03-14

由此求出两个电荷之间的净力:f1_3+f1_2=fnet

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

取刚求出的加速度,求出速度。V=-(at)是时间间隔(我从步骤6中的方程导出得到方程aver,初始方程是x(0.05)=at+V

这些步骤是我想反复重复的,现在我的间隔,我希望它是1微秒或10e-6,所以从0.0-1.0微秒,0.0-2.0微秒,我希望它存储每个间隔的位置。问题是,它必须使用我在前几步列出的方程重新计算力的值,然后再回去找到位置。我不知道是否应该使用“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;
}        
}

共有1个答案

雍飞雨
2023-03-14

是的,这对于物理模拟是很常见的。通常,您定义一个时间步骤是如何进行的,然后循环整个时间。对于涉及粒子的模拟,通常需要同时存储位置和速度来计算运动。

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] = ...
    }
}

编辑:作为一个例子,我们可以模拟重力。我们可以通过以下操作更改takestep

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

对于你的例子,你需要计算每个粒子的力(我们不需要,因为重力的值已经知道了),然后应用它,类似于我用线性积分的方法。这意味着

velocity = force*change_in_time
position = velocity*change_in_time
 类似资料:
  • 问题内容: 需要将foreach循环中的值存储到数组中,需要帮助。 下面的代码不起作用,仅存储上次尝试的值,但这也不起作用,将不胜感激。 问题答案: 在循环外声明数组,并用于向数组添加项目:

  • 问题内容: 因此,Scala应该和Java一样快。我正在重新研究最初在Java中解决的Scala Project Euler 问题。特别是问题5:“能被从1到20的所有数字均分的最小正数是多少?” 这是我的Java解决方案,需要0.7秒才能在计算机上完成: 这是我对Scala的“直接翻译”,需要103秒(长147倍!) 最后,这是我进行函数式编程的尝试,该过程需要39秒(长55倍) 在Window

  • 场景:我有一个存储过程,它基于两个输入从表中获取数据:一个日期和一个字符串(这是一个列名)。第一个过程是从另一个过程调用的,该过程使用光标循环表中的行,并将每一行传递给第一个过程的字符串(要检查的列名)。我对第二个过程(即直接调用的过程)的输入是日期。 问题:当我单独调用它时,我的第一个过程运行良好。我的第二个过程是抛出一些我不知道如何修复的语法错误。 Obs:我已经在这里检查了关于这个主题的一些

  • 这两个URL之间有区别吗?一个直接指向mp4,然后另一个URL是“下载链接”?有区别吗? 在谷歌云平台中有这样存储文件的选项吗?

  • 问题内容: 我有一个需要很多参数的插入存储过程-其中2个是@ FirstName,@ LastName。我还有一个更新存储过程,该过程需要许多参数-其中2个是@ FirstName,@ LastName。 我想做的是,从插入SP内部完成之后,调用更新SP并将其发送给@ FirstName,@ LastName。 我不知道这样做的正确语法;我试过了: 但我认为这是错误的。 有人可以告诉我怎么写这个电

  • direction(方向) 定义动画的方向。 Accepts Infos 'normal' 正方向动画 'reverse' 反方向动画 'alternate' 往返 anime({ targets: '.dir-normal', translateX: 250, easing: 'easeInOutSine' }); anime({ targets: '.dir-reverse'