当前位置:网站首页>MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]
MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]
2022-08-10 01:28:00 【Electrical Engineering Workshop】
️️️️️️
Have the same mind as the sea,To have a life like the sea;Has the tranquility of the sea,To be able to withstand the turbulent waves.Be like a sea,有跌宕起伏,There was placidity.
欢迎来到本博客️️️
博主优势:博客内容尽量做到思维缜密,逻辑清晰,为了方便读者,博主专门做了一个专栏目录,整个专栏只放了一篇文章,足见我对其重视程度:博主专栏目录.做到极度细致,方便大家进行学习!亲民!!!还有我开了一个专栏给女朋友的,很浪漫的喔,代码学累的时候去瞧一瞧,看一看:女朋友的浪漫邂逅.有问题可以私密博主,博主看到会在第一时间回复.
欢迎您的到来
个人主页:科研室
所有代码目录:电气工程科研社
【现在公众号名字改为:荔枝科研社】
本文目录如下:️️️
目录
Nice running result
Matlab代码
function [] = LocalMinimaSuffring()
close all; clear all;
global history
history = [];
% *************Peak function****************
dx = 1/8;
[x,y] = meshgrid(-3:dx:3);
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2);
% Self demonstration
surfc(x,y,z)
axis('tight')
xlabel('x'), ylabel('y'), title('峰值')
%% ******************开始********************
x0 = [-3 -3];
y0 = peaksObj(x0);
hold on
plot3(x0(1),x0(2),-10,'gs','lineWidth',2,'MarkerSize',10);
plot3(x0(1),x0(2),y0,'gs','lineWidth',2,'MarkerSize',10);
% [c,ceq] = peaksCon(x0)
options = optimset('Display','iter','OutputFcn',@peaksOutputFcn);
x = fmincon(@peaksObj,x0,[],[],[],[],[],[],@peaksCon,options)
plot3(history(:,1),history(:,2),(-10)*ones(size(history,1)),'-r.','lineWidth',1,'MarkerSize',15)
plot3(history(:,1),history(:,2),history(:,3),'-r.','lineWidth',1,'MarkerSize',15)
plot3(history(end,1),history(end,2),-10,'r*','lineWidth',2,'MarkerSize',10)
plot3(history(end,1),history(end,2),history(end,3),'r*','lineWidth',2,'MarkerSize',10)
%% ******************x0 = [3 -2];********************
history = [];
x0 = [3 -2];
hold on
plot3(x0(1),x0(2),-10,'go','lineWidth',2,'MarkerSize',10);
plot3(x0(1),x0(2),y0,'go','lineWidth',2,'MarkerSize',10);
options = optimset('Display','iter','OutputFcn',@peaksOutputFcn);
x = fmincon(@peaksObj,x0,[],[],[],[],[],[],@peaksCon,options)
plot3(history(:,1),history(:,2),(-10)*ones(size(history,1)),'-r.','lineWidth',1,'MarkerSize',15)
plot3(history(:,1),history(:,2),history(:,3),'-r.','lineWidth',1,'MarkerSize',15)
plot3(history(end,1),history(end,2),-10,'r*','lineWidth',2,'MarkerSize',10)
plot3(history(end,1),history(end,2),history(end,3),'r*','lineWidth',2,'MarkerSize',10)
box on
end
function stop = peaksOutputFcn(x, optimValues,state)
stop =false;
% hold on;
% plot3(x(1),x(2),-10,'*');
record(x,optimValues.fval);
end
function []=record(x,y)
global history
history=[history;[x,y]]
end
function f = peaksObj(x)
%PEAKSOBJ casts PEAKS function to a form accepted by optimization solvers.
% PEAKSOBJ(X) calls PEAKS for use as an objective function for an
% optimization solver. X must conform to a M x 2 or N x 2 array to be
% valid input.
%
% Syntax
% f = peaksObj(x)
%
% Example
% x = [ -3:1:3; -3:1:3]
% f = peaksObj(x)
%
% See also peaks
% Check x size to pass data correctly to PEAKS
[m,n] = size(x);
if (m*n) < 2
error('peaksObj:inputMissing','Not enough inputs');
elseif (m*n) > 2 && (min(m,n) == 1) || (min(m,n) > 2)
error('peaksObj:inputError','Input must have dimension m x 2');
elseif n ~= 2
x = x';
end
% Objective function
f = peaks(x(:,1),x(:,2));
end
function [c,ceq] = peaksCon(x)
%PEAKSCON Constraint function for optimization with PEAKSOBJ
% PEAKSCON(X) is the constraint function for use with PEAKSOBJ. X is of
% size M x 2 or 2 x N.
%
% Sytnax
% [c,ceq] = peaksCon(x)
%
% See also peaksobj, peaks
% Check x size to pass data correctly to constraint definition
[m,n] = size(x);
if (m*n) < 2
error('peaksObj:inputMissing','Not enough inputs');
elseif (m*n) > 2 && (min(m,n) == 1) || (min(m,n) > 2)
error('peaksObj:inputError','Input must have dimension m x 2');
elseif n ~= 2
x = x';
end
% Set plot function to plot constraint boundary
try
mypref = 'peaksNonlinearPlot';
if ~ispref(mypref)
addpref(mypref,'doplot',true);
else
setpref(mypref,'doplot',true);
end
catch
end
% Define nonlinear equality constraint
ceq = [];
% Define nonlinear inequality constraint
% x1^2 + x^2 <= 3^2
c = x(:,1).^2 + x(:,2).^2 - 9;
% fmincon accepted input form is ceq <= 0
end
边栏推荐
猜你喜欢
Today's sleep quality record 61 points
Eureka protects itself
数字钱包红海角逐,小程序生态快速引入可助力占领智慧设备入口
ES6 从入门到精通 # 13:数组的扩展方法二
分布式数据库难题(三):数据一致性
Golden Warehouse Database KingbaseGIS User Manual (6.4. Geometry Object Access Function)
如何抑制告警风暴?
字节技术面都过了,薪资都谈好了20K*13结果还是被刷了,问HR原因是。。。
首席信息官如何将可持续性和技术结合起来
Golden Warehouse Database KingbaseGIS User Manual (6.5. Geometry Object Editing Function)
随机推荐
Golden Warehouse Database KingbaseGIS User Manual (6.4. Geometry Object Access Function)
Pinduoduo store operation must know to leave a little knowledge of operation
61.【快速排序法详解】
上交所实时行情文件汇总
【励志】名言警句
数据库的备份与恢复「建议收藏」
分布式数据库难题(三):数据一致性
JSP简介
【C语言】通讯录《静态内存版本》
深入理解Aarch64内存管理
了解什么是架构基本概念和架构本质
直播平台怎么搭建,原生js实现编辑器撤消/恢复功能
CAD 绘制圆角处理
Jpa 查询view or 无主键的table
信息系统项目管理师核心考点(六十四)信息安全基础知识重要概念
conda新建环境时报错NotWritableError: The current user does not have write permissions
ES6 从入门到精通 # 12:数组的扩展方法一
NTU General Database-Gbase-8a-Learning-04-Deploying Distributed Clusters
新开窗口 展示协议
selenium和驱动安装