In [1]:
%%bash
sudo apt-get install time


Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  time
0 upgraded, 1 newly installed, 0 to remove and 22 not upgraded.
Need to get 26.2 kB of archives.
After this operation, 79.9 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 time amd64 1.7-25.1build1 [26.2 kB]
Fetched 26.2 kB in 6s (4626 B/s)
Selecting previously unselected package time.
(Reading database ... 18427 files and directories currently installed.)
Preparing to unpack .../time_1.7-25.1build1_amd64.deb ...
Unpacking time (1.7-25.1build1) ...
Setting up time (1.7-25.1build1) ...
debconf: delaying package configuration, since apt-utils is not installed

In [2]:
%%file Rcf.c
#include<stdio.h>
#include<stdlib.h>
#include<math.h> //header para funciones de mate
void Rcf(double ext_izq, double ext_der, int n,\
    double *suma_global_p);
double f(double nodo);
int main(int argc, char *argv[]){
    double suma_global = 0.0;
    double a=0.0, b=1.0;
    int n=1e6; //número de subintervalos
    double objetivo=0.7468241328124271;
    Rcf(a,b,n,&suma_global);
    printf("Integral de %f a %f = %1.15e\n", a,b,suma_global);
    printf("Error relativo de la solución: %1.15e\n", fabs(suma_global-objetivo)/fabs(objetivo));
    return 0;
}
void Rcf(double a, double b, int n, double *sum){
    double h_hat=(b-a)/n;
    double x=0.0;
    int i=0;
    *sum = 0.0;
    for(i=0;i<=n-1;i++){
        x = a+(i+1/2.0)*h_hat;
        *sum+=f(x);
    }
    *sum =h_hat*(*sum);
}
double f(double nodo){
    double valor_f;
    valor_f = exp(-pow(nodo,2));
    return valor_f;
}


Writing Rcf.c

In [3]:
%%bash
gcc -Wall Rcf.c -o Rcf.out -lm

In [4]:
%%bash
/usr/bin/time -p ./Rcf.out


Integral de 0.000000 a 1.000000 = 7.468241328124773e-01
Error relativo de la solución: 6.719397313003120e-14
real 0.03
user 0.02
sys 0.00

$n=16777216 \approx 10^7$ subintervalos?


In [8]:
%%file Rcf2.c
#include<stdio.h>
#include<stdlib.h>
#include<math.h> //header para funciones de mate
void Rcf(double ext_izq, double ext_der, int n,\
    double *suma_global_p);
double f(double nodo);
int main(int argc, char *argv[]){
    double suma_global = 0.0;
    double a=0.0, b=1.0;
    int n=16777216; //número de subintervalos
    double objetivo=0.7468241328124271;
    Rcf(a,b,n,&suma_global);
    printf("Integral de %f a %f = %1.15e\n", a,b,suma_global);
    printf("Error relativo de la solución: %1.15e\n", fabs(suma_global-objetivo)/fabs(objetivo));
    return 0;
}
void Rcf(double a, double b, int n, double *sum){
    double h_hat=(b-a)/n;
    double x=0.0;
    int i=0;
    *sum = 0.0;
    for(i=0;i<=n-1;i++){
        x = a+(i+1/2.0)*h_hat;
        *sum+=f(x);
    }
    *sum =h_hat*(*sum);
}
double f(double nodo){
    double valor_f;
    valor_f = exp(-pow(nodo,2));
    return valor_f;
}


Overwriting Rcf2.c

In [9]:
%%bash
gcc -Wall Rcf2.c -o Rcf2.out -lm

In [10]:
%%bash
/usr/bin/time -p ./Rcf2.out


Integral de 0.000000 a 1.000000 = 7.468241328123114e-01
Error relativo de la solución: 1.549029203572843e-13
real 0.53
user 0.53
sys 0.00

$n=134217728\approx 10^8$ subintervalos?


In [19]:
%%file Rcf3.c
#include<stdio.h>
#include<stdlib.h>
#include<math.h> //header para funciones de mate
void Rcf(double ext_izq, double ext_der, int n,\
    double *suma_global_p);
double f(double nodo);
int main(int argc, char *argv[]){
    double suma_global = 0.0;
    double a=0.0, b=1.0;
    int n=134217728; //número de subintervalos
    double objetivo=0.7468241328124271;
    Rcf(a,b,n,&suma_global);
    printf("Integral de %f a %f = %1.15e\n", a,b,suma_global);
    printf("Error relativo de la solución: %1.15e\n", fabs(suma_global-objetivo)/fabs(objetivo));
    return 0;
}
void Rcf(double a, double b, int n, double *sum){
    double h_hat=(b-a)/n;
    double x=0.0;
    int i=0;
    *sum = 0.0;
    for(i=0;i<=n-1;i++){
        x = a+(i+1/2.0)*h_hat;
        *sum+=f(x);
    }
    *sum =h_hat*(*sum);
}
double f(double nodo){
    double valor_f;
    valor_f = exp(-pow(nodo,2));
    return valor_f;
}


Overwriting Rcf3.c

In [20]:
%%bash
gcc -Wall Rcf3.c -o Rcf3.out -lm

In [21]:
%%bash
/usr/bin/time -p ./Rcf3.out


Integral de 0.000000 a 1.000000 = 7.468241328123667e-01
Error relativo de la solución: 8.087062252817913e-14
real 4.39
user 4.37
sys 0.01

In [ ]: