Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MO_Powell.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- double f(double p1, double p2, double x, double y, double alpha)
- {
- return pow((p1 + x*alpha - 2.0), 2.0) + pow((p2 + y*alpha + 1.0), 2.0) - 0.5*(p1 + x*alpha)*(p2 + y*alpha) + 4.0;
- //return sin(p1 + x*alpha + 0.8) *sin(p1 + x*alpha + 0.8) + sin(p2 + y*alpha - 0.2)* sin(p2 + y*alpha - 0.2);
- }
- int main()
- {
- double a = -1.0, b = 3.0; //interval
- double e[2] = { 1.0,0 }; //direction
- double x_init[2] = { 0,0 }; //
- double x_second[2] = { 0,0 };
- double accuracy = 0.01;
- double x1, x2;
- //25% method
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- std::cout << x1;
- std::cout << x2;
- double min;
- while (abs(x2-x1)>accuracy)
- {
- if (f(x_init[0], x_init[1], e[0], e[1], x1) >= f(x_init[0], x_init[1], e[0], e[1], x2))
- {
- min = f(x_init[0], x_init[1], e[0], e[1], x2);
- a = x1;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- }
- else if (f(x_init[0], x_init[1], e[0], e[1], x2) > f(x_second[0], x_second[1], e[0], e[1], x1))
- {
- min = f(x_init[0], x_init[1], e[0], e[1], x1);
- b = x2;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- }
- else continue;
- }
- std::cout << "local min at" << (x1 + x2) / 2.0 << " with value " <<min<<std::endl;
- x_second[0] = (x1 + x2) / 2.0;
- e[0] = 0;
- e[1] = 1;
- a = -1.0;
- b = 3.0;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- std::cout << "second iteration: "<<std::endl;
- while (abs(x2 - x1)>accuracy)
- {
- if (f(x_second[0], x_second[1], e[0], e[1], x1) >= f(x_second[0], x_second[1], e[0], e[1], x2))
- {
- min = f(x_init[0], x_init[1], e[0], e[1], x2);
- b = x2;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- }
- else if (f(x_init[0], x_init[1], e[0], e[1], x2) > f(x_second[0], x_second[1], e[0], e[1], x1))
- {
- min = f(x_second[0], x_second[1], e[0], e[1], x1);
- b = x2;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- }
- else continue;
- }
- std::cout << "local min at: " << (x1 + x2) / 2.0 << " with value " << min<<std::endl;
- e[0] = x_second[0] - x_init[0];
- e[1] = x_second[1] - x_init[1];
- a = -1.0;
- b = 3.0;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- std::cout << x1 << "," << x2 << "," << e[0] << "," << e[1] << "," << a << "," << b << std::endl;
- std::cout << "third iteration: " << std::endl;
- while (abs(x2 - x1)>=accuracy)
- {
- if (f(x_second[0], x_second[1], e[0], e[1], x1) >= f(x_second[0], x_second[1], e[0], e[1], x2))
- {
- min = f(x_init[0], x_init[1], e[0], e[1], x2);
- b = x2;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- }
- else if (f(x_init[0], x_init[1], e[0], e[1], x2) > f(x_second[0], x_second[1], e[0], e[1], x1))
- {
- min = f(x_second[0], x_second[1], e[0], e[1], x1);
- b = x2;
- x1 = a + (b - a)*0.25;
- x2 = b - (b - a)*0.25;
- }
- }
- std::cout << "local min at: " << (x1 + x2) / 2.0 << " with value " << min;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement