当前位置:网站首页>利用 MATLAB 编程实现最速下降法求解无约束最优化问题
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
2022-04-23 14:33:00 【i道i】
本文章包含以下内容
1、画出最速下降法的算法流程图;
2、MATLAB 编写用数值微分法的梯度计算函数(函数式 M 文件);
3、MATLAB 编写最速下降法求解无约束优化问题的函数,要求采用黄金分割法精确一维搜索,用数值微分法计算梯度(函数式 M 文件,精度设为 epson 可调);
4、MATLAB 编写最速下降法求解无约束优化问题的函数,要求采用 Wolfe-Powell 非精确一维搜索,用数值微分法计算梯度(函数式 M 文件, 精度设为 epson 可调);
5、MATLAB 编写程序(命令式 M 文件),分别利用精确搜索和不精确搜索 的最速下降法,求解如下问题:
精度为 0.001,初始点为(-1,1);
改变初始点为(-1.2,1)重新运行,观察运行结果。
本实验中函数用单独function计算
function y=f(x)
if(length(x)==1)
global xk;
global pk;
x=xk+x*pk;
end
y=100*(x(2)-x(1)^2)^2+(1-x(1))^2;
1.最速下降法的算法流程图
2、MATLAB 编写用数值微分法的梯度计算函数(函数式 M 文件);
function g=shuzhiweifenfa(x)
for i = 1:length(x)
m=zeros(1,length(x));
m(i)=(10^-3)/2;
g(i)=f(x+m)-f(x-m);
end
g=g/10^-3;
3、最速下降法求解无约束优化问题的函数,采用黄金分割法精确一维搜索,用数值微分法计算梯度(函数式 M 文件,精度设为 epson 可调);
function x=zuisuxiajiangfa_hjfg(e,x)
%step 1
%没用到k,只存储当前迭代的值。
global xk;
global pk;
while 1
%step 2
g=shuzhiweifenfa(x);
%step 3
%范数用的是平方和开根号
if sqrt(sum(g.^2))<=e
return;
end
pk=-g;
xk=x;
%这两个函数见之前代码(matlab无约束最优化的一般算法)
[a,b,c]=jintuifa(0,0.1);
a=huangjinfenge(a,c,10^-4);
%step 4
x=x+a*pk;
end
4、最速下降法求解无约束优化问题的函数,要求采用 Wolfe-Powell 非精确一维搜索,用数值微分法计算梯度(函数式 M 文件, 精度设为 epson 可调);
function a=Wolfe_Powell(x,pk)
%step 1
u=0.1;
b=0.5;
a=1;
n=0;
m=10^100;
%step 2
fx=f(x);
g=shuzhiweifenfa(x);
while 1
xk=x+a*pk;
fxk=f(xk);
gk=shuzhiweifenfa(xk);
if (fx-fxk)>=(-u*a*g*pk.')%(3-1)
if (gk*pk.')>=(b*g*pk.')%(3-2)
return;
else
%step 4
n=a;
a=min(2*a,(a+m)/2);
end
else
%step 3
m=a;
a=(a+n)/2;
end
end
function x=zuisuxiajiangfa_Wolfe(e,x)
%step 1
%没用到k,只存储当前迭代的值。
while 1
%step 2
g=shuzhiweifenfa(x);
%step 3
%范数用的是平方和开根号
if sqrt(sum(g.^2))<=e
return;
end
pk=-g;
a=Wolfe_Powell(x,pk);
%step 4
x=x+a*pk;
end
5、分别利用精确搜索和不精确搜索 的最速下降法,问题:
clear
clc
for i=1:2
if(i==1)
x=[-1,1];
fprintf('=========================');
fprintf('\nx=%f\t\t%f\n',x(1),x(2));
fprintf('=========================\n');
else
x=[-1.2,1];
fprintf('=========================');
fprintf('\nx=%f\t\t%f\n',x(1),x(2));
fprintf('=========================\n');
end
fprintf('精确搜索的最速下降法:\n');
x_=zuisuxiajiangfa_hjfg(10^-3,x);
fprintf('x*=%f\t%f\n',x_(1),x_(2));
fprintf('f(x)=%f\n',f(x_));
fprintf('不精确搜索的最速下降法\n');
x_=zuisuxiajiangfa_Wolfe(10^-3,x);
fprintf('x*=%f\t%f\n',x_(1),x_(2));
fprintf('f(x)=%f\n',f(x_));
end
结果:
版权声明
本文为[i道i]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_58196051/article/details/124331438
边栏推荐
- LotusDB 设计与实现—1 基本概念
- 1分钟看懂执行流程,永久掌握for循环(附for循环案例)
- Basic regular expression
- tcp_diag 内核相关实现 1 调用层次
- 浅谈skiplist在LevelDB的应用
- 外包干了四年,废了...
- Parameter stack pressing problem of C language in structure parameter transmission
- Arduino for esp8266串口功能简介
- Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
- TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
猜你喜欢
ASEMI整流模块MDQ100-16在智能开关电源中的作用
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
一个月把字节,腾讯,阿里都面了,写点面经总结……
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
外包干了四年,废了...
ASEMI超快恢复二极管与肖特基二极管可以互换吗
QT interface optimization: QT border removal and form rounding
QT actual combat: Yunxi chat room
详解TCP的三次握手
Nacos uses demo as configuration center (IV)
随机推荐
Solve the problem of SSH configuration file optimization and slow connection
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
QT interface optimization: double click effect
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
Flop effect
ArrayList集合基本使用
Upgrade of openssh and modification of version number
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
常见存储类型和FTP主被动模式解析
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
1N5408-ASEMI整流二极管1N5408
Outsourcing for four years, abandoned
QT interface optimization: QT border removal and form rounding
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
Matrix exchange row and column
C语言知识点精细详解——初识C语言【1】
A blog allows you to learn how to write markdown on vscode