• MATLB|和她跌宕起伏最终到达人生之峰【浪漫旅途】


    ✨🍒🍒🍒🏆🏆🏆😜😜😜🌈🌈🌈💕💕💕💓💓📋📋📋🍺🍺🍺🏅🏅🏅💞💞💞🏆🏆🏆❤️❤️❤️✨✨✨☁️☁️☁️⛅⛅⛅

    拥有海一样的胸怀,才能有海一样的人生;拥有海一样的宁静,才能镇得住波涛汹涌。做人如海,有跌宕起伏,有波澜不惊。 

    💥💥💥💞💞💞欢迎来到本博客❤️❤️❤️💥💥💥

    🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者,博主专门做了一个专栏目录,整个专栏只放了一篇文章,足见我对其重视程度:博主专栏目录。做到极度细致,方便大家进行学习!亲民!!!还有我开了一个专栏给女朋友的,很浪漫的喔,代码学累的时候去瞧一瞧,看一看:女朋友的浪漫邂逅。有问题可以私密博主,博主看到会在第一时间回复。
     

                              

                                        🎉🎉欢迎您的到来🎉🎉

                         ⛅⛅⛅ 📃个人主页:科研室🌈🌈🌈

                        📚📚📚📋所有代码目录:电气工程科研社👨‍💻👨‍💻👨‍💻

                               

    【现在公众号名字改为:荔枝科研社】

    📋📋📋本文目录如下:⛳️⛳️⛳️

    目录

    💝漂亮的运行结果 

    💝Matlab代码 

     

    💝漂亮的运行结果 

    💝Matlab代码 

    1. function [] = LocalMinimaSuffring()
    2. close all; clear all;
    3. global history
    4. history = [];
    5. % *************峰函数****************
    6. dx = 1/8;
    7. [x,y] = meshgrid(-3:dx:3);
    8. z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
    9. - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
    10. - 1/3*exp(-(x+1).^2 - y.^2);
    11. % Self demonstration
    12. surfc(x,y,z)
    13. axis('tight')
    14. xlabel('x'), ylabel('y'), title('峰值')
    15. %% ******************开始********************
    16. x0 = [-3 -3];
    17. y0 = peaksObj(x0);
    18. hold on
    19. plot3(x0(1),x0(2),-10,'gs','lineWidth',2,'MarkerSize',10);
    20. plot3(x0(1),x0(2),y0,'gs','lineWidth',2,'MarkerSize',10);
    21. % [c,ceq] = peaksCon(x0)
    22. options = optimset('Display','iter','OutputFcn',@peaksOutputFcn);
    23. x = fmincon(@peaksObj,x0,[],[],[],[],[],[],@peaksCon,options)
    24. plot3(history(:,1),history(:,2),(-10)*ones(size(history,1)),'-r.','lineWidth',1,'MarkerSize',15)
    25. plot3(history(:,1),history(:,2),history(:,3),'-r.','lineWidth',1,'MarkerSize',15)
    26. plot3(history(end,1),history(end,2),-10,'r*','lineWidth',2,'MarkerSize',10)
    27. plot3(history(end,1),history(end,2),history(end,3),'r*','lineWidth',2,'MarkerSize',10)
    28. %% ******************x0 = [3 -2];********************
    29. history = [];
    30. x0 = [3 -2];
    31. hold on
    32. plot3(x0(1),x0(2),-10,'go','lineWidth',2,'MarkerSize',10);
    33. plot3(x0(1),x0(2),y0,'go','lineWidth',2,'MarkerSize',10);
    34. options = optimset('Display','iter','OutputFcn',@peaksOutputFcn);
    35. x = fmincon(@peaksObj,x0,[],[],[],[],[],[],@peaksCon,options)
    36. plot3(history(:,1),history(:,2),(-10)*ones(size(history,1)),'-r.','lineWidth',1,'MarkerSize',15)
    37. plot3(history(:,1),history(:,2),history(:,3),'-r.','lineWidth',1,'MarkerSize',15)
    38. plot3(history(end,1),history(end,2),-10,'r*','lineWidth',2,'MarkerSize',10)
    39. plot3(history(end,1),history(end,2),history(end,3),'r*','lineWidth',2,'MarkerSize',10)
    40. box on
    41. end
    42. function stop = peaksOutputFcn(x, optimValues,state)
    43. stop =false;
    44. % hold on;
    45. % plot3(x(1),x(2),-10,'*');
    46. record(x,optimValues.fval);
    47. end
    48. function []=record(x,y)
    49. global history
    50. history=[history;[x,y]]
    51. end
    52. function f = peaksObj(x)
    53. %PEAKSOBJ casts PEAKS function to a form accepted by optimization solvers.
    54. % PEAKSOBJ(X) calls PEAKS for use as an objective function for an
    55. % optimization solver. X must conform to a M x 2 or N x 2 array to be
    56. % valid input.
    57. %
    58. % Syntax
    59. % f = peaksObj(x)
    60. %
    61. % Example
    62. % x = [ -3:1:3; -3:1:3]
    63. % f = peaksObj(x)
    64. %
    65. % See also peaks
    66. % Check x size to pass data correctly to PEAKS
    67. [m,n] = size(x);
    68. if (m*n) < 2
    69. error('peaksObj:inputMissing','Not enough inputs');
    70. elseif (m*n) > 2 && (min(m,n) == 1) || (min(m,n) > 2)
    71. error('peaksObj:inputError','Input must have dimension m x 2');
    72. elseif n ~= 2
    73. x = x';
    74. end
    75. % Objective function
    76. f = peaks(x(:,1),x(:,2));
    77. end
    78. function [c,ceq] = peaksCon(x)
    79. %PEAKSCON Constraint function for optimization with PEAKSOBJ
    80. % PEAKSCON(X) is the constraint function for use with PEAKSOBJ. X is of
    81. % size M x 2 or 2 x N.
    82. %
    83. % Sytnax
    84. % [c,ceq] = peaksCon(x)
    85. %
    86. % See also peaksobj, peaks
    87. % Check x size to pass data correctly to constraint definition
    88. [m,n] = size(x);
    89. if (m*n) < 2
    90. error('peaksObj:inputMissing','Not enough inputs');
    91. elseif (m*n) > 2 && (min(m,n) == 1) || (min(m,n) > 2)
    92. error('peaksObj:inputError','Input must have dimension m x 2');
    93. elseif n ~= 2
    94. x = x';
    95. end
    96. % Set plot function to plot constraint boundary
    97. try
    98. mypref = 'peaksNonlinearPlot';
    99. if ~ispref(mypref)
    100. addpref(mypref,'doplot',true);
    101. else
    102. setpref(mypref,'doplot',true);
    103. end
    104. catch
    105. end
    106. % Define nonlinear equality constraint
    107. ceq = [];
    108. % Define nonlinear inequality constraint
    109. % x1^2 + x^2 <= 3^2
    110. c = x(:,1).^2 + x(:,2).^2 - 9;
    111. % fmincon accepted input form is ceq <= 0
    112. end

  • 相关阅读:
    Codeforces暑期训练周报(8.22~8.28)
    react路由的匹配模式(模糊匹配、精确匹配)
    Map和Set常见操作汇总
    无线通信模块定点传输-点对多点的具体传输应用
    【docker】debian安装官方docker源解析IP404问题
    C语言之指针详解
    【多标签, 极限的多标签算法】评价指标梳理
    目标检测YOLO实战应用案例100讲-森林野火预警的小目标检测
    Spring入门第一讲——Spring框架的快速入门
    lodash已死?radash最全使用介绍(附源码说明)—— Array方法篇(4)
  • 原文地址:https://blog.csdn.net/weixin_46039719/article/details/126248377