Example #1
0
int main(int argc, char *argv[]) {
  iParams p;
  int i;

  /* set defaults for parameters */
  p.a = 0.0;
  p.b = 1.0;
  p.n = 10;
  
  parse_arguments(argc, argv, &p);
  
  fprintf(stdout,"Initializing integration of f(x) = sqrt(x) + sin(x^(3/2)), from %f to %f with  %d segments\n",p.a, p.b, p.n);
  
  p.h = (double)(p.b - p.a)/(double)p.n;

  double trapezoid_sum = 0;

  for (i = 0; i < p.n; ++i) {
    trapezoid_sum += trapezoid(p.a + (double)i * p.h, p.a + (double)(i+1) * p.h);
  }

  fprintf(stdout, "Integrated value: %20.15f\n",trapezoid_sum);
     
  return 0;
}
Example #2
0
	static typename function_traits<F>::result_t integrate(const F &f, double a, double b) {
		using T = typename function_traits<F>::result_t;

		if (a >= b)
			return T(0);

		T t;
		std::array<T, N> Tk;
		Tk.fill(T(0));

		Tk[0] = .5 * (b - a) * (f(a) + f(b));

		for (std::uint64_t n = 1; n < N; ++n) {
			std::uint64_t k = static_cast<std::uint64_t>(1) << n;
			double h = (b - a) / static_cast<double>(k);
			t = .5 * Tk[0] + h * trapezoid(f, h, a, b, k);

			for (std::uint64_t m = 1; m <= n; ++m) {
				T s = t + (t - Tk[m - 1]) / static_cast<double>((1 << 2 * m) - 1);
				Tk[m - 1] = t;
				t = s;
			}

			Tk[n] = t;
		}

		return Tk[N - 1];
	}
