diff --git a/ex-5/casino b/ex-5/casino deleted file mode 100755 index 6da0aae..0000000 Binary files a/ex-5/casino and /dev/null differ diff --git a/ex-5/casino.c b/ex-5/casino.c deleted file mode 100644 index 961c494..0000000 --- a/ex-5/casino.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -#include - -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; -} - diff --git a/ex-5/trifecta.c b/ex-5/main.c similarity index 87% rename from ex-5/trifecta.c rename to ex-5/main.c index 5cf3822..cf05f46 100644 --- a/ex-5/trifecta.c +++ b/ex-5/main.c @@ -5,10 +5,6 @@ #include -// Exact value of the integral. -double exact = 1.71828182845905; - - // Function which must be passed to the gsl_function. double function (double * x, size_t dim, void * params){ // 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 ("result: % .10f\n", result); 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. double integral, error; - size_t calls = 50000000; + size_t calls = 5000; size_t dims = 1; double lower[1] = {0}; // Integration lower limit double upper[1] = {1}; // Integration upper limit diff --git a/ex-5/manual b/ex-5/manual deleted file mode 100755 index fea418c..0000000 Binary files a/ex-5/manual and /dev/null differ diff --git a/ex-5/manual.c b/ex-5/manual.c deleted file mode 100644 index 16ba2f9..0000000 --- a/ex-5/manual.c +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include - -/* 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; -} diff --git a/ex-5/trifecta b/ex-5/trifecta deleted file mode 100755 index 9addaef..0000000 Binary files a/ex-5/trifecta and /dev/null differ