多項式から係数を分離し、別々の方法でそれらを計算してみてください。例:
public class NewClass {
public static void main(String[] args) {
int greatestExponent = 9;
double x = 0.5;
System.out.println(calculate(x,greatestExponent));
}
public static double getCoefficient(int n){
double coff = 1;
for(int i=1; i<n; i++){
if(i%2==0){
coff = coff/i; //if even put it in the denominator
}
else{
coff = coff*i; // else numerator
}
}
return coff/n; // at the end divide with the exponent (ex. x^9 /9)
}
public static double calculate(double x, int expo){
double result = 1;
for (int i = 1; i<= expo; i+=2){
result += (getCoefficient(i)*Math.pow(x, i)); //for each odd exponent calculate the cofficient and x^i
}
return result;
}
}
何を考え出そうとしましたか?これはおそらくあなたをテストするか、何かを教えることを目的とする演習のように見えるので、最初の試行をすべき人はいますか? – Thomas
n項までループします。もし奇数倍であれば、奇数乗算器で除算し、xを逓倍して次の奇数の累乗を同じ数で割った –
そして、私は「i」と何をしようとしていますか?シリーズを分析して、「i」に基づいて式/ステップ定義を作成しようとしましたか? – Thomas