0

Simpson 1/3rd integration method by Linklist.

#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? Trying to do integration by Simpson's 1/3rd integration method for any non-linear Equation. Tried this in code blocks IDE. Applying the correct logic of Simpson's 1/3rd rule for integration, still getting the Integrand value always to zero, don't know where I did wrong in this code.

  • 2
    step through it in a debugger and see what's happening. – Christian Gibbons Jan 31 '18 at 19:18
  • 1
    When you print the result ("Integral Is"), you use the `%ld` format on a `double` arguent. Use `%g`, `%f` or `%e`. Switching on compiler warnings will detect such errors. – M Oehm Jan 31 '18 at 19:30
  • 1
    And instead of assertions such as "applying the correct logic", please include the input data, the expected result and the actual result. That would make life much easier for those whose help you are requesting. – M Oehm Jan 31 '18 at 19:32
  • 1
    `if(i==0){` could be replaced by `if(start == NULL){`, eliminating the `i` variable. Also: most people prefer `array[j]` indexing to `*(array +j)` – joop Feb 01 '18 at 11:05
  • Got that!! thanks, Mr. M Oehm!! –  Feb 03 '18 at 18:46

1 Answers1

-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;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
HAZARD
  • 1
  • 2
    Code only answers with no explanation, especially large blobs like this, don't really answer the question as to what is wrong with the asker's code and how to fix it. – dbush Dec 12 '19 at 17:36
  • Repair this code. Use code tags instead of indentation. – Gander Dec 12 '19 at 17:54