Skip to content

Instantly share code, notes, and snippets.

@full-of-foo
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save full-of-foo/a354373e16cd88fab836 to your computer and use it in GitHub Desktop.
Save full-of-foo/a354373e16cd88fab836 to your computer and use it in GitHub Desktop.
/* 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