-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimo_aproximado_funcion.cpp
More file actions
35 lines (27 loc) · 1.05 KB
/
Copy pathMinimo_aproximado_funcion.cpp
File metadata and controls
35 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>
// IMPORTANTE: CON ESTE METODO ES POSIBLE EXPLORAR UN MINIMO LOCAL Y QUEDARSE EN ESTE, POR LO QUE NO SE ASEGURA ENCONTRAR EL MINIMO GLOBAL
using namespace std;
double Ecuacion(const double& x){
return 5 * x * x - 7 * x - 13;
}
int main(){
double valorInicial = rand() % 10;
double Yini = Ecuacion(valorInicial);
double variacion = 1;
double Ysigue;
while(abs(variacion) > 0.00001){
Ysigue = Ecuacion(valorInicial + variacion);
if(Ysigue > Yini){ // No disminuye, seguimos hacia un paso menor
variacion *= -1; // Cambiamos el signo de la variacion
variacion /= 10; // Reducimos la variacion
}
else{
Yini = Ysigue; // Hallamos un valor menor que el anterior y disminuimos a este nuevo valor
valorInicial += variacion;
cout << "x: " << valorInicial << " + Yini: " << Yini << endl;
}
}
cout << "Respuesta: " << valorInicial << endl;
return 0;
}