且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MAT之SA:T1编写主函数法和T2Matlab自带的SA工具箱GUI法,两种方法实现对一元函数优化求解

更新时间:2022-01-30 21:23:47

输出结果


https://imgconvert.csdnimg.cn/aHR0cHM6Ly9pbWFnZXMyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTMxMzQ3NS8yMDE4MDMvMTMxMzQ3NS0yMDE4MDMwMTIwMTc1NzMyMi00Mjg4NDMxMTQuZ2lm

实现代码

%SA:T1法利用Matlab编写主函数实现对一元函数优化求解——Jason niu

x = 1:0.01:2;

y = sin(10*pi*x) ./ x;  

figure

plot(x,y,'linewidth',1.5)

ylim([-1.5, 1.5])

xlabel('x')

ylabel('y')

title('SA:T1法利用Matlab编写主函数实现对一元函数y = sin(10*pi*x) / x优化求解—Jason niu')

hold on

[maxVal,maxIndex] = max(y);

plot(x(maxIndex), maxVal, 'r*','linewidth',2)

text(x(maxIndex), maxVal, {['    X: ' num2str(x(maxIndex))];['    Y: ' num2str(maxVal)]})  

hold on

[minVal,minIndex] = min(y);

plot(x(minIndex), minVal, 'gs','linewidth',2)

text(x(minIndex), minVal, {['    X: ' num2str(x(minIndex))];['    Y: ' num2str(minVal)]})