另一种输出方式



我想知道是否有办法获得特定的输出。对于我的代码,我使用Newton-Raphson方法来求解这三个方程。我必须在某个迭代中显示它的根。

正如你从代码中看到的,我的输出是"在COUNTER迭代后,根是root。"现在,在运行程序得到答案后,我在代码中键入根和计数器。有没有一种方法可以在"+counter+"迭代后得到类似"的输出根是"+root"。"?换句话说,我如何让程序找到根并打印它,而不必手动输入它和计数器?

public static void main(String[] args)
{
    //x^3 + x^2 + 1
    //2x^3 - 2x^2 - 2
    //3x^3 + 3x^2 + 3
    newrap1();
    newrap2();
    newrap3();
}
public static double func1(double x)
    {
        double f1;
        f1 = Math.pow(x, 3) + Math.pow(x, 2) + 1;
        return f1;
    }
public static double func2(double x)
    {
        double f2;
        f2 = 2*Math.pow(x, 3) - 2*Math.pow(x, 2) - 2;
        return f2;
    }
public static double func3(double x)
    {
        double f3;
        f3 = 3*Math.pow(x, 3) + 3*Math.pow(x, 2) + 3;
        return f3;
    }
public static double der1(double x)
    {
        double d1;
        d1 = 3*Math.pow(x, 2) + 2*x;
        return d1;
    }
public static double der2(double x)
    {
        double d2;
        d2 = 6*Math.pow(x, 2) - 4*x;
        return d2;
    }
public static double der3(double x)
    {
        double d3;
        d3 = 9*Math.pow(x, 2) + 6*x;
        return d3;
    }
public static void newrap1()
    {
        double x = 100;
        for (int i = 0; i < 30; i++)
        {
            double diff;
            diff = func1(x)/der1(x);
            if (diff == 0) return;
            x -= diff;
            System.out.println(Math.floor(x * 1e6) / 1e6);
        }
        System.out.println("The root is -1.465572 after 20 iterations.");
        System.out.println();
    }
public static void newrap2()
    {
        double x = 100;
        for (int i = 0; i < 30; i++)
        {
            double diff;
            diff = func2(x)/der2(x);
            if (diff == 0) return;
            x -= diff;
            System.out.println(Math.floor(x * 1e6) / 1e6);
        }
        System.out.println("The root is 1.465571 after 15 iterations.");
        System.out.println();
    }
public static void newrap3()
    {
        double x = 100;
        for (int i = 0; i < 30; i++)
        {
            double diff;
            diff = func3(x)/der3(x);
            if (diff == 0) continue;
            x -= diff;
            System.out.println(Math.floor(x * 1e6) / 1e6);
        }
        System.out.println("The root is -1.465572 after 20 iterations.");
        System.out.println();
    }

这是当前输出

66.556258
44.260755
29.39754
19.489335
12.884605
8.482183
5.547478
3.589458
2.277446
1.382632
0.729147
0.100537
-4.269108
-2.99942
-2.190119
-1.719717
-1.511997
-1.467533
-1.465575
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
-1.465572
The root is -1.465572 after 20 iterations.
66.778557
44.631345
29.867195
20.025493
13.466126
9.09625
6.188412
4.259889
2.993429
2.186424
1.717784
1.511383
1.467482
1.465574
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
1.465571
The root is 1.465571 after 15 iterations.
66.556258
44.260755
29.39754
19.489335
12.884605
8.482183
5.547478
3.589458
2.277446
1.382632
0.729147
0.100537
-4.269108
-2.99942
-2.190119
-1.719717
-1.511997
-1.467533
-1.465575
-1.465572
-1.465572
-1.465572
The root is -1.465572 after 20 iterations.

一些一般注意事项:

  • 你正在打印一条固定的消息,你正在进行3次30次迭代(而不是你在消息中写的15次、20次和30次)。这个for (int i = 0; i < 30; i++)总是一样的
  • 我想这个程序的全部精神是迭代直到收敛,所以使用2个变量。可以存储以前的值,也可以存储当前的值。比较这两个变量,你可以成像是否已经收敛和break循环。因为比较不会给出确切的0值(double s不准确),所以使用阈值e,在该阈值下,您的值被视为相等
  • 从这个意义上说,您可以使用while循环使其收敛,然后退出循环,或者使用for命令使用预定义数量的循环,当它在给定迭代中收敛时也可以使用break
  • 至于你表达的主要问题,你可以在每次迭代中存储根估计,而不是在每个循环中打印它。除非你想看到收敛的过程
  • 例如,将double finalRoot = Math.floor(x * 1e6) / 1e6放入代码中,您可以在循环完成后使用它进行打印。不过,在循环之前定义它,以便在循环完成后可以访问

希望能给你一个大致的想法。

最新更新