MIPI_AdvancedC_FRTK/Lect8/62_calcIntegralTrap.c

46 lines
950 B
C
Raw Permalink Normal View History

2024-11-14 08:45:50 +03:00
#include <stdio.h>
#include <math.h>
typedef float(*function)(float);
//(-2, -1.5) (-1.5, -1) (-1, -0.5) (-0.5, 0)
float f(float x) {
return 8*x*x*x*x + 32*x*x*x + 40*x*x + 16*x + 1;
}
float testf(float x){
return 2;
}
float calcIntegralSquare(float xl, float xr, size_t n, function f) {
float sum = 0;
float h = (xr-xl)/n;
for(size_t i=0; i<n; i++) {
sum += f(xl);
xl += h;
}
return sum*h;
}
float calcIntegralTrap(float xl, float xr, size_t n, function f) {
float sum = 0;
float h = (xr-xl)/n;
for(float x=xl+h; x<xr-h; x+=h) {
sum += 0.5*h*(f(x)+f(x+h));
}
return sum;
}
int main()
{
printf("calcIntegralSquare integral %f\n", calcIntegralSquare(0,2,1000,testf));
printf("calcIntegralSquare = %f\n", calcIntegralSquare(-1.382683,-0.617316,1000,f));
printf("calcIntegralTrap = %f\n", calcIntegralTrap(-1.382683,-0.617316,1000,f));
return 0;
}