Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double funcResult(double x) {
- return x/pow(3 * x + 2, 0.5);
- }
- double rectangle_integral_left(double (*func)(double), double start, double end, double rect_width) {
- double area = 0;
- for(double x = start; x <= end; x += rect_width) {
- double funcVal = func(x);
- area += rect_width * funcVal;
- }
- return area;
- }
- double rectangle_integral_center(double (*func)(double), double start, double end, double rect_width) {
- double area = 0;
- for(double x = start; x <= end; x += rect_width) {
- double funcVal = func(x + rect_width/2);
- area += rect_width * funcVal;
- }
- return area;
- }
- double rectangle_integral_right(double (*func)(double), double start, double end, double rect_width) {
- double area = 0;
- for(double x = start; x <= end; x += rect_width) {
- double funcVal = func(x + rect_width);
- area += rect_width * funcVal;
- }
- return area;
- }
- double trapezoidal_integral(double (*func)(double), double start, double end, double delta){
- double area = 0;
- for(double x = start; x <= end; x += delta) {
- double funcValA = func(x),
- funcValB = func(x + delta);
- area += ( (funcValA + funcValB) / 2 ) * delta;
- }
- return area;
- }
- double simpon_integral(double (*func)(double), double start, double end, double delta){
- double sum = func(start);
- for(double x = start + delta; x <= end; x += 2 * delta) {
- sum += func(x);
- }
- for(double x = start + 2 * delta; x <= end; x += 2 * delta) {
- sum += func(x);
- }
- return sum * delta;
- }
- int isPrecise(double val) {
- return ТУТ НАПИШИ РЕАЛЬНЕ ЗНАЧЕННЯ ІНТЕГРАЛУ < 0.001;
- }
- int main(){
- double integralLeftRectangle = rectangle_integral_left(funcResult, 0, 2, 0.00001);
- double integralCenterRectangle = rectangle_integral_center(funcResult, 0, 2, 0.00001);
- double integralRightRectangle = rectangle_integral_right(funcResult, 0, 2, 0.00001);
- double integralTrapezoidal = trapezoidal_integral(funcResult, 0, 2, 0.00001);
- double integralSimpson = simpon_integral(funcResult, 0, 2, 0.00001);
- printf("Integral Left Rectangle: %lf %s\n", integralLeftRectangle, isPrecise(integralLeftRectangle) ? "is Percise Enough" : "is not Percese Enough");
- printf("Integral Center Rectangle: %lf %s\n", integralCenterRectangle, isPrecise(integralCenterRectangle) ? "is Percise Enough" : "is not Percese Enough");
- printf("Integral Right Rectangle: %lf %s\n", integralRightRectangle, isPrecise(integralRightRectangle) ? "is Percise Enough" : "is not Percese Enough");
- printf("Integral Trapezoidal: %lf %s\n", integralTrapezoidal, isPrecise(integralTrapezoidal) ? "is Percise Enough" : "is not Percese Enough");
- printf("Integral Simpson: %lf %s\n", integralSimpson, isPrecise(integralSimpson) ? "is Percise Enough" : "is not Percese Enough");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement