ex-5: rearranged everything

This commit is contained in:
Giù Marcer 2020-04-30 22:25:44 +02:00 committed by rnhmjoj
parent cb6dc269c1
commit e7937a8c0d
6 changed files with 13 additions and 75 deletions

Binary file not shown.

View File

@ -1,37 +0,0 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
int main(int argc, char** argv){
size_t size = atoi(argv[1]); // size of the generated sample
// Initialize an RNG.
gsl_rng_env_setup();
gsl_rng *r = gsl_rng_alloc(gsl_rng_default);
// Random points (x, y) generation: x in [0,1), y in [0,3)
// get if y < exp(x)
// leave if y > exp(x)
size_t in = 0;
size_t total = 0;
do{
double x = gsl_rng_uniform(r);
double y = 3 * gsl_rng_uniform(r);
if (y <= exp(x)){
in++;
}
total++;
} while (total < size);
// The integral is esitmated thorugh the proportion:
// integral : 3 = in : total
double integral = 3.0*(double)in/(double)total;
printf("∫exp(x)dx in [0,1]:\t%.07f\n", integral);
// Free memory
gsl_rng_free(r);
return EXIT_SUCCESS;
}

View File

@ -5,10 +5,6 @@
#include <math.h> #include <math.h>
// Exact value of the integral.
double exact = 1.71828182845905;
// Function which must be passed to the gsl_function. // Function which must be passed to the gsl_function.
double function (double * x, size_t dim, void * params){ double function (double * x, size_t dim, void * params){
// Avoid warning when no parameters are passed to the function. // Avoid warning when no parameters are passed to the function.
@ -23,14 +19,24 @@ void results (char *title, double result, double error)
printf ("---------------------------\n%s\n", title); printf ("---------------------------\n%s\n", title);
printf ("result: % .10f\n", result); printf ("result: % .10f\n", result);
printf ("sigma: % .10f\n", error); printf ("sigma: % .10f\n", error);
printf ("exact: % .10f\n", exact);
} }
int main(){ int main(int argc, char** argv){
// If no argument is given is input, an error signal is displayed
// and the program quits.
if (argc != 2)
{
fprintf(stderr, "usage: %s D\n", argv[0]);
fprintf(stderr, "Computes the integral generating D points.\n");
return EXIT_FAILURE;
}
// Some useful variables. // Some useful variables.
double integral, error; double integral, error;
size_t calls = 50000000; size_t calls = 5000;
size_t dims = 1; size_t dims = 1;
double lower[1] = {0}; // Integration lower limit double lower[1] = {0}; // Integration lower limit
double upper[1] = {1}; // Integration upper limit double upper[1] = {1}; // Integration upper limit

Binary file not shown.

View File

@ -1,31 +0,0 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
/* This program evaluates the integral of exp(x) in [0;1] through the
* trapezoidal rule.
* The integration step is given in input as d in step = 1e-d.
*
*/
int main(int argc, char** argv){
double left = 0; // left limit
double point = left; // points at the current position
double right = 1; // right limit
double step = pow(10, -atoi(argv[1])); // step of integration
double sum = 0;
// Integral evaluation: stops when point goes over the right edge of the
// range.
do {
sum += step * 0.5 * (exp(point) + exp(point + step));
point += step;
} while (point < right);
// Print out the result
printf("∫exp(x)dx in [0,1]:\t%.7f\n", sum);
return EXIT_SUCCESS;
}

Binary file not shown.