uj5u.com熱心網友回復:
人工智能題,就用人工智能方法,梯度下降法。比如Google無比時髦的深度學習開源庫,就有自動求導功能,用梯度下降法即可。
uj5u.com熱心網友回復:
Google的深度學習庫:TensorFlowuj5u.com熱心網友回復:
明天我寫好MATLAB程式給你uj5u.com熱心網友回復:
采用牛頓迭代法,僅供參考:#include <math.h>
#include <stdio.h>
int main(void)
{
/* 牛頓迭代法求解方案 */
int count = 0;
double x, x1 = 0.1, x2;
/* f1:一階導數 f2:二階導數 */
double f1, f2;
while(1)
{
f1 = (x1-1)*(exp(x1)/pow(x1,2) - 1.0/exp(x1));
f2 = (pow(x1,3)*exp(x1) + 2)/pow(x1,3) - (2-x1)/exp(x1);
x2 = x1 - f1/f2;
if(fabs(x2 - x1) < 0.000001) break;
printf("%d\tx1:%lf\tx2:%lf\tf1:%lf\tf2:%lf\n", ++count, x1, x2, f1, f2);
x1 = x2;
}
x = (x1 + x2)/2;
printf("x:\t%lf\nmin:\t%lf\n", x, exp(x)/x + x/exp(x));
return 0;
}
運行結果:
E:\Workspace>tcc -run demo.c
1 x1:0.100000 x2:0.132903 f1:-98.651029 f2:2998.280809
2 x1:0.132903 x2:0.176237 f1:-55.309482 f2:1276.337719
3 x1:0.176237 x2:0.232854 f1:-30.942794 f2:546.532813
4 x1:0.232854 x2:0.305883 f1:-17.250485 f2:236.214183
5 x1:0.305883 x2:0.398202 f1:-9.561980 f2:103.575430
6 x1:0.398202 x2:0.511206 f1:-5.247612 f2:46.437333
7 x1:0.511206 x2:0.642233 f1:-2.825354 f2:21.563150
8 x1:0.642233 x2:0.779870 f1:-1.460443 f2:10.610806
9 x1:0.779870 x2:0.899293 f1:-0.688534 f2:5.765524
10 x1:0.899293 x2:0.971386 f1:-0.265094 f2:3.677113
11 x1:0.971386 x2:0.995409 f1:-0.069275 f2:2.883612
12 x1:0.995409 x2:0.999469 f1:-0.010840 f2:2.670432
13 x1:0.999469 x2:0.999943 f1:-0.001250 f2:2.636518
14 x1:0.999943 x2:0.999994 f1:-0.000135 f2:2.632595
15 x1:0.999994 x2:0.999999 f1:-0.000014 f2:2.632171
x: 1.000000
min: 3.086161
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/227971.html
標籤:C語言
上一篇:求助,為什么會出錯
