user-0 提问: 求大佬帮助，人工智能题（希望用c或者MATLAB编写）

回复:
user: user-1
content: 采用牛顿迭代法，仅供参考： #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

user: user-1
content: “精度达到小数点后6位”，这个应该是f(x)的吧？上面是对x的，可能理解有误。 修正一下代码： #include <math.h> #include <stdio.h>  int main(void) { 	/* 牛顿迭代法求解方案 */ 	int count = 0; 	double x, x1, x2; 	 	/* f01 f02:原函数 f1:一阶导数 f2:二阶导数 */ 	double f01, f02, f1, f2; 	x1 = 0.1; 	f01 = exp(x1)/x1 + x1/exp(x1); 	 	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; 		f02 = exp(x2)/x2 + x2/exp(x2); 		printf("%d\tx1:%lf\tx2:%lf\tf1:%lf\tf2:%lf\n", ++count, x1, x2, f1, f2); 		if(fabs(f02 - f01) < 0.000001) break; 		x1 = x2; 		f01 = f02; 	} 	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 x:      0.999706 min:    3.086161

user: user-0
content: 谢谢大佬！我参考参考

user: user-2
content: 引用 8 楼 ctrigger的回复:[quote=引用 6 楼 user-2 的回复:] f(x)的最小值为3.086266  貌似精度不够。 当x=1时，f(1) = 3.0861612696304875569558112415141[/quote] 按照他的要求输出6位

user: user-1
content: 引用 6 楼 user-2 的回复: f(x)的最小值为3.086266  貌似精度不够。 当x=1时，f(1) = 3.0861612696304875569558112415141

user: user-2
content: %% 用遗传算法求函数f(x)=exp(x)/x+x/exp(x)在区间[0,5]上的最小值，要求精度达到小数点后6位 clc; clear; close all; tic; %%遗传参数设置 NUMPOP=100;%初始种群大小 % irange_l=-1; %问题解区间 % irange_r=2; irange_l=0; %问题解区间 irange_r=5; LENGTH=22; %二进制编码长度 ITERATION = 10000;%迭代次数 CROSSOVERRATE = 0.7;%杂交率 SELECTRATE = 0.5;%选择率 VARIATIONRATE = 0.001;%变异率  %初始化种群 pop=m_InitPop(NUMPOP,irange_l,irange_r); pop_save=pop; %绘制初始种群分布 x=linspace(0,5,1000); y=m_Fx(x); plot(x,y); hold on for i=1:size(pop,2)     plot(pop(i),m_Fx(pop(i)),'ro'); end hold off title('初始种群');  %开始迭代 for time=1:ITERATION     %计算初始种群的适应度     fitness=m_Fitness(pop);     %选择     pop=m_Select(fitness,pop,SELECTRATE);     %编码     binpop=m_Coding(pop,LENGTH,irange_l);     %交叉     kidsPop = crossover(binpop,NUMPOP,CROSSOVERRATE);     %变异     kidsPop = Variation(kidsPop,VARIATIONRATE);     %解码     kidsPop=m_Incoding(kidsPop,irange_l);     %更新种群     pop=[pop kidsPop]; end figure x=linspace(0,5,1000); y=m_Fx(x); plot(x,y); hold on for i=1:size(pop,2)     plot(pop(i),m_Fx(pop(i)),'ro'); end hold off title('终止种群'); fprintf('%.6f\n',max(m_Fx(pop))); toc;

user: user-2
content: f(x)的最小值为3.086266

user: user-2
content: 明天我写好MATLAB程序给你

user: user-1
content: Google的深度学习库：TensorFlow

user: user-1
content: 人工智能题，就用人工智能方法，梯度下降法。 比如Google无比时髦的深度学习开源库，就有自动求导功能，用梯度下降法即可。