gpt4 book ai didi

c - C语言中任意非线性方程的辛普森1/3积分法积分?

转载 作者:行者123 更新时间:2023-11-30 18:15:55 25 4
gpt4 key购买 nike

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",&deg);
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;
}

expecting output to be 60.00 but getting 0, don't know why?尝试通过辛普森 1/3 积分法对任何非线性方程进行积分。在代码块 IDE 中尝试过此操作。应用辛普森1/3规则的正确逻辑进行积分,仍然使被积分值始终为零,不知道我在这段代码中哪里做错了。

最佳答案

/* DEFINING TRAPEZIUM RULE FUNCTION */

double trapeziumrule (Variables *p){

printf("You have chosen the Trapezium Rule!\n");
printf("Please enter the highest order polynomial.\n");
scanf("%d",&p->poly);

for(int i=p->poly ; i>=0 ; --i){
printf("Please enter the coefficient for x^%d: ", i);
scanf("%lg",&p->coeff[i]);
}

//Establishing conditions
printf("\nPlease enter the lower bound \n");
scanf("%lg",&p->lbound);
printf("Please enter the higher bound \n");
scanf("%lg",&p->hbound);

//Sanity Check
if (p->hbound<=p->lbound){
printf("The higher bound must be higher than the lower bound!\n");
return -1;
}

printf("Please enter how many intervals you wish to use \n");
scanf("%lg",&p->interval);
printf("\n You chose a lower bound of %lg, an upper bound of %lg and %lg intervals \n",p->lbound, p->hbound,p->interval);

p->width = (p->hbound-p->lbound)/p->interval;

//Finding the x values to evaluate y at
for (int i=0 ; i<=p->interval ; ++i){
p->x[i]=p->lbound + i*p->width;
}

for (int k=0; k<=p->interval; ++k){
for (int i=0; i<=p->poly; ++i){
//Case for y(0) and y(interval)
if (k==0 || k==p->interval){
p->value=p->coeff[i]*pow(p->x[k],i)*(1.0/2.0);
}
//Case for middle y values
else if (k!=0 || k!=p->interval){
p->value=p->coeff[i]*pow(p->x[k],i);
}
//Adding each segment onto the previous
p->area=p->area+p->value;
}
}
//Finding the area under the curve
p->area=p->area*p->width;
printf("\n The result of the integration via Trapezium rule is %lg \n", p->area);
return 0;
}

/* DEFINING SIMPSONS RULE FUNCTION */

double simpsonsrule (Variables *s){

printf("You have chosen Simpsons rule!\n");
printf("Please enter the highest order polynomial.\n");
scanf("%d",&s->poly);

for(int i=s->poly ; i>=0 ; --i){
printf("Please enter the coefficient for x^%d: ", i);
scanf("%lg",&s->coeff[i]);
}
//Conditions
printf("Please enter the lower bound \n");
scanf("%lg",&s->lbound);
printf("Please enter the higher bound \n");
scanf("%lg",&s->hbound);

//Sanity Check
if (s->hbound<=s->lbound){
printf("The higher bound must be higher than the lower bound!\n");
return -1;
}

printf("Please enter how many intervals you wish to use \n");
scanf("%lg",&s->interval);
printf("\nYou chose a lower bound of %lg, an upper bound of %lg and %lg intervals \n",s->lbound, s->hbound,s->interval);

s->width = (s->hbound-s->lbound)/s->interval;

//Finding the x values to evaluate y at
for (int i=0 ; i<=s->interval ; ++i){
s->x[i]=s->lbound + i*s->width;
}

for (int k=0; k<=s->interval; ++k){
for (int i=0; i<=s->poly; ++i){
//Case for y(0) and y(interval)
if (k==0 || k==s->interval){
s->value=s->coeff[i]*pow(s->x[k],i)*(1.0/3.0);
}
//Case for odd values of y(interval)
else if (k%2!=0 && k!=s->interval){
s->value=s->coeff[i]*pow(s->x[k],i)*(4.0/3.0);
}
//Case for even values of y(interval)
else if (k%2==0 && k!=s->interval){
s->value=s->coeff[i]*pow(s->x[k],i)*(2.0/3.0);
}
s->area=s->area+s->value;
}
}
s->area=s->area*s->width;
printf("\n The result of the integration via Simpsons rule is %lg \n", s->area);
return 0;
}

double midptrule (Variables *m){

printf("You have chosen the Midpoint Rule!\n");
printf("Please enter the highest order polynomial.\n");
scanf("%d",&m->poly);

for(int i=m->poly ; i>=0 ; --i){
printf("Please enter the coefficient for x^%d: ", i);
scanf("%lg",&m->coeff[i]);
}
//Conditions
printf("Please enter the lower bound \n");
scanf("%lg",&m->lbound);
printf("Please enter the higher bound \n");
scanf("%lg",&m->hbound);



printf("Please enter how many intervals you wish to use \n");
scanf("%lg",&m->interval);
printf("\nYou chose a lower bound of %lg, an upper bound of %lg and %lg intervals \n",m->lbound, m->hbound, m->interval);

m->width = ((m->hbound-m->lbound)/m->interval)/2;

//Finding and storing x values
for (int i=0 ; i<m->interval ; ++i){
m->x[i]=m->lbound + (m->width+i*2*m->width);
}

//Calculating the y values
for (int k=0; k<m->interval; ++k){
for (int i=0; i<=m->poly; ++i){
m->value=m->coeff[i]*pow(m->x[k],i);
m->area=m->area+m->value;
}
}
m->area=m->area*2*m->width;
printf("\n The result is %lg \n", m->area);
return 0;
}

关于c - C语言中任意非线性方程的辛普森1/3积分法积分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com