我试图用泰勒近似来评估1.89的sin的近似值。我将输出与Math.sin(x)的值进行了比较;然而,在大约14个术语之后,我的价值偏离很大并且变得错误。我尝试了x的较小值(< 0.5)的近似值,并且这些值相匹配。使用20项计算sin(x)的近似值
我只是想弄清楚为什么在Mac OSx上使用崇高和通过bash编译的Java偏离了真正的输出。
public class Test {
public static void main (String[] args) {
double x = 1.89;
double sinx = 0;
int n = 20;
int countOdd = 1;
int counter = 1;
int result = 0;
int value = 0;
while (countOdd <= n) {
if (counter%2 != 0) {
// Even term odd number
if (countOdd%2 == 0) {
sinx = sinx - (Math.pow(x,counter)/(double)factorial(counter));
System.out.println(" counter even odd term = " + countOdd);
countOdd++;
System.out.println(" sinx = " + sinx);
}
// Odd term odd number
else {
sinx = sinx + (Math.pow(x,counter)/(double)factorial(counter));
System.out.println(" counter odd odd term = " + countOdd);
countOdd++;
System.out.println(" sinx = " + sinx);
}
}
// Update the result and reset the value
//sinx = sinx + value;
//value = 0;
System.out.println(" counter = " + counter);
counter++;
}
System.out.println(" sinx = " + sinx);
System.out.println(" sinx from math library = " + Math.sin(x));
}
/** calcutes and returns n!
@param n : a positive integer number
@return n!
*/
public static int factorial(int n)
{
// your code goes here
int result = 1; // if n = 0, while loop is by passed and 0 is returned
while (n >= 1) {
result = result * (n);
n--;
}
return result;
}
}
2017-08-25
Diante
+2
尝试打印出''n'的'factorial'函数的结果。 –
+0
@AndyTurner,感谢您的建议,我将运行一个循环,并比较以查看函数是否按预期工作,我尝试了单点值并在初始停止 –