Example #3
0
int testTrapezoid(acc sample1, acc sample2, CAWAX_TIME_MSM time1, CAWAX_TIME_MSM time2, sample_th order1, sample_th order2) {
	printf("Testing trapezoid \n");
	printf("Sample 1: %.6f / %li / %li\n", sample1, time1, order1);
	printf("Sample 2: %.6f / %li / %li\n", sample2, time2, order2);

	printf("Trapezoid = %.6f\n", trapezoid(sample1, sample2, time1, time2, order1, order2, UNIT_MILLISEC_TO_MICRO));
}
Example #4
0
/* Ex 8.04 -- compute the area of a polygon */
double
area(poly_t P) {
	int i;
	double area=0.0;
	/* step through the edges, adding up the areas of the
	   trapezoidal regions indicated by pairs of consecutive
	   points. Assumes that polygon is well-behaved.
	*/
	for (i=0; i<P.npts-1; i++) {
		area += trapezoid(P.pts[i], P.pts[i+1]);
	}
	/* plus the last closing edge */
	area += trapezoid(P.pts[P.npts-1], P.pts[0]);
	/* area is always positive, but summation may have ended
	   up with a negative value */
	if (area<0) {
		area = -area;
	}
	return area;
}
Example #5
0
void calcarea(double start, double stop, int steps)
{
    double totarea = 0.0, x1, x2, fx1, fx2, dx, sum_y;

    x1 =  start;
    fx1 = f(x1);

    for(int i =1; i<= steps; i++)
    {
        x2 = x1 + (double)i*(stop-start)/steps;
        fx2 = f(x2);
        dx = x2 - x1;
        sum_y = fx1 + fx2;
        totarea = totarea + trapezoid(dx, sum_y);
    }
    printf("\nThE total area is: %f\n", totarea );
}
Example #6
0
/* Main function */
int main(int argc, char* argv[]) {
    double j = 0.0;
    integral fun = &f1;

    /* Print the integrals of the first function using all of the available 
     * methods including trapezoidal, rectangle, Simpson and Gauss
     */
    printf("\nIntegrals of x^2 on [0,1]:\n\n");
    j = trapezoid(10, 0, 1, fun);
    printf("Trapezoidal \t= %G\n", j);
    j = rect(10, 0, 1, fun);
    printf("Rectangle \t= %G\n", j);
    j = simpson(10, 0, 1, fun);
    printf("Simpson \t= %G\n", j);
    j = gauss(10, 0, 1, fun);
    printf("Gauss \t\t= %G\n", j);

    fun = &f2;

    /* Print the integrals of the second function using all of the available 
     * methods including trapezoidal, rectangle, Simpson and Gauss
     */
    printf("\nIntegrals of x^3 on [0,1]:\n\n");
    j = trapezoid(10, 0, 1, fun);
    printf("Trapezoidal \t= %G\n", j);
    j = rect(10, 0, 1, fun);
    printf("Rectangle \t= %G\n", j);
    j = simpson(10, 0, 1, fun);
    printf("Simpson \t= %G\n", j);
    j = gauss(10, 0, 1, fun);
    printf("Gauss \t\t= %G\n", j);
	
    fun = &f3;

    /* Print the integrals of the third function using all of the available 
     * methods including trapezoidal, rectangle, Simpson and Gauss
     */
    printf("\nIntegrals of x^5 on [0,1]:\n\n");
    j = trapezoid(10, 0, 1, fun);
    printf("Trapezoidal \t= %G\n", j);
    j = rect(10, 0, 1, fun);
    printf("Rectangle \t= %G\n", j);
    j = simpson(10, 0, 1, fun);
    printf("Simpson \t= %G\n", j);
    j = gauss(10, 0, 1, fun);
    printf("Gauss \t\t= %G\n", j);
 
    fun = &f4;

    /* Print the integrals of the fourth function using all of the available 
     * methods including trapezoidal, rectangle, Simpson and Gauss
     */
    printf("\nIntegrals of e^-x on [0,1]:\n\n");
    j = trapezoid(10, 0, 1, fun);
    printf("Trapezoidal \t= %G\n", j);
    j = rect(10, 0, 1, fun);
    printf("Rectangle \t= %G\n", j);
    j = simpson(10, 0, 1, fun);
    printf("Simpson \t= %G\n", j);
    j = gauss(10, 0, 1, fun);
    printf("Gauss \t\t= %G\n", j);

    fun = &f5;

    /* Print the integrals of the fifth function using all of the available 
     * methods including trapezoidal, rectangle, Simpson and Gauss
     */
    printf("\nIntegrals of xe^-x on [0,2]:\n\n");
    j = trapezoid(10, 0, 2, fun);
    printf("Trapezoidal \t= %G\n", j);
    j = rect(10, 0, 2, fun);
    printf("Rectangle \t= %G\n", j);
    j = simpson(10, 0, 2, fun);
    printf("Simpson \t= %G\n", j);
    j = gauss(10, 0, 2, fun);
    printf("Gauss \t\t= %G\n", j);

    fun = &f6;

    /* Print the integrals of the sixth function using all of the available 
     * methods including trapezoidal, rectangle, Simpson and Gauss
     */
    printf("\nIntegrals of x^-0.5 on [0,2]:\n\n");
    j = trapezoid(10, 0, 2, fun);
    printf("Trapezoidal \t= %G\n", j);
    j = rect(10, 0, 2, fun);
    printf("Rectangle \t= %G\n", j);
    j = simpson(10, 0, 2, fun);
    printf("Simpson \t= %G\n", j);
    j = gauss(10, 0, 2, fun);
    printf("Gauss \t\t= %G\n", j);

    return 1;
}
int main(int argc, char** argv) {

  int rank;           /* process rank */
  int nprocs;         /* number of processes */
  int source;         /* rank of sender */
  int dest = MASTER;  /* all procs send to master */
  int tag = 0;        /* message tag */
  MPI_Status status;  /* struct to hold message status */ 
  int local_n;        /* number of trapezoids evaluated by each process */

  double integral;    /* where we'll store the result of the integration */
  double local_sum;   /* local portion of the integration summation */
  double a = 0.0;     /* left endpoint of definite integration */
  double b = 1.0;     /* right endpoint of definite integration */
  double local_a;     /* left endpoint for local chunk */
  double local_b;     /* right endpoint for local chunk */
  double width;       /* width of each trapezoid - same for all proceses*/

  double PI25DT = 3.141592653589793238462643;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

  /* width is the same for all trapezoids, on all procs */
  width = (b - a)/(double)N;

  /* how many trapezoids per process? */
  local_n = N/nprocs;

  printf("rank %d:\tlocal_n %d\n", rank, local_n);

  /* calculate local interals to work on */
  local_a = a + (rank * (local_n * width));  /* since each local interval is (local_n * width) */
  local_b = local_a + (local_n * width);

  printf("rank %d:\tlocal_a %f, local_b %f\n", rank, local_a, local_b);

  /* local trapezoid calculations are bundled into a function */ 
  local_sum = trapezoid(local_a,local_b, local_n, width);

  printf("rank %d:\tlocal_sum %f\n", rank, local_sum);

  /* message passing and totalling of local sums */
  if (rank == MASTER) {
    integral = local_sum;
    for (source =1; source < nprocs; source++) {
      MPI_Recv(&local_sum, 1, MPI_DOUBLE, source, tag, MPI_COMM_WORLD, &status);
      integral += local_sum;
    }
  }
  else {
    MPI_Send(&local_sum, 1, MPI_DOUBLE, dest, tag, MPI_COMM_WORLD);
  }

  /* print the result */
  if (rank == MASTER) {
    printf("\n");
    printf("Definite integral from %f to %f estimated as %f (using %d trapezoids)\n",
	   a, b, integral, N);
    printf("(error: %.16f)\n",fabs(integral - PI25DT));
  }

  MPI_Finalize();

  return EXIT_SUCCESS;
}