Javaコードについてサポートが必要です。私が達成しようとしているのは、多項式の各ステップのサイズを計算することです。double s = (b-a)/nsteps;
作成される多項式の入力は、次数、係数、の開始値x
、の停止値x
、およびステップ数です。テストを実行しようとすると、出力はと0
のためx
になりy
、コードに何が欠けているのかわかりません。これがどのように機能するかについての私の実行テストですが、私の結果は次x
のとおりy
です0
:
Enter degree:2
Enter coefficient 2:1
Enter coefficient 1:0
Enter coefficient 0:0
f(x) = 1.0x^2 + .0x^1 + 0.0
Enter initial x:0
Enter final x:10
Enter number of steps:20
x = 0.0; f(x) = 0.0
x = 0.5; f(x) = 0.25
x = 1.0; f(x) = 1.0
x = 1.5; f(x) = 2.25
x = 2.0; f(x) = 4.0
x = 2.5; f(x) = 6.25
x = 3.0; f(x) = 9.0
x = 3.5; f(x) = 12.25
x = 4.0; f(x) = 16.0
x = 4.5; f(x) = 20.25
x = 5.0; f(x) = 25.0
x = 5.5; f(x) = 30.25
x = 6.0; f(x) = 36.0
x = 6.5; f(x) = 42.25
x = 7.0; f(x) = 49.0
x = 7.5; f(x) = 56.25
x = 8.0; f(x) = 64.0
x = 8.5; f(x) = 72.25
x = 9.0; f(x) = 81.0
x = 9.5; f(x) = 90.25
x = 10.0; f(x) = 100.0
これが私のJavaコードです。
import java.util.*;
public class PolyAreaTwo{
//method evalpoly Horner's rule
public static double evalpoly(double[] c, double x) {
int n = c.length - 1;
double y = c[n];
for (int i = n - 1; i >= 0; i--) {
y = c[i] + (x * y);
}
return y;
}
//main method
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n;
double a, b;
int nsteps;
//input degree
System.out.print("Enter degree of polynomial:");
n = in.nextInt();
//input n+1 coefficients
double[] c = new double[n+1];
for (int i=n; i>=0; i--) {
System.out.print("Enter coefficent " + i + ":");
c[i] = in.nextDouble();
}
for (double d : c) {
System.out.print(" x ^ " + d);
}
//input starting value x = a
System.out.println("Enter starting x: ");
a = in.nextDouble();
//input stopping value x = b
System.out.print("Enter stop x: ");
b = in.nextDouble();
//input number of steps between starting x and stopping x
System.out.print("Enter steps: ");
nsteps = in.nextInt();
//calculate size of each step
double s = (b-a)/nsteps;
int steps = 0;
//loop to call the evalpoly method
for (double x = a; x <= b; x += s) {
double y = evalpoly(c, x);
System.out.println("x ="+x+ " , y ="+y);
}
}
}