对于C语言中的任何非线性方程,使用辛普森1/3积分方法进行积分?

问题描述 投票:0回答:1

Linklist的Simpson 1 / 3rd积分方法。

#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?尝试通过Simpson的1 / 3rd积分方法对任何非线性方程式进行积分。在代码块IDE中对此进行了尝试。应用Simpson的1 / 3rd规则进行积分的正确逻辑,仍然使Integrand值始终为零,不知道我在此代码中的错误之处。

c linked-list codeblocks dynamic-programming simpsons-rule
1个回答
-4
投票
/* 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.