带约束的多元函数最小值。
1、Fmincon函数求解多元函数的最小值
求函数 $ e^{(x+y)^2}+(x-1)^2 $的最小值。
已知:
1、尝试在无约束条件下求解全局最小值:
1 | fun=@(x,y)exp((x+y).^2)+(x-1).^2 |
2、尝试在有约束条件下求解局部最小值:
1 | clear;clc |
3、定义函数求解局部最小值最小值。
1 | clear;clc |
1 | opts=optimset('algorithm','sqp')%更换求解算法 |
4、求解最小值一般步骤
1 | clear;clc |
定义函数:
fun=@(x)0.25*pi*x(1)*(10*x(2)^2*x(3)^2-x(5)^2-x(6)^2)+0.25*pi*x(4)*(x(5)^2+x(6)^2);
确定初始值:
x0=[100;50;5;200;100;120];
编写求解器:
[x,fval,exitflag]=fmincon(fun,x0,[],[],[],[],[],[],@My_con)
编写约束函数:
My_con
5、运算过程
2、用遗传算法求解(GA)
1、遗传算法调用方式:
- fun:定义为待求解函数
- nvars:定义为变量数目
- A:定义为非线性约束系数
- b:定义为:非线性约束常数
- Aeq:定义为线性约束系数
- beq:定义为线性约束常数
- lb:定义为自变量下限约束
- ub:定义为自变量上限约束
2、调用GA求解问题
例子:
约束:
求解算法:
1 | clear;clc |
3、机械优化作业
1、第一二此作业主要采用fmincon函数和遗传算法
下面两道题分别采用了这两种算法,只需要修改我标注的需要修改的部分为题目要求即可完成运算。
代码:
1 | clear;clc |
结果:
2、黄金搜索法
修改我标注修改的部分即可完成任务。
Homework 4
The objective function is $f(x)=x_2-7x+10$ ,Suppose the initial value are $x_0=0$,$h_0=1$, allowable error is $eps=0.01$;
- Find the interval that contains the minimum.
- Use the Golden search method and Quadratic interpolation method and Newton method, to find the optimal solution.
1 | clear;clc |
3、牛顿法
1 | clear;clc |
4、二次插值法
1 | clear;clc |