чмы лаба 5

 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<cmath>
#include<cstdio>
#include<iostream>
#define SIZE 1000
using namespace std;
double f(double x,double y){
return x*x*x*x + y*y + 2*y + cos(0.2*x + 0.15*y);
}
double dx(double x,double y){
return 4*x*x*x - sin(0.2*x + 0.15*y)*0.2;
}
double dy(double x,double y){
return 2*y + 2 - sin(0.2*x + 0.15*y)*0.15;
}
double antyGrad(double x, double y, double k){
return f(x - k*dx(x,y), y - k*dy(x,y));
}
double norma(double x, double y){
return sqrt(x*x + y*y);
}
double findFactor(double a, double b, double eps, double x, double y)
{
while ((b-a)>=eps) {
if( antyGrad(x,y,(a+b-0.5*eps)/2) <= antyGrad(x,y,(a+b+0.5*eps)/2) )
b=(a+b+0.5*eps)/2;
else a=(a+b-0.5*eps)/2;
}
return (a+b)/2;
}
double descent(double bx, double by,double eps){
double x = bx, y = by, x1 = 0, y1 = 0, factor;
int k = 0;
do {
k++;
x = x1; y = y1;
factor = findFactor(-10000, 10000, eps, x, y);
x1 = x - factor * dx(x, y);
y1 = y - factor * dy(x, y);
} while (norma(x1 - x,y1 - y) > eps);
std::cout << "steps = " << k << std::endl;
std::cout <<"min: " <<"x = " << x1 << "; y = " << y1 <<std::endl;
return f(x1,y1);
}
int main(){
double x = 10, y = 10, eps = 0.001;
std::cout << "f(x, y) = " << descent(x,y,eps) << std::endl;
return 0;
}