我正在使用 mclaurins 系列来计算 arccos(x^2-1) 但是当我将它与 math.acos 的结果进行比较时,它会有所不同。这是我的代码:
public class Maclaurin {
public static int factorial(int fact) {
if(fact==0)
return 1;
return fact*factorial(fact-1);
}
public static void main(String[] args) {
int i, j;
double function = 0, x,result;
x=0;
for (int n = 0; n < 8; n++) {
function=((factorial(2*n))/(Math.pow(4, n)*Math.pow(factorial(n),2)*(2*n+1)))*Math.pow((Math.pow(x, 2)-1),2*n+1);
result=(Math.PI/2)-function;
System.out.println("x= "+x+" y= "+result);
System.out.println("Test "+Math.acos(Math.pow(x, 2)-1));
x+=0.13;
}
}
}
Programm output. test is a value calculated with Math.arccos and it differs from y calculated with mclaurins formula:
x= 0.0 y= 2.5707963267948966
Test 3.141592653589793
x= 0.13 y= 1.7291549939933966
Test 2.9574849820283498
x= 0.26 y= 1.6236496851024964
Test 2.771793621843802
x= 0.39 y= 1.5848621264898726
Test 2.5828078861333155
x= 0.52 y= 1.5725761587226181
Test 2.3885331918392687
x= 0.65 y= 1.5708496332463704
Test 2.1864594293995867
x= 0.78 y= 1.570796415168701
Test 1.9731661516473589
x= 0.91 y= 1.5707963267948972
Test 1.7435543826662978
编辑:计算在 maclaurin 函数中的新代码,我从主函数中调用它。对除前 3 个值外的所有值都有效:maclaurin 包;
public class Maclaurin {
private static double x;
//public static int factorial(int fact) {
// if(fact==0)
// return 1;
// return fact*factorial(fact-1);
//}
public static double factorial(int fact) {
if(fact==0)
return 1;
return fact*factorial(fact-1);
}
public static void main(String[] args)
{
x = 0;
for (int i=0;i<8;i++)
{
maclaurin(x);
x=x+0.14;
}
}
public static void maclaurin(double value){
double function = 0, x, result;
x =value;
for (int n = 0; n < 20; n++) {
function += ((factorial(2 * n)) / (Math.pow(4, n) * Math.pow(factorial(n), 2) * (2 * n + 1)))
* Math.pow((Math.pow(x, 2) - 1), 2 * n + 1);
}
result = (Math.PI / 2) - function;
System.out.println("x= " + x + " y= " + result);
System.out.println("Test " + Math.acos(Math.pow(x, 2) - 1));
}
}