In [1]:
%%bash
sudo apt-get install time
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;
}
In [3]:
%%bash
gcc -Wall Rcf.c -o Rcf.out -lm
In [4]:
%%bash
/usr/bin/time -p ./Rcf.out
$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;
}
In [9]:
%%bash
gcc -Wall Rcf2.c -o Rcf2.out -lm
In [10]:
%%bash
/usr/bin/time -p ./Rcf2.out
$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;
}
In [20]:
%%bash
gcc -Wall Rcf3.c -o Rcf3.out -lm
In [21]:
%%bash
/usr/bin/time -p ./Rcf3.out
In [ ]: