I have to make a program that calculates the integral of a function using the simpsons method and the trapezoid method. It works just fine on my computer using MinGW, but when I try to compile it in my uni's computer (GNU OS) I get:
It = 0 (should be 0.954499)
pn = 0 (should be 18)
Is = 0 (should be 0.954500)
pn = 0 (should be 6)

This is what I came up with (sorry the variables and comments are in portuguese, I'll fix it later after I get home):

integral.h:

#include <stdio.h>
#include <math.h>

#define eps 1.e-6
#define kmax 20

double trapezio(double (*f)(double x), double a, double b, int *n);
double simpson(double (*f)(double x), double a, double b, int *n);

integral.c:

#include "integral.h"
#define xmin -2
#define xmax 2

double f(double x);

int main(){
    double It,Is;
    int n = 0;
    int *pn = NULL;

    pn = &n;
    It = trapezio(f,xmin,xmax,pn)/sqrt(2*M_PI);
    printf("Pelo metodo dos trapezios a integral vale aproximadamente %lf\n", It);
    printf("O numero de iteracoes usadas foi %d\n\n",*pn);

    *pn = 0;
    Is = simpson(f,xmin,xmax,pn)/sqrt(2*M_PI);
    printf("Pelo metodo de simpson a integral vale aproximadamente %lf\n", Is);
    printf("O numero de iteracoes usadas foi %d\n",*pn);

    return 0;
}

double f(double x){
    return exp(-0.5*x*x); // Funcao que sera integrada
}

trapezio.c:

#include "integral.h"

double trapezio(double (*f)(double x), double a, double b, int *n){
    double To, Tk;
    double soma;
    int i, k = 1;
    Tk = 0.5*(f(a) - f(b))*(b - a);

    while (fabs((Tk-To)/To) > eps && k < kmax){
        soma = 0; // Resetando variavel soma
        To = Tk; // To e' T(k - 1), caso o loop se repita o ultimo Tk vira To
        for (i = 1 ; i <= (pow(2,k)-1) ; i += 2) soma += f(a + i*(b - a)/pow(2.,k));
        Tk = 0.5*To + soma*(b - a)/pow(2.,k);
        k++;
        *n += 1;
    }

    return Tk;
}

simpson.c:

#include "integral.h"

double simpson(double (*f)(double x), double a, double b, int *n){
    double So, Sk = 0;
    double somaimp, somapar;
    int i, k = 1;

    while (fabs((Sk-So)/So) > eps && k < kmax){
        somaimp = 0;
        somapar = 0;
        So = Sk; // So e' S(k - 1)
        for (i = 1; i <= (pow(2,k)-1); i += 2) somaimp += f(a + i*(b - a)/pow(2.,k));
        for (i = 2; i <= (pow(2,k)-2); i += 2) somapar += f(a + i*(b - a)/pow(2.,k));
        Sk = (b - a)*(f(a) + 4*somaimp + 2*somapar + f(b))/(3*pow(2.,k));
        k++;
        *n += 1;
    }

    return Sk;
}

Edit: I forgot to mention that if I take the pointer out, trapezio works, but simpson still returns 0.

Edited 3 Years Ago by Arroway

It works for me, too (mingw32-gcc 4.7.1). What compiler versions do you have on your machine and the school one?

In function trapezio() you do not initialize the variable To, hence the result of fabs((Tk-To)/To) is NAN (Not A Number). The reason Mingw may have worked could be because it auto-initialized the automatic variables.

Ok. Did some more debugging. To is initialized to 0, but you are dividing by To (divide by zero == no-no) at the start of the loop so it doesn't go into the loop. So, your solution is to use do {} while (condition); instead of while (condition) {}; - that will work for both cases. FWIW, trapezios not returns 0.954499 and simpson returns 0.954500 as it did before.

Edited 3 Years Ago by rubberman

This article has been dead for over six months. Start a new discussion instead.