强力推荐大家阅读《深度学习的数学》一书,日本作家所写(通俗易懂),可当作课外读物阅读
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double n = 0.0001;
double start_x = 1000.0, start_y = 1000.0;
double grade_x = 2 * (start_x) + 1;
double grade_y = 2 * start_y;
double dx = -n * grade_x;
double dy = -n * grade_y;
while (fabs(dx) > 0.00001 || fabs(dy) > 0.00001)
{
cout << "位移 " << dx << " " << dy << endl;
start_x = start_x + dx;
start_y = start_x + dy;
dx = -n * (2 * (start_x) + 1);
dy = -n * (2 * start_y);
std::cout << "min-value is " << (start_x * start_x + start_y * start_y + start_x + 1) << std::endl;
cout << start_x << "," << start_y << endl;
if (!(fabs(start_x) > 0.01 && fabs(start_y) > 0.01))
{
cout << 0 << "," << 0 << endl;
break;
}
}
if (!(fabs(start_x) > 0.01 && fabs(start_y) > 0.01))
{
cout << 0 << "," << 0 << endl;
}
return 0;
}