用牛顿法求三次函数的全部复根;Java中的s方法

用牛顿法求三次函数的全部复根;Java中的s方法,java,complex-numbers,fractals,newtons-method,Java,Complex Numbers,Fractals,Newtons Method,我到处寻找我能理解的代码,这些代码可以帮助我前进。我已经找到了一个,但我正在努力,所以我希望有人能帮助我 这就是我想要实现的目标: 求解一个三次函数(ax^3+bx^2+cx+d),其中a、b、c和d是填充的 在运行命令行时,通过命令行输入 我需要使用牛顿法找到根和复根。我正在挣扎的代码是这样的,但我不知道这是否有效,我也不知道如何计算所有3个根(即使知道它是否有重数1、2或3) 感谢您的帮助 import java.util.function.Function; public class N

我到处寻找我能理解的代码,这些代码可以帮助我前进。我已经找到了一个,但我正在努力,所以我希望有人能帮助我

这就是我想要实现的目标:

求解一个三次函数(ax^3+bx^2+cx+d),其中a、b、c和d是填充的 在运行命令行时,通过命令行输入

我需要使用牛顿法找到根和复根。我正在挣扎的代码是这样的,但我不知道这是否有效,我也不知道如何计算所有3个根(即使知道它是否有重数1、2或3)

感谢您的帮助

import java.util.function.Function;

public class Newton {
    static double a = Polynom.geta(); // these are to get the input from the class you run from calling this class to solve the roots
    static double b = Polynom.getb();
    static double c = Polynom.getc();
    static double d = Polynom.getd();

    public static void main(String args[]) {
    }

    private Complex[] sqrt(double x, double y) { 
        Complex com = new Complex(x,y);             // Complex is my class that deals with Complexnumbers, com is supposed to get the value of the root in the end
        double tolerance = 1e-11;                   // tolerance for the error
        int iterations = 1, max = 512;
        Complex aa = com.pow(3).multiply(a);        // These put in the values from input to complex values and fill in the cubic function of ax^3+bx^2+cx+d 
        Complex bb = com.pow(2).multiply(b);
        Complex cc = com.multiply(c);
        Complex dd = com.pow(2).multiply(a).multiply(3.0);
        Complex ee = com.multiply(2.0).add(com);
        Complex function = aa.add(bb).add(cc).add(d,0);
        Complex derivative = dd.add(ee);

        for(int k = 0; k<3; k++) {
            while(iterations<max) {
                Complex difference = function.divide(derivative); //difference=fx/dx
                com = com.subtract(difference);
                if (Math.abs(difference.getReal()) < tolerance && Math.abs(difference.getImaginary()) < tolerance)
                    return com; // this is where i get an error atm "Cannot convert from Complex to Complex
                iterations++;
            }
        }
    }
import java.util.function.function;
公共类牛顿{
static double a=Polynom.geta();//这些函数用于从调用该类以求解根的过程中运行的类中获取输入
静态双b=Polynom.getb();
静态双c=Polynom.getc();
静态双d=Polynom.getd();
公共静态void main(字符串参数[]){
}
私有复合体[]sqrt(双x,双y){
complexcom=newcomplex(x,y);//Complex是我处理Complexnumbers的类,com最终应该得到根的值
双公差=1e-11;//错误的公差
int迭代次数=1,最大值=512;
复数aa=com.pow(3).乘法(a);//将输入值与复数值相加,填入ax^3+bx^2+cx+d的三次函数
复数bb=com.pow(2).乘法(b);
复数cc=com.multiply(c);
复数dd=com.pow(2)、乘法(a)、乘法(3.0);
复数ee=com.乘(2.0).加(com);
复变函数=aa.add(bb).add(cc).add(d,0);
复导数=dd.add(ee);

对于(int k=0;kWhy你有一个
Polynom
类?你可以把指数和系数保存在
LinkedHashMap
中。不管怎样,你研究过这个吗?->Polynom类是我运行的类,查找根只是我想要的一部分,所以我创建了一个新类来查找根,并创建了一个用于复数的类,跟随对象o我是一个java初学者,所以我不知道linkedhashmap是什么。你提供的链接说明了如何解决这个问题,但我需要使用牛顿方法。