Created
June 8, 2023 05:05
-
-
Save YeyoM/f8054886a32f0037e911c3467f76d616 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
/* | |
Benemerita Universidad Autonoma de Aguascalientes | |
Ing. En Computacion Inteligente | |
Estructuras Computacionales Avanzadas | |
Diego Emilio Moreno Sanchez ID: 264776 | |
Diego Emanuel Saucedo Ortega ID: 261230 | |
Carlos Daniel Torres Macias ID: 244543 | |
Profesor: Mr. en C. Luis Fernando Gutierrez Marfile�o | |
---->PROGRAMA | |
*/ | |
float fx(float x){ | |
return x*exp(2*x); | |
} | |
float f4x(float x){ | |
return 16*exp(2*x)*(x+2); | |
} | |
float f_error13(float a, float b, float x) { | |
float f4 = f4x(x); | |
float parte2 = pow((b - a), 4); | |
return (parte2 * f4 / 180); | |
} | |
float f_error38(float a, float b, float x) { | |
float f4 = f4x(x); | |
float parte2 = pow((b - a), 5); | |
return (3* parte2 * f4 / 80); | |
} | |
void simpson_1_3(){ | |
int opcion = 0; | |
float a, b; | |
int n; | |
cout<<"\tIngrese el valor del limite inferior: "; cin>>a; | |
cout<<"\tIngrese el valor del limite superior: "; cin>>b; | |
cout<<"\tIngrese numero de intervalos: "; cin>>n; | |
if (n % 2 != 0) { | |
cout << "El valor de n debe ser par" << endl; | |
return; | |
} | |
float h = (b - a) / n; | |
float sum_p = 0; | |
float sum_i = 0; | |
for (int i = 1; i <= n - 1; i++) { | |
if (i % 2 == 0){ | |
sum_p += fx(a + (i * h)); | |
} | |
else { | |
sum_i += fx(a + (i * h)); | |
} | |
} | |
float i_1 = (h / 3) * (fx(a) + 4 * sum_i + 2 * sum_p + fx(b)); | |
cout << "El valor de la integral es: " << i_1 << endl; | |
// error | |
float intervalos = (b - a) / n; | |
float a_error = a; | |
float error_total = 0; | |
for (int i = 0; i < n; i++) { | |
float b_error = a_error + intervalos; | |
error_total += f_error13(a_error, b_error, b_error); | |
a_error = b_error; | |
cout << "El error es: " << error_total << endl; | |
} | |
cout << "El error total es: " << error_total << endl; | |
} | |
void simpson_3_8(){ | |
float a,b; | |
int N; | |
cout<<"\tIngrese el valor del limite inferior: "; cin>>a; | |
cout<<"\tIngrese el valor del limite superior: "; cin>>b; | |
cout<<"\tIngrese numero de intervalos: "; cin>>N; | |
if(N%3!=0){ | |
cout<<"\n\tEl numero de intervalos debe ser multiplo de 3\n"; | |
return; | |
}//debe ser multiplo de tres | |
float h = (b - a) / N; | |
float sum = fx(a) + fx(b); | |
float error = 0; | |
// Suma de los t�rminos interiores | |
for (int i = 1; i < N; i++) { | |
float x = a + i * h; | |
if (i % 3 == 0) { | |
sum += 2 * fx(x); | |
} | |
else { | |
sum += 3 * fx(x); | |
} | |
}//fin for i | |
float res = (3 * h / 8) * sum; | |
cout << "El valor de la integral es: " << res<<endl; | |
//error | |
float intervalos = (b - a) / N; | |
float a_error = a; | |
float error_total = 0; | |
for (int i = 0; i < N; i++) { | |
float b_error = a_error + intervalos; | |
error_total += f_error38(a_error, b_error, b_error); | |
a_error = b_error; | |
cout << "El error es: " << error_total << endl; | |
}//fin for i | |
cout << "El error total es: " << error_total << endl; | |
} | |
int main(){ | |
int op; | |
do{ | |
cout<<"\t\t---------::PURA DOBLE P, VIEJILLO::---------\n"; | |
cout<<"\tEl siguiente programa realiza la integracion por Regla de Simpson\n"; | |
cout<<"\tde 1/3 y 3/8 para la funcion f(x)=x*e^(2*x)\n"; | |
cout<<"\n\t\t---------::MENU::---------\n"; | |
cout<<"\t\t\t1.Regla de Simpson 1/3.\n"; | |
cout<<"\t\t\t2.Regla de Simpson 3/8.\n"; | |
cout<<"\t\t\t3.Salir.\nSeleccion--->"; | |
cin>>op; | |
if(op==1){simpson_1_3();} | |
if(op==2){simpson_3_8();} | |
if(op==3){ | |
cout<<"Volando por L.A.\n"; | |
cout<<"Pa' Sinaloa me voy tambien\n"; | |
cout<<"Y las champanitas saben bien\n"; | |
} | |
system("pause"); | |
system("cls"); | |
}while(op!=3); | |
cout<<"-----------Programa elaborado por:---------------\n"; | |
cout<<"Diego Emilio Moreno Sanchez\n"; | |
cout<<"Diego Emanuel Saucedo Ortega\n"; | |
cout<<"Carlos Daniel Torres Macias\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment