主题:谁能帮我修改一下这个梯度下降算法,多谢!
#include <iostream>
#include <cmath>
using namespace std;
int a, b, c;
void input_funtion() {
printf("请依次输入二元二次方程ax^2 + bx + c的系数a, b, c, 请以a b c的形式输入3个系数:\n");
}
double fun_2(double x) {
return 2 * a * x + b;
}
double fun_1(double x) {
return a * x * x + b * x + c;
}
int main() {
input_funtion();
while(scanf("%d %d %d", &a, &b, &c) != EOF) {
double e = 0.00001, step = 0.5, x = 0, y0 = fun_1(x), y1 = 0;
while(true) {
x = x - step * fun_2(x);
y1 = fun_1(x);
if(abs(y1 - y0) < e) {
break;
}
y0 = y1;
}
printf("最小值点位于%lf。\n",x);
}
return 0;
}
#include <cmath>
using namespace std;
int a, b, c;
void input_funtion() {
printf("请依次输入二元二次方程ax^2 + bx + c的系数a, b, c, 请以a b c的形式输入3个系数:\n");
}
double fun_2(double x) {
return 2 * a * x + b;
}
double fun_1(double x) {
return a * x * x + b * x + c;
}
int main() {
input_funtion();
while(scanf("%d %d %d", &a, &b, &c) != EOF) {
double e = 0.00001, step = 0.5, x = 0, y0 = fun_1(x), y1 = 0;
while(true) {
x = x - step * fun_2(x);
y1 = fun_1(x);
if(abs(y1 - y0) < e) {
break;
}
y0 = y1;
}
printf("最小值点位于%lf。\n",x);
}
return 0;
}