求sin的:参考下 #includestdio.h void main() { double x,a,b,sum=0; printf("请输入x的弧度值:\n"); scanf("%lf",x); int i,j,count=0; for(i=1;;i+=2) { count++; a=b=1; for(j=1;j=i;j++) { a*=x; b*=(double)j; } if(a/b0.0000001) break; else { if(count%2==0) sum-=a/b; else sum+=a/b; } } printf("%lf\n",sum); }
成都创新互联公司 - 成都服务器托管,四川服务器租用,成都服务器租用,四川网通托管,绵阳服务器托管,德阳服务器托管,遂宁服务器托管,绵阳服务器托管,四川云主机,成都云主机,西南云主机,成都服务器托管,西南服务器托管,四川/成都大带宽,机柜大带宽,四川老牌IDC服务商
这是辛普森积分法。
给你写了fun_1( ),fun_2(),请自己添加另外几个被积函数。
调用方法 t=fsimp(a,b,eps,fun_i);
a,b --上下限,eps -- 迭代精度要求。
#includestdio.h
#includestdlib.h
#include math.h
double fun_1(double x)
{
return 1.0 + x ;
}
double fun_2(double x)
{
return 2.0 * x + 3.0 ;
}
double fsimp(double a,double b,double eps, double (*P)(double))
{
int n,k;
double h,t1,t2,s1,s2,ep,p,x;
n=1; h=b-a;
t1=h*(P(a)+P(b))/2.0;
s1=t1;
ep=eps+1.0;
while (ep=eps)
{
p=0.0;
for (k=0;k=n-1;k++)
{
x=a+(k+0.5)*h;
p=p+P(x);
}
t2=(t1+h*p)/2.0;
s2=(4.0*t2-t1)/3.0;
ep=fabs(s2-s1);
t1=t2; s1=s2; n=n+n; h=h/2.0;
}
return(s2);
}
void main()
{
double a,b,eps,t;
a=0.0; b=3.141592653589793238; eps=0.0000001;
// a definite integral by Simpson Method.
t=fsimp(a,b,eps,fun_1);
printf("%g\n",t);
t=fsimp(a,b,eps,fun_2);
printf("%g\n",t);
// ...
printf("\n Press any key to quit...");
getch();
}
#include
#include
double integral(double(*fun)(double x),double a,double b,int,n){
double s,h,y;
int i;
s=(fun(a)+fun(b))/2;
h=(b-a)/n; /*积分步长*/
for(i=1;in;i++)
s=s+fun(a+i*h);
y=s*h;
return y;/*返回积分值*/
}
double f(double x){
return(x*sinx) /*修改此处可以改变被积函数*/
}
int main(){
double y;
y=integral(f,1.0,2.0,150);/*修改此处可以改变积分上下限和步数,步长=(上限-下限)/步数*/
printf("y=%f\n",y);
return 0;
}
int main()
#include stdio.h
#include math.h
#define PI (acos(-1))
#define STEP (1e-6)
double func(double x);
double inte(double up,double down,double func(double));
int main(void)
{
double up,down;
printf("%lf%lf",up,down);
printf("%lf\n",inte(1,0,func));
return 0;
}
double func(double x)
{
return 1+cos(PI*x);
}
double inte(double up,double down,double func(double))
{
double sum;
for(sum=0;down=up;down+=STEP)
{
sum+=func(down)*STEP;
}
return sum;
}
求解含有三角函数的定积分c语言程序∫(1+cosπx)dx
#include stdio.h
#include math.h
#define PI (acos(-1))
#define STEP (1e-6)
double func(double x);
double inte(double up,double down,double func(double));
int main(void)
{
double up,down;
printf("%lf%lf",up,down);
printf("%lf\n",inte(1,0,func));
return 0;
}
double func(double x)
{
return 1+cos(PI*x);
}
double inte(double up,double down,double func(double))
{
double sum;
for(sum=0;down=up;down+=STEP)
{
sum+=func(down)*STEP;
}
return sum;
}