Last active
August 29, 2015 14:20
-
-
Save full-of-foo/a354373e16cd88fab836 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -std=c99 -g -Wall -fopenmp -o trap trap.c */ | |
#include <stdio.h> | |
#include <math.h> | |
#include <omp.h> | |
double f(double x){ return (x*x + 2*sin(x)); } | |
void main(){ | |
double integral, step; | |
double a = 0.0; /*left*/ | |
double b = 1.0; /*right*/ | |
int N = 100000; /*subdivisions*/ | |
step = (b-a)/N; | |
integral = (f(b)+f(a))/2; | |
#pragma omp parallel for reduction(+: integral) | |
for(int i=0; i<N; i++) { | |
double x = a+i*step; | |
integral += f(x); | |
} | |
integral *= step; | |
printf("%s%d%s%f\n", "WITH N=", N, " TRAPEZOIDS, INTEGRAL=", integral); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment