Linklist 的 Simpson 1/3 积分方法。
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
struct term{
int power;
int coefficient;
struct term *nxt;
};
struct term *start=NULL;
int deg=0;
double valOfFuncAt(double);
void createEquationTermsInLL(int);
int main(){
double xo,xoTemp,xn,*fx,value;
double h;
double y=0.0,y_=0.0,z=0.0;
int n,i;
printf("Enter The Degree Of The Equation:: ");
scanf("%d",°);
createEquationTermsInLL(deg);
printf("\n\nEnter The Lower Limit,Upper Limit And No. Of Intervals(Must Be Even)::");
scanf("%lf %lf %d",&xo,&xn,&n);
h = (xn - xo)/n;
fx = (double*)malloc((n+1)*sizeof(double));
i=0;
xoTemp=xo;
while(i<=n){
*(fx + i)=valOfFuncAt(xoTemp);
xoTemp = xoTemp + h;
i++;
}
y = (*(fx+0)) + (*(fx + n));
i=1;
while(i<n){
z = z + *(fx + i);
i+=2;
}
z = 4*z;
i=2;
while(i<n){
y_ = y_ + *(fx + i);
i+=2;
}
y_ = 2*y_;
value = (h/3)*(y + z + y_);
printf("Integral Is:: %ld",value);
getch();
}
double valOfFuncAt(double x){
double fx1=0; int i;
struct term *temp=start;
for(i=deg;i>=0;i--){
fx1 = fx1 + (temp->coefficient) * pow(x,temp->power);
temp=temp->nxt;
}
return fx1;
}
void createEquationTermsInLL(int deg1){ /*Creating link list nodes */
static int i=0;
int j,coefficient;
int degClone=deg1;
struct term *temp=NULL;
for(j=1;j<=deg1+1;j++){
if(i==0){
start=(struct term*)malloc(sizeof(struct term));
printf("Enter Coefficient of %dst term",j);
scanf("%d",&coefficient);
start->coefficient=coefficient;
start->power=degClone; i++;
degClone-=1;
temp=start;
}
else{
temp->nxt=(struct term*)malloc(sizeof(struct term));
temp=temp->nxt;
if(j==2)
printf("Enter Coefficient of %dnd term",j);
else if(j==3)
printf("Enter Coefficient of %drd term",j);
else
printf("Enter Coefficient of %dth term",j);
fflush(stdin);
scanf("%d",&coefficient);
temp->power=degClone;
temp->coefficient=coefficient;
degClone-=1;
}
}
temp->nxt=NULL;
}
期望输出为 60.00 但得到 0,不知道为什么? 尝试通过辛普森的 1/3 积分方法对任何非线性方程进行积分。在代码块IDE中试过这个。应用 Simpson 的 1/3 积分规则的正确逻辑,仍然使 Integrand 值始终为零,不知道我在这段代码中哪里做错了。