The third day of MATLAB learning punch card - nonlinear programming (1)

Table of contents

1. Citation

2. Analysis

3. Linear inequality constraints

4. Linear Inequalities and Equality Constraints

5. Find the maximum value with nonlinear constraints

6. Nonlinear Constraints


1. Citation

The original tutorial is "Punching Cards on the Second Day - Integer Programming (1)" by Chuanchuan Cainiao

The link checks in for the third day - nonlinear programming (1) - Programmer Sought

Self-collection by those in need.

2. Analysis

In the first two days, we systematically studied linear programming. We are now able to use MATLAB proficiently to process the linear programming model.

But in real life, how many problems can we just use the linear programming model to solve?
In the final analysis, there are only a few problems that can be exactly linear programming, so we can't use linear programming, what should we use? That is naturally a nonlinear programming.

The model of nonlinear programming is as follows

Of course, there is no need to say more about the first and second lines, but some students may be confused when they see the third and fourth lines. In fact, it is easy to understand that the third and fourth lines are the first two lines in the nonlinear state. For example, X^2+2X<=6, X^2-X=12 can be placed in the third row and the fourth row respectively.

At the same time, in order to solve the problem of nonlinear programming, our ordinary formula two days ago was a bit stretched. To this end, I will introduce a new function to you: the fmincon function. The fmincon function is used to find the minimum of a constrained nonlinear multivariate function. How to use this function?

This is the syntax format:

x = fmincon(fun,x0,A,b) 从 x0 开始,尝试在满足线性不等式 A*x ≤ b 的情况下寻找 fun 中所述的函数的最小值点 x。x0 可以是标量、向量或矩阵。
x = fmincon(fun,x0,A,b,Aeq,beq) 在满足线性等式 Aeq*x = beq 以及不等式 A*x ≤ b 的情况下最小化 fun。如果不存在不等式,则设置 A = [] 和 b = []。
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub) 对 x 中的设计变量定义一组下界和上界,使解始终在 lb ≤ x ≤ ub 范围内。如果不存在等式,请设置 Aeq = [] 和 beq = []。如果 x(i) 无下界,请设置 lb(i) = -Inf,如果 x(i) 无上界,请设置 ub(i) = Inf。
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) 使用 options 所指定的优化选项执行最小化。使用 optimoptions 可设置这些选项。如果没有非线性不等式或等式约束,请设置 nonlcon = []。

And the parameters in this

b 和 beq 是向量,A 和 Aeq 是矩阵,c(x) 和 ceq(x) 是返回向量的函数,f(x) 是返回标量的函数。f(x)、c(x) 和 ceq(x) 可以是非线性函数。
x、lb 和 ub 可以作为向量或矩阵传递

can't read? It doesn't matter, let us combine the topic to learn it well.

3. Linear inequality constraints

Objective function:

fun =   (x)100*(x(2)-x(1)^2)^2+(1-x(1))^2

Constraint equation: X(1)+2X(2)<=1

So, let's write MATLAB as

 Look, isn't it simple?

4. Linear Inequalities and Equality Constraints

Let's look at the question, what's more than ours?

One more equation, right?

So analogous to linear programming, we can

It can also be remembered by analogy with linear programming. 

5. Find the maximum value with nonlinear constraints

Objective function:

min f (x) = x1^2 + x2^2 + x3^2 + 8

Restrictions:

x1^2 − x2 + x3 ^2 ≥ 0
x1 + x2^2 + x3 ^3 ≤ 20
− x1 − x2^2 + 2 = 0
x2 + 2x3^2 = 3x1
x1,x2 , x3 ≥ 0

So, MATLAB:

 insert image description here

The objective function is the function to be minimized, and fun is a function that accepts a vector or array x and returns a real scalar f, the value of the objective function computed at x.
For nonlinear constraints, nonlcon is a function that accepts a vector or array x and returns two arrays c(x) and ceq(x).

6. Nonlinear Constraints

When we ask you to find the minimum value of an objective function within the range of (1/3, 1/3) as the center and 1/3 as the radius

Objective function:

fun =   (x)100*(x(2)-x(1)^2)^2+(1-x(1))^2

after anonymous

fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;

Restrictions:

 0 ≤ x (1) ≤ 0.5 0
 0.2≤x(2)≤0.8

then

The function code is

function [c,ceq] = circle(x)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 - (1/3)^2;
ceq = [];
end

The master code is

clc 
clear all
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
lb = [0,0.2];
ub = [0.5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];


nonlcon = @circle;
[x,y] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

insert image description here

Guess you like

Origin blog.csdn.net/m0_57011726/article/details/121260530