首页 > 解决方案 > 找到电势为零的所有点并绘制它

问题描述

public class physics2 {
    public static void main(String[]args){
        double k=9*10^9;       // Constant k
        double q=6*10^(-9);   // Charge of q
        double Q= -1 * 10^(-9);    // Charge of Q
        int Pqx = 0; // X coordinate of charge q
        int Pqy = 0; // Y coordinate of charge q
        int PQx = 10; // X coordinate of charge Q
        int PQy = 0; // Y coordinate of charge Q


        for (double Px = 7.10102; Px <= 16.8989 ; Px+=0.00001) {
            for (double Py = -8.98; Py <= 8.98; Py+=0.00001) {

                double Vq = (k * q) / Math.sqrt(Math.pow((Px-Pqx),2)+Math.pow((Py-Pqy),2));  // Electric Potential of charge q

                double VQ = (k * Q) /  Math.sqrt(Math.pow((Px-PQx),2)+Math.pow((Py-PQy),2));  // Electric Potential of charge Q

                while (Vq==-VQ){
                    System.out.println("["+Px+","+Py+"]");
                }}

        }


    }
}

坐标系中有两个电荷。第一个在点 (0,0) 处,具有 +6 纳库仑。另一个在点 (10,0) 并具有 -1 纳库仑电荷。主题是找到并绘制所有电势为零的点。我写了那个,但它运行了很长时间,我无法得到任何输出。我哪里错了?我看不见。

标签: javaphysics

解决方案


首先尝试替换while (Vq==-VQ)if (Vq==-VQ). 我认为你的程序在那里陷入了无限循环。这应该可以解决没有打印的问题。

如果程序仍然很慢,那是由于您处理的点数较多。你应该以某种方式减少这个数字,也许首先尝试接近这两个点的点。

最后,这里很好地解释了如何比较 doubles


推荐阅读