Java 试图编写一个程序来计算一个5次多项式和一个常数的根
此类从JPanel GUI读取输入。除非我为每个系数输入1,否则什么都不会发生。然后它只打印“0.0”关于如何让它工作的任何建议?我已经尝试了我能想到的一切Java 试图编写一个程序来计算一个5次多项式和一个常数的根,java,methods,polynomial-math,calculus,bisection,Java,Methods,Polynomial Math,Calculus,Bisection,此类从JPanel GUI读取输入。除非我为每个系数输入1,否则什么都不会发生。然后它只打印“0.0”关于如何让它工作的任何建议?我已经尝试了我能想到的一切 public class PolyRoots extends PolyGUI { private double coefficientToFifthPower; private double coefficientToFourthPower; private double coefficientToThirdPower; private
public class PolyRoots extends PolyGUI {
private double coefficientToFifthPower;
private double coefficientToFourthPower;
private double coefficientToThirdPower;
private double coefficientToSecondPower;
private double coefficientToFirstPower;
private double constant;
private double x;
public double getCoefficientToFifthPower() {
return coefficientToFifthPower;
}
public void setCoefficientToFifthPower(double coefficientToFifthPower) {
this.coefficientToFifthPower = coefficientToFifthPower;
}
public double getCoefficientToFourthPower() {
return coefficientToFourthPower;
}
public void setCoefficientToFourthPower(double coefficientToFourthPower) {
this.coefficientToFourthPower = coefficientToFourthPower;
}
public double getCoefficientToThirdPower() {
return coefficientToThirdPower;
}
public void setCoefficientToThirdPower(double coefficientToThirdPower) {
this.coefficientToThirdPower = coefficientToThirdPower;
}
public double getCoefficientToSecondPower() {
return coefficientToSecondPower;
}
public void setCoefficientToSecondPower(double coefficientToSecondPower) {
this.coefficientToSecondPower = coefficientToSecondPower;
}
public double getCoefficientToFirstPower() {
return coefficientToFirstPower;
}
public void setCoefficientToFirstPower(double coefficientToFirstPower) {
this.coefficientToFirstPower = coefficientToFirstPower;
}
public double getConstant() {
return constant;
}
public void setConstant(double constant) {
this.constant = constant;
}
private double y;
public void readInputCoefficients() {
/* this.coefficientToFifthPower = Integer.parseInt(inputCoefficientFifthPower);
this.coefficientToFourthPower = Integer.parseInt(inputCoefficientFourthPower);
this.coefficientToThirdPower = Integer.parseInt(inputCoefficientThirdPower);
this.coefficientToSecondPower = Integer.parseInt(inputCoefficientSecondPower);
this.coefficientToFirstPower = Integer.parseInt(inputCoefficientFirstPower);
this.constant = Integer.parseInt(inputConstant);*/
}
public double calculateY(double x) {
this.y = this.coefficientToFifthPower * Math.pow(x, 5) + this.coefficientToFourthPower * Math.pow(x, 4) +
this.coefficientToThirdPower * Math.pow(x, 3) + this.coefficientToSecondPower * Math.pow(x, 2) + (this.coefficientToFirstPower * x) + this.constant;
return this.y;
}
public void getTheBounds() {
for (double x = -10.0001; x <= 10.0001; x += .1) {
double y1 = calculateY(x);
double y2 = calculateY(x + .01);
if ((y1 * y2) < 0) {
System.out.println(bisectionMethod(y1, y2));
}
else if((y1 * y2) > 0){
System.out.println(bisectionMethod(y1,y2));
}
}
}
public double bisectionMethod(double a, double b) {
double average;
double root = 0;
double yOfC;
int leaveloop = 0;
for(int x = 0; x <= 10000000; x++) {
average = (a + b) / 2;
yOfC = calculateY(average);
if (Math.abs(yOfC) < 0.0001) {
root = average;
return root;
} else if (yOfC * calculateY(a) > 0) {
a = average;
} else {
b = average;
}
}
System.out.println(root);
return root;
}
}这里是一个简单的程序示例,它有一个
main
,您可以看到每行代码的作用
public class PolyRoots {
private final double[] coeffs; // any number of coefficients.
public PolyRoots(double... coeffs) {
this.coeffs = coeffs;
}
public double calcY(double x) {
double ret = coeffs[0];
for (int i = 1; i < coeffs.length; i++)
ret = ret * x + coeffs[i];
return ret;
}
public static void main(String[] args) {
PolyRoots roots = new PolyRoots(1e-3, -2e-2, 0.1, -1, 5);
for (int i = -10_000; i <= 10_000; i++) {
double x = i / 1000.0;
double y = roots.calcY(x);
System.out.println(x + "\t" + y);
}
}
}
公共类多根{
private final double[]coefs;//任意数量的系数。
公共多根(双…系数){
这个系数=系数;
}
公共双卡(双x){
双ret=系数[0];
对于(int i=1;i 对于(int i=-10000;我让您在IDE中使用调试器逐步检查代码,以确保每一行都符合您的预期?我有,而且似乎没有发现任何问题。我对Java相当陌生,所以我希望逻辑本身是合理的。如果没有问题,请选择a)你没有编写足够的代码来完成所有的工作,或者b)它工作得很好。你能给出一个最简单的例子,它不能产生你想要的结果吗?我已经尝试了我所知道的一切,添加SOP来查找错误,等等,但没有运气…我对这一点非常迷茫
public class PolyRoots {
private final double[] coeffs; // any number of coefficients.
public PolyRoots(double... coeffs) {
this.coeffs = coeffs;
}
public double calcY(double x) {
double ret = coeffs[0];
for (int i = 1; i < coeffs.length; i++)
ret = ret * x + coeffs[i];
return ret;
}
public static void main(String[] args) {
PolyRoots roots = new PolyRoots(1e-3, -2e-2, 0.1, -1, 5);
for (int i = -10_000; i <= 10_000; i++) {
double x = i / 1000.0;
double y = roots.calcY(x);
System.out.println(x + "\t" + y);
}
}
}