Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- void regraAurea();
- int main()
- {
- regraAurea();
- return 0;
- }
- double f(double x){
- return pow(2*x + 1, 2) - 5*cos(10*x);
- }
- void regraAurea(){
- double x1 = -1, x2 = 0, x3 = 0, x4 = 0, B = (sqrt(5) - 1)/2, A = pow(B, 2);
- while (abs((x2 - x1)/x2) >= 0.005)
- {
- x3 = x1 + A*(x2 - x1);
- x4 = x1 + B*(x2 - x1);
- //minimo
- if (f(x3) < f(x4))
- {
- x1 = x1;
- x2 = x4;
- }
- else
- {
- x1 = x3;
- x2 = x2;
- }
- }
- cout << "Minimo:\n";
- cout << "x1 = " << x1 << endl;
- cout << "x2 = " << x2 << endl;
- cout << "x3 = " << x3 << endl;
- cout << "x4 = " << x4 << endl;
- x1 = -1;
- x2 = 0;
- while (abs((x2 - x1)/x2) >= 0.005)
- {
- x3 = x1 + A*(x2 - x1);
- x4 = x1 + B*(x2 - x1);
- //maximo
- if (f(x3) > f(x4))
- {
- x1 = x1;
- x2 = x4;
- }
- else
- {
- x1 = x3;
- x2 = x2;
- }
- }
- cout << "Maximo:\n";
- cout << "x1 = " << x1 << endl;
- cout << "x2 = " << x2 << endl;
- cout << "x3 = " << x3 << endl;
- cout << "x4 = " << x4 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement