编程计算下列公式的值,y=x-x^3/3!+x^5/5!-x^7/7!+…,精确到第n项,其中n=10,x=2.5
答案:2 悬赏:70 手机版
解决时间 2021-11-21 14:59
- 提问者网友:捧腹剧
- 2021-11-21 10:35
编程计算下列公式的值,y=x-x^3/3!+x^5/5!-x^7/7!+…,精确到第n项,其中n=10,x=2.5
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-11-21 12:01
分析:y=(-1)^(n-1)*x^(2n-1)/(2n-1)! 主要代码如下
int x=2.5,n=10;
double y=0,t=1,t2=1;
for(int i=1;i for(int j=1;j<(2*i-1)+1;j++){
t*=-x; //count x^(2n-1)
t2*=j; //count (2n-1)!
}
i%2==1?y-=t/t2:y+=t/t2; //if n is ood - else +
}追问你能找出它的迭代公式吗 能把完整的代码写出来吗追答double calc(int n, double x){
double y=0,t=1,t2=1;
for(int i=1;i for(int j=1;j<(2*i-1)+1;j++){
t*=-x; //count x^(2n-1)
t2*=j; //count (2n-1)!
}
i%2==1?y-=t/t2:y+=t/t2; //if n is ood - else +
}
return y;
}
void main(){
int n = 10;
double x = 2.5;
printf("x=%3.1lf,n=%d,y=%lf",x,n,calc(n,x));
}
公式不是已经给出了吗?追问error C2018: unknown character '0xac'
error C2146: syntax error : missing ';' before identifier 't2'
error C2065: 't2' : undeclared identifier
?追答double calc(int n, double x){
double y=0,t=1,t2=1;
for(int i=1;i for(int j=1;j<(2*i-1)+1;j++){
t*=-x;
t2*=j;
}
i%2==1?y-=t/t2:y+=t/t2;
}
return y;
}
void main(){
int n = 10;
double x = 2.5;
printf("x=%3.1lf,n=%d,y=%lf",x,n,calc(n,x));
}
int x=2.5,n=10;
double y=0,t=1,t2=1;
for(int i=1;i
t*=-x; //count x^(2n-1)
t2*=j; //count (2n-1)!
}
i%2==1?y-=t/t2:y+=t/t2; //if n is ood - else +
}追问你能找出它的迭代公式吗 能把完整的代码写出来吗追答double calc(int n, double x){
double y=0,t=1,t2=1;
for(int i=1;i
t*=-x; //count x^(2n-1)
t2*=j; //count (2n-1)!
}
i%2==1?y-=t/t2:y+=t/t2; //if n is ood - else +
}
return y;
}
void main(){
int n = 10;
double x = 2.5;
printf("x=%3.1lf,n=%d,y=%lf",x,n,calc(n,x));
}
公式不是已经给出了吗?追问error C2018: unknown character '0xac'
error C2146: syntax error : missing ';' before identifier 't2'
error C2065: 't2' : undeclared identifier
?追答double calc(int n, double x){
double y=0,t=1,t2=1;
for(int i=1;i
t*=-x;
t2*=j;
}
i%2==1?y-=t/t2:y+=t/t2;
}
return y;
}
void main(){
int n = 10;
double x = 2.5;
printf("x=%3.1lf,n=%d,y=%lf",x,n,calc(n,x));
}
全部回答
- 1楼网友:刀戟声无边
- 2021-11-21 12:12
double calc(int n, double x)
{
int i;
double result1 = -1.0;
double result2 = 0.0;
double result = 0;
for(i = 0; i < 2 * n -1; i++)
{
result1 *= -1 * x;
result2 *= i;
}
result = result1 / result2;
return result;
}
int main()
{
int n = 10;
int i = 0;
double x = 1.5;
for(i = 0; i < n; i++)
result += calc(n, x);
}追问运行不出结果 x=2.5 , n=10 答案为0.598472
麻烦再想象 可否交个朋友啊追答你确定答案是0.598472,我算出来的是0.997471.
#include
double calc(int n, double x)
{
int i;
double result1 = 1.0;
int result2 = 1.0;
double result = 0.0;
int sign = -1;
for(i = 1; i < 2 * n; i++)
{
result1 *= x;
result2 *= i;
}
for(i = 1; i < n + 1; i++)
{
sign *= -1;
}
result = sign * result1 / result2;
return result;
}
int main()
{
int n = 2;
int i = 0;
double x = 1.5;
double result = 0.0;
for(i = 1; i < n + 1; i++)
result += calc(i, x);
printf("The result is %lf\n", result);
return 0;
}追问是的 你再想下吧追答你看一下前两项的和是多少,总的和肯定比前两项的和大吧。
{
int i;
double result1 = -1.0;
double result2 = 0.0;
double result = 0;
for(i = 0; i < 2 * n -1; i++)
{
result1 *= -1 * x;
result2 *= i;
}
result = result1 / result2;
return result;
}
int main()
{
int n = 10;
int i = 0;
double x = 1.5;
for(i = 0; i < n; i++)
result += calc(n, x);
}追问运行不出结果 x=2.5 , n=10 答案为0.598472
麻烦再想象 可否交个朋友啊追答你确定答案是0.598472,我算出来的是0.997471.
#include
double calc(int n, double x)
{
int i;
double result1 = 1.0;
int result2 = 1.0;
double result = 0.0;
int sign = -1;
for(i = 1; i < 2 * n; i++)
{
result1 *= x;
result2 *= i;
}
for(i = 1; i < n + 1; i++)
{
sign *= -1;
}
result = sign * result1 / result2;
return result;
}
int main()
{
int n = 2;
int i = 0;
double x = 1.5;
double result = 0.0;
for(i = 1; i < n + 1; i++)
result += calc(i, x);
printf("The result is %lf\n", result);
return 0;
}追问是的 你再想下吧追答你看一下前两项的和是多少,总的和肯定比前两项的和大吧。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