Calculate area under the curve of a function

2

I have a problem with my algorithm. The purpose of the program is to calculate the area under the curve of a function by means of the Riemann method. The error is in the lower sum. since at the moment of being taking out the areas of the triangles, what I did is to multiply by and, but the monent to give the calculation is inaccurate. They could help me, and look for everything goolge and I can not find an example. I have suspicions that it has to be with an if you compare y. But I can not think of how. I'm a noob. Thanks.

pd: An example is: Lower limit: 4. Upper limit: 10. Number of rectangles: 3.

Correct answer: 76. Program response: 112

Analysis: It is giving me 36 more, since x = 6 is 36, but when x = 7 is 36. then it should not add that y. Greetings

#include <stdlib.h>
#include <stdio.h>

int main(){

float limiteMinimo,limiteMaximo; 
int rectangulos;  
float ancho;
float x,y;
float area;

printf("\nEste programa calculara el %crea de la funci%cn: y = -(x-7)^2 +19 \n",160,162);
printf("en los intervalos de 2.68 a 11.32 \n\n");

printf("Ingrese el l%cmite m%cnimo: ",161,161);
scanf("%f",&limiteMinimo);

printf("Ingrese el l%cmite m%cximo: ",161,160);
scanf("%f",&limiteMaximo);


printf("\nIngrese el n%cmero de segmentos (rect%cngulos) enteros: ",163,160);
scanf("%d",&rectangulos);

system("cls");

printf("\n**Suma inferior**\n\n");
printf("Datos: L%cmite m%cnimo: %.2f \n ",161,161,limiteMinimo);
printf("      L%cmite m%cximo: %.2f \n ",161,160,limiteMaximo);
printf("      N%cmero de segmentos: %d \n ",163,rectangulos);

ancho = (limiteMaximo - limiteMinimo)/rectangulos;
printf("\n Ancho: %.2f\n\n",ancho);

for( x=limiteMinimo ; x<=limiteMaximo ; x=x+ancho ){

    y= -((x-7)*(x-7))+19;

    area = area + (ancho*y);

}
printf("\nEl area es: %.2f \n\n",area);

system("pause");
return 0;
}
    
asked by Jose Warrior 01.05.2018 в 00:26
source

2 answers

0

We go in parts:

  • To begin with, you should avoid comparisons with float or double because it can cause problems (sometimes 1 iteration comes out, and sometimes 1 less). As we know that we are going to add the area of a number rectangulos of rectangles it is best to do the following for:

    for (int i=0; i<rectangulos; i++){
        //Hacemos las sumas.
    }
    
  • Now let's focus on how to calculate the areas. As you said, the Riemann method consists in dividing the function into rectangles and calculating its areas to get an approximation. The height of the rectangles depends on the value of the function that we choose, in your case you have chosen the value to the left of the rectangle. Other ways are to take the maximum of the function within the range of that rectangle (getting the so-called upper sum of Riemann) or equivalently the minimum (lower sum of Riemann). For more information you can see the wikipedia . Finally we add the areas and it would already be:

    double f(double x){
        return -((x-7)*(x-7))+19;
    }
    
    int main(){
    
        //Cogemos los datos...
    
        double area= 0;
        double ancho= (limiteMaximo - limiteMinimo)/rectangulos;
        for (int i=0; i<rectangulos; i++){
            area+= ancho*f(limiteMinimo+ancho*i);
        }
    
        printf("El area es %g",area);
    
    }
    
  • It should be remembered that the more rectangles we use to divide the same interval, we will get a better approximation. EYE! This does not mean that increasing in a rectangle, we will get a better result (this can be the same or even worse).

    RECOMMENDATION

    For the calculation of computer integrals, approximation methods are used, of which we find the trapeze method , the method of the compound trapezoid , Simpson's rule , Gauss quadrature ... Each with its advantages and disadvantages in terms of precision and complexity of implementation.

    However, the compound trapezoidal method is easy to understand and implement, and I recommend it before the Riemann method directly:

    The compound trapezoid method is characterized by dividing the area below the function into trapezoids, so all we need to know is to calculate the area of a trapezoid.

    Here is the sample code:

    double f(double x){
        double y= -((x-7)*(x-7))+19;
        return y;
    }
    
    double metodoTrapecio(double limIzq, double limDer){
        double ancho= limDer-limIzq;
        double area= ancho*(f(limIzq)+f(limDer))/2.0;
        return area;
    }
    
    double metodoTrapecioCompuesto(double limIzq, double limDer, int numPuntos){
        double paso= (limDer-limIzq)/numPuntos;
        double area= 0;
        for (int i=0; i<numPuntos-1; i++){
            area+= metodoTrapecio(limIzq+paso*i,limIzq+paso*(i+1));
        }
        return area;
    }
    
    
    int main(){
        printf("El area es %g",metodoTrapecioCompuesto(4,10,4));
        return 0;
    }
    

    This code calculates the approximation of the integral of the function you used in the question, with the same limits, and with 4 division points, or what is the same, 3 trapezoids, obtaining a result of 73,6875 . Notice that I used the 'simple' trapeze method to implement the compound trapezoid method. There is a more efficient way that is using the formula you can find in wikipedia :

    double f(double x){
        double y= -((x-7)*(x-7))+19;
        return y;
    }
    
    
    double metodoTrapecioCompuesto(double limIzq, double limDer, int numPuntos){
        double h= (limDer-limIzq)/numPuntos;
        double area= 0;
        for (int i=1; i<numPuntos-2; i++){
            area+= f(limIzq+ancho*i);
        }
        area+= h/2*(area+f(limIzq)+f(limDer))
        return area;
    }
    
    
    int main(){
        printf("El area es %g",metodoTrapecioCompuesto(4,10,4));
        return 0;
    }
    

    In this way I have achieved the result 73.625 .

        
    answered by 23.07.2018 в 21:06
    0

    Your problem is that you are doing 1 extra iteration in your for loop. You can add the width to the Minimum limit to avoid it. It would stay like this:

    limiteMinimo = limiteMinimo + ancho;
    for( x=limiteMinimo ; x<=limiteMaximo ; x=x+ancho ){
    
        y= -((x-7)*(x-7))+19;
    
        area = area + (ancho*y);
    
    }
    
        
    answered by 01.05.2018 в 01:03