c语言编程- 微积分程序
原题在这个链接里http://hi.baidu.com/170418878/blog/item/08691054bdc70652574e0002.html现在要使用tra...
原题在这个链接里 http://hi.baidu.com/170418878/blog/item/08691054bdc70652574e0002.html
现在要使用trapezoidal rule 求下列函数的积分值,
trapezoidal rule 如下,
(不能贴图片,贴个链接) 请用c语言编出,求下列积分值得程序, 并且 用 n=10 , 100 , 1000 时的值 和 exact solution 做比较 展开
(不能贴图片,贴个链接) 请用c语言编出,求下列积分值得程序, 并且 用 n=10 , 100 , 1000 时的值 和 exact solution 做比较 展开
2个回答
展开全部
辛普森法
#include <stdio.h>
#include <math.h>
#define F(X) (4.0/(1+X*X))
static float EPS = 1.0E-14;
static int COUNT=1;
static double a=0.0, b=1.0;
static double M[32],T[32],S[32],C[32],R[32],E[32];
double GETM(int K)
{ unsigned long j,n=1;
double x=0,y=0,step=0;
for(j=0;j<K;j++) n*=2;
step = (b-a)/n;
x = a+step/2;
for(j=0;j<n;j++){y+=F(x);x+=step;}
return (y*step);
}
Simpson(float EPS)
{ int k=0;
T[0]= (b-a)*(F(a)+F(b))/2.0;
S[0]=T[0];
E[0]=1.0E10;
for(k=0;k<20;k++)
{ M[k] = GETM(k);
T[k+1]=(M[k]+T[k])/2.0;
S[k+1]=(4.0*T[k+1]-T[k])/3.0;
COUNT++;
E[k+1]=fabs(S[k+1]-S[k]);
if(k<3)continue;
if(E[k+1]<EPS)break;
}
return;
}
ShawResult()
{ int k;
system("cls");
printf("\n K M[K] T[K] S[K]");
printf(" E[K]");
printf("\n-------------------------------------------");
printf("-----------------------------------");
for(k=0;k<COUNT;k++)
printf("\n%2d %20.15lf%20.15lf%20.15lf%12.4e",k,M[k],T[k],S[k],E[k]);
printf("\n--------------------------------------------");
printf("-----------------------------------");
getch();
system("cls");
return;
}
SaveResult()
{ int k;
FILE * fp;
fp=fopen("Simpson.htm","w");
if(!fp) return;
fprintf(fp,"<html><head></head>");
fprintf(fp,"<body bgcolor = #006699 text = #FFff00>");
fprintf(fp,"<pre><font size=\"6\">");
fprintf(fp,"\n K M[K] T[K] S[K]");
fprintf(fp," E[K]");
fprintf(fp,"\n----------------------------------------------");
fprintf(fp,"----------------------------------");
for(k=0;k<COUNT;k++)
fprintf(fp,"\n%2d %20.15f%20.15f%20.15f%12.4le",k,M[k],T[k],S[k],E[k]);
fprintf(fp,"\n----------------------------------------------");
fprintf(fp,"----------------------------------");
fprintf(fp,"</pre></font></body>");
fclose(fp);
return;
}
main()
{ Simpson(EPS);
ShawResult();
SaveResult();
}
#include <stdio.h>
#include <math.h>
#define F(X) (4.0/(1+X*X))
static float EPS = 1.0E-14;
static int COUNT=1;
static double a=0.0, b=1.0;
static double M[32],T[32],S[32],C[32],R[32],E[32];
double GETM(int K)
{ unsigned long j,n=1;
double x=0,y=0,step=0;
for(j=0;j<K;j++) n*=2;
step = (b-a)/n;
x = a+step/2;
for(j=0;j<n;j++){y+=F(x);x+=step;}
return (y*step);
}
Simpson(float EPS)
{ int k=0;
T[0]= (b-a)*(F(a)+F(b))/2.0;
S[0]=T[0];
E[0]=1.0E10;
for(k=0;k<20;k++)
{ M[k] = GETM(k);
T[k+1]=(M[k]+T[k])/2.0;
S[k+1]=(4.0*T[k+1]-T[k])/3.0;
COUNT++;
E[k+1]=fabs(S[k+1]-S[k]);
if(k<3)continue;
if(E[k+1]<EPS)break;
}
return;
}
ShawResult()
{ int k;
system("cls");
printf("\n K M[K] T[K] S[K]");
printf(" E[K]");
printf("\n-------------------------------------------");
printf("-----------------------------------");
for(k=0;k<COUNT;k++)
printf("\n%2d %20.15lf%20.15lf%20.15lf%12.4e",k,M[k],T[k],S[k],E[k]);
printf("\n--------------------------------------------");
printf("-----------------------------------");
getch();
system("cls");
return;
}
SaveResult()
{ int k;
FILE * fp;
fp=fopen("Simpson.htm","w");
if(!fp) return;
fprintf(fp,"<html><head></head>");
fprintf(fp,"<body bgcolor = #006699 text = #FFff00>");
fprintf(fp,"<pre><font size=\"6\">");
fprintf(fp,"\n K M[K] T[K] S[K]");
fprintf(fp," E[K]");
fprintf(fp,"\n----------------------------------------------");
fprintf(fp,"----------------------------------");
for(k=0;k<COUNT;k++)
fprintf(fp,"\n%2d %20.15f%20.15f%20.15f%12.4le",k,M[k],T[k],S[k],E[k]);
fprintf(fp,"\n----------------------------------------------");
fprintf(fp,"----------------------------------");
fprintf(fp,"</pre></font></body>");
fclose(fp);
return;
}
main()
{ Simpson(EPS);
ShawResult();
SaveResult();
}
展开全部
#include<stdio.h>
#include<math.h>
void
main()
{
int
i,n;
float
x[10001],y[10001];
float
x0,xn,h,JiFeng;
/*f=x^3*/
printf("input
x0,xn,n:");
scanf("%f",&x0);
scanf("%f",&xn);
scanf("%d",&n);
h=(xn-x0)/n;
x[0]=x0;
for(i=0;i<=n;i++)
{
x[i]=x[0]+i*h;
y[i]=pow(x[i],3);
}
JiFeng=0.0;
for(i=0;i<n;i++)
{
JiFeng=JiFeng+y[i]+y[i+1];
}
JiFeng=JiFeng*h/2.0;
printf("\n");
printf("n=%d
JiFeng
is
%f",n,JiFeng);
}
#include<math.h>
void
main()
{
int
i,n;
float
x[10001],y[10001];
float
x0,xn,h,JiFeng;
/*f=x^3*/
printf("input
x0,xn,n:");
scanf("%f",&x0);
scanf("%f",&xn);
scanf("%d",&n);
h=(xn-x0)/n;
x[0]=x0;
for(i=0;i<=n;i++)
{
x[i]=x[0]+i*h;
y[i]=pow(x[i],3);
}
JiFeng=0.0;
for(i=0;i<n;i++)
{
JiFeng=JiFeng+y[i]+y[i+1];
}
JiFeng=JiFeng*h/2.0;
printf("\n");
printf("n=%d
JiFeng
is
%f",n,JiFeng);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询