```
//在 Qt5.x 编译qmake通过……2020年1月:
#include "mainwindow.h"
#include <QApplication>
#include "qdebug.h"
struct struc_X1X2 //放(2Ci二次函数的)两个根(的数据)结构:
{
double x1;
double x2;
};//
struc_X1X2 x1x2_001;
//----------------------------------------------------------------
//求(2Ci二次函数的)函数值
double func_Fx(double x, double a1, double b, double c,double y=0)//
{
double tmp_fx= a1*x*x+ b*x+ c;
return tmp_fx;
}//
//----------------------------------------------------------------
// 用极值公式,求(2Ci二次函数)的极值:
double fun_extremValue(double a1, double b, double c, double y=0)//
{
double tmp_y= 4.0*a1*c-b*b;
return (tmp_y/(4.0*a1) );
}//
//---------------------------------------------------------------------------
//用求根公式,求(2Ci二次函数)的两个根:注意 形参 &x1,&x2……传递进入(子)函数的用法:
struc_X1X2 func_x1x2(double aa, double b, double c, double * x1, double * x2)//
{
double tmp_y= sqrt(double(qAbs(double( b*b-4.0*aa*c ) ) ) ); //(b方- 4ac)并开平方
(*x1)= (-1.0*b - tmp_y)/ (2.0*aa);
(*x2)= (-1.0*b+ tmp_y) /(2.0*aa);
x1x2_001.x1= double(-1.0*b - tmp_y)/ (2.0*aa);
x1x2_001.x2= double(-1.0*b + tmp_y)/ (2.0*aa);
return x1x2_001;
}//
//---------------------
//(二次函数)的导函数的计算(函数):
double func_di_fx(double x, double a, double b)//
{
double tmp_di_y= 2.0* a* x + b;
return tmp_di_y;
}//
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
//
qDebug()<< "log test - Debug";
double x0_0=-20.0; //10.0;//-20.0;//给 X0赋予初值 2.0;//1.0;//1.0; //-10.0;
double tmp_Fx;
double tmp_Di_fx;
double aa=1.0;
double b=2.0;//2.0;
double c=-1.0;//0.0;//3.0;
double x0_01= x0_0-( func_Fx(x0_0,aa,b,c,0) / func_di_fx(x0_0,aa,b) ); //用 x0的值,(第1次)试用牛顿迭代公式,计算X1的值
double Xn_old01=x0_01; //准备迭代,所以把 X1的值 先放在 X(n-1)这个变量(Xn_old01)里 待用
double X_n=0.0; //= Xn_old01; // X_n赋予初值 (无实际意义)与 X_n=0.0;等效
//
double X_extremV=fun_extremValue(aa,b,c,0.0); //用极值公式 计算 极值
//下面 迭代
double x1=0.0; double x2=0.0; //初始化 两个 变量,放(二次函数)的两个根
func_x1x2(aa,b,c, &x1,&x2); //(用求根公式)求根,并放在 (x1,x2) 当中,注意 形参 &x1,&x2的用法。
for(int i=1;i<99;++i)//
{
tmp_Fx= func_Fx(Xn_old01 , aa, b, c, 0); //计算函数 F(x)=Y 的值
tmp_Di_fx= func_di_fx(Xn_old01, aa, b); //计算 导函数 f'(x)= y' 的值
//double
X_n= (double)Xn_old01- ( (double)tmp_Fx/ tmp_Di_fx); //这就是著名的 牛顿迭代公式
qDebug()<<"F(x(n-1)):"<<tmp_Fx<<", Dfx(n-1)"<<tmp_Di_fx;
qDebug()<< "第NoId:"<< i;
qDebug()<< "次迭代,求得的根x(n):";
qDebug() <<X_n << ", 求根与极值之间的差(这个errorValue代替误差)X(trun)-X(x):=" <<( X_extremV- X_n ) << "] x1,x2:=:"<< x1x2_001.x1<<","<< x1x2_001.x2<< " ;";
Xn_old01= X_n;
}//for110i
// qDebug("如果只写在括号里,是不需要QDebug头文件的 %d %s", 1.2, "3.4");
//
return a.exec();
}//
```
迭代99次以后的结果:
```
F(x(n-1)): 89.2528 , Dfx(n-1) -19.1053
x(n): -5.881 , X(trun)-X(x):= 3.881 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 21.8242 , Dfx(n-1) -9.762
x(n): -3.64538 , X(trun)-X(x):= 1.64538 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 4.99801 , Dfx(n-1) -5.29075
x(n): -2.70071 , X(trun)-X(x):= 0.700706 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.892401 , Dfx(n-1) -3.40141
x(n): -2.43834 , X(trun)-X(x):= 0.438344 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.0688337 , Dfx(n-1) -2.87669
x(n): -2.41442 , X(trun)-X(x):= 0.414416 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.000572555 , Dfx(n-1) -2.82883
x(n): -2.41421 , X(trun)-X(x):= 0.414214 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 4.09657e-08 , Dfx(n-1) -2.82843
x(n): -2.41421 , X(trun)-X(x):= 0.414214 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0 , Dfx(n-1) -2.82843
```
观察结果, 几次(6次)以后…就稳定、并且收敛了……
![](https://img.kancloud.cn/a9/ce/a9cedf494d3fc5460435ca64f9351e3e_951x983.jpg)![](https://img.kancloud.cn/2a/08/2a08fd7a300341b5d87a3bf24ea5670b_971x1221.jpg)![](https://img.kancloud.cn/e6/af/e6af46a343c7a34e99ee3a90867e9e0d_953x1213.jpg)
![](https://img.kancloud.cn/fc/4d/fc4da37a58824ad021dfecc4d91e4598_963x1229.jpg)
![](https://img.kancloud.cn/f6/28/f628dd7a7aad2390c89d41c35a3c6657_955x1247.jpg)
![](https://img.kancloud.cn/ca/29/ca29b685744d35b1f4d0a6beaa58bcf8_973x1029.jpg)
再运行一遍:
p, li { white-space: pre-wrap; }
~~~
double x0_0=10.0;//-20.0;//给 X0赋予初值
~~~
```
F(x(n-1)): 29.2583 , Dfx(n-1) 11.1818
??NoId: 1
???????????????x(n):
1.97432 , ???????????????????????errorValue????????)X(trun)-X(x):= -3.97432 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 6.84656 , Dfx(n-1) 5.94863
??NoId: 2
???????????????x(n):
0.82337 , ???????????????????????errorValue????????)X(trun)-X(x):= -2.82337 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 1.32468 , Dfx(n-1) 3.64674
??NoId: 3
???????????????x(n):
0.46012 , ???????????????????????errorValue????????)X(trun)-X(x):= -2.46012 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.13195 , Dfx(n-1) 2.92024
??NoId: 4
???????????????x(n):
0.414935 , ???????????????????????errorValue????????)X(trun)-X(x):= -2.41494 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.00204167 , Dfx(n-1) 2.82987
??NoId: 5
???????????????x(n):
0.414214 , ???????????????????????errorValue????????)X(trun)-X(x):= -2.41421 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 5.20519e-07 , Dfx(n-1) 2.82843
??NoId: 6
???????????????x(n):
0.414214 , ???????????????????????errorValue????????)X(trun)-X(x):= -2.41421 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 3.37508e-14 , Dfx(n-1) 2.82843
??NoId: 7
???????????????x(n):
0.414214
6次以后收敛、并且稳定。
```
第3次运行:
double x0_0=-20.0;//给 X0赋予初值
```
F(x(n-1)): 89.2528 , Dfx(n-1) -19.1053
??NoId: 1
???????????????x(n):
-5.881 , ???????????????????????errorValue????????)X(trun)-X(x):= 3.881 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 21.8242 , Dfx(n-1) -9.762
??NoId: 2
???????????????x(n):
-3.64538 , ???????????????????????errorValue????????)X(trun)-X(x):= 1.64538 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 4.99801 , Dfx(n-1) -5.29075
??NoId: 3
???????????????x(n):
-2.70071 , ???????????????????????errorValue????????)X(trun)-X(x):= 0.700706 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.892401 , Dfx(n-1) -3.40141
??NoId: 4
???????????????x(n):
-2.43834 , ???????????????????????errorValue????????)X(trun)-X(x):= 0.438344 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.0688337 , Dfx(n-1) -2.87669
??NoId: 5
???????????????x(n):
-2.41442 , ???????????????????????errorValue????????)X(trun)-X(x):= 0.414416 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0.000572555 , Dfx(n-1) -2.82883
??NoId: 6
???????????????x(n):
-2.41421 , ???????????????????????errorValue????????)X(trun)-X(x):= 0.414214 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 4.09657e-08 , Dfx(n-1) -2.82843
??NoId: 7
???????????????x(n):
-2.41421 , ???????????????????????errorValue????????)X(trun)-X(x):= 0.414214 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0 , Dfx(n-1) -2.82843
??NoId: 8
???????????????x(n):
-2.41421 , ???????????????????????errorValue????????)X(trun)-X(x):= 0.414214 ] x1,x2:=: -2.41421 , 0.414214 ;
F(x(n-1)): 0 , Dfx(n-1) -2.82843
```
请看:
利用 牛顿迭代法(Newton Iteration Method)求(2Ci二次函数的数值解)6次以后,就稳定、并且收敛到(局部)极值(局部最优解)了。
Qt支持中文不好,所以 ?(问号)都是中文提示……请忽略……
- BP神经网络到c++实现等--机器学习“掐死教程”
- 训练bp(神经)网络学会“乘法”--用”蚊子“训练高射炮
- Ann计算异或&前馈神经网络20200302
- 神经网络ANN的表示20200312
- 简单神经网络的后向传播(Backpropagration, BP)算法
- 牛顿迭代法求局部最优(解)20200310
- ubuntu安装numpy和pip3等
- 从零实现一个神经网络-numpy篇01
- _美国普林斯顿大学VictorZhou神经网络神文的改进和翻译20200311
- c语言-普林斯顿victorZhou神经网络实现210301
- bp网络实现xor异或的C语言实现202102
- bp网络实现xor异或-自动录入输入(写死20210202
- Mnist在python3.6上跑tensorFlow2.0一步一坑20210210
- numpy手写数字识别-直接用bp网络识别210201