我有点喜欢通过命令行运行参数。我在命令行上实现最后4个参数时遇到问题,因此在命令行上输入的样本将是java newton 2 4 .005 50 1 2 0 5
,其中1 2 0 5
是返回陷阱底部静态双精度中多项式的系数。
它应显示为1x^3 + 2x^2 + 0^2 + 5
。一切似乎都正常,但我无法使args坚持到底,也不知道为什么。如果有人可以帮助我,我已经花了将近10个小时的时间进行研究,而且似乎找不到任何帮助。
import java.util.Scanner;
import java.text.DecimalFormat;
public class newton {
public static void main(String[] args) {
double x0, xnew, xxnew;// Initiating double
double x1, p1;
double fx0, fx1;
double delta, delta1; // amount added to get next iterate
double error; // error estimate
double tol = Double.parseDouble(args[2]);// tolerance (max error)
int i, maxIts, j; // iteration count and maximum number of
// iteraterations made
x0 = Integer.parseInt(args[0]);
x1 = Integer.parseInt(args[1]);
p1 = Integer.parseInt(args[4]);
maxIts = Integer.parseInt(args[3]);
DecimalFormat fmt = new DecimalFormat("0.############");
System.out.println("\n");
System.out.println("Polynomail Root Finder By [Gilbert Jimenez]" + "\n");
System.out.println("Initial Perameters :" + "\n");
System.out.println("P0 : = " + args[0]);
System.out.println("p1 : = " + args[1]);
System.out.println("Tol = " + tol);
System.out.println("Maximum = " + maxIts + "\n");
System.out.println("Polynomial is of order: 4 ");
System.out.println("Terms of polynomial: " + args[4] + "x^3" + "+" + args[5] + "x^2" + "+" + args[6] + "x" + "+"
+ args[7]);
{
// Performing Newton's method
i = 1;
error = 100;
System.out.println("Newtons Method:\t " + "\n");
while (i <= maxIts && error > tol) {
delta = -(f(x0) / fprime(x0));
error = Math.abs(delta);
xnew = x0 + delta;
System.out.println("p" + i + "\t" + fmt.format(xnew));
i++;
x0 = xnew;
}
System.out.println("\n");
System.out.println("Solution found after " + i + " " + "itterations :" + fmt.format(x0) + "\n");
}
{
// Performing
j = 1;
error = 100;
System.out.println("Secant Method:\t " + "\n");
fx0 = f(x0);
while (j <= maxIts && error > tol) {
fx1 = f(x1);
delta1 = (-fx1 * (x1 - x0) / (fx1 - fx0));
error = Math.abs(delta1);
xxnew = x1 + delta1;
System.out.println("p" + j + "\t" + fmt.format(xxnew));
j++;
x0 = x1;
fx0 = fx1;
x1 = xxnew;
}
System.out.println("\n");
System.out.println("Solution found after " + j + " " + "itterations :" + fmt.format(x1) + "\n");
}
}
// function of f
public static double f(double x) {
return (x * x * x - 2.0 * x * x + 0 * x - 5);
}
// derivative of f
public static double fprime(double x) {
return (3.0 * x * x - 4.0 * x);
}
}
最佳答案
假设多项式采用以下形式:ax ^ 3 + bx ^ 2 + c * x + d
所做更改的列表:
将类名从Newton更改为Newton
将多项式系数保存为a,b,c,d
修改函数f()和fprime()以使用a,b,c,d
请尝试看看是否有帮助。
public class Newton {
static int a = 0;
static int b = 0;
static int c = 0;
static int d = 0;
public static void main(String[] args) {
double x0, xnew, xxnew;// Initiating double
double x1, p1;
double fx0, fx1;
double delta, delta1; // amount added to get next iterate
double error; // error estimate
double tol = Double.parseDouble(args[2]);// tolerance (max error)
int i, maxIts, j; // iteration count and maximum number of
// iteraterations made
x0 = Integer.parseInt(args[0]);
x1 = Integer.parseInt(args[1]);
p1 = Integer.parseInt(args[4]);
maxIts = Integer.parseInt(args[3]);
DecimalFormat fmt = new DecimalFormat("0.############");
System.out.println("\n");
System.out.println("Polynomail Root Finder By [Gilbert Jimenez]" + "\n");
System.out.println("Initial Perameters :" + "\n");
System.out.println("P0 : = " + args[0]);
System.out.println("p1 : = " + args[1]);
System.out.println("Tol = " + tol);
System.out.println("Maximum = " + maxIts + "\n");
System.out.println("Polynomial is of order: 4 ");
a = Integer.valueOf(args[4]);
b = Integer.valueOf(args[5]);
c = Integer.valueOf(args[6]);
d = Integer.valueOf(args[7]);
System.out.println("Terms of polynomial: " + a + "x^3" + "+" + b + "x^2" + "+" + c + "x" + "+" + d);
{
// Performing Newton's method
i = 1;
error = 100;
System.out.println("Newtons Method:\t " + "\n");
while (i <= maxIts && error > tol)
{
delta = -(f(x0) / fprime(x0));
error = Math.abs(delta);
xnew = x0 + delta;
System.out.println("p" + i + "\t" + fmt.format(xnew));
i++;
x0 = xnew;
}
System.out.println("\n");
System.out.println("Solution found after " + i + " " + "itterations :" + fmt.format(x0) + "\n");
}
{
// Performing
j = 1;
error = 100;
System.out.println("Secant Method:\t " + "\n");
fx0 = f(x0);
while (j <= maxIts && error > tol)
{
fx1 = f(x1);
delta1 = (-fx1 * (x1 - x0) / (fx1 - fx0));
error = Math.abs(delta1);
xxnew = x1 + delta1;
System.out.println("p" + j + "\t" + fmt.format(xxnew));
j++;
x0 = x1;
fx0 = fx1;
x1 = xxnew;
}
System.out.println("\n");
System.out.println("Solution found after " + j + " " + "itterations :" + fmt.format(x1) + "\n");
}
}
// function of f
public static double f(double x)
{
return (a * x * x * x + b * x * x + c * x + d);
}
// derivative of f
public static double fprime(double x)
{
return (3 * a * x * x + 2 * b * x + c);
}
}