# nonlinear programming

catalogue

1, Definition of nonlinear programming

2, Nonlinear programming model

3, Nonlinear programming function

4, Linear inequality constraints

5, Linear inequalities and equality constraints

6, Optimization with nonlinear constraints

7, Nonlinear constraint

## 1, Definition of nonlinear programming

Previously, we learned linear programming and integer programming. We can understand integer programming as a special linear programming.
In real life, we think more that the data is nonlinear. For linear programming, it will be a small amount after all. Therefore, we introduce nonlinear programming.

If the objective function or constraints contain nonlinear functions, this kind of programming problem is called nonlinear programming problem. Generally speaking, solving nonlinear programming is much more difficult than solving linear programming. Moreover, unlike the general method of simplex method in linear programming, there is no general algorithm suitable for various problems in nonlinear programming, and each method has its own specific scope of application.

## 2, Nonlinear programming model

The mathematical model of nonlinear programming in Matlab is written in the following form: The commands in Matlab are:

`[x,y]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)`

## 3, Nonlinear programming function

fmincon function is used to find the minimum value of constrained nonlinear multivariable function. How does this function work?
This is the syntax format:

```%1.from x0 Start by trying to satisfy linear inequalities A*x ≤ b Looking for fun The minimum value point of the function described in x. x0 Can be scalar, vector, or matrix.
x = fmincon(fun,x0,A,b)
%2.In satisfying the linear equation Aeq*x = beq And inequality A*x ≤ b Minimize when fun. If there is no inequality, set A = [] and b = [].
x = fmincon(fun,x0,A,b,Aeq,beq)
%3. yes x The design variables in define a set of lower and upper bounds so that the solution is always lb ≤ x ≤ ub Within. If no equation exists, set Aeq = [] and beq = []. If x(i) No lower bound, please set lb(i) = -Inf，If x(i) No upper bound, please set ub(i) = Inf.
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
%4.use options Minimize the specified optimization options. use optimoptions These options can be set. If there are no nonlinear inequality or equality constraints, set nonlcon = [].
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
```

What does the parameter mean?

```b and beq It's a vector, A and Aeq It's a matrix, c(x) and ceq(x) Is a function that returns a vector, f(x) Is a function that returns a scalar. f(x),c(x) and ceq(x) It can be a nonlinear function.
x,lb and ub It can be passed as a vector or matrix
```

## 4, Linear inequality constraints Objective function:

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

In this case, the function to be defined is x

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

Constraints: from point   [-1,2]   Find the minimum value for the starting point and constrain the equation   x(1)+2x(2)≤1
matlab code:

```clc
clear all

fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;%Anonymous function
x0 = [-1,2];%The starting point can be changed, which seems to have no impact
A = [1,2];
b = 1;
[x,y] = fmincon(fun,x0,A,b)%Apply function
```

function: ## 5, Linear inequalities and equality constraints

The requirements are as follows: matlab code:

```clc
clear all
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;%Anonymous function

x0 = [1,1];%The initial point setting is different
A = [1,-2];%Coefficient of inequality variable
b = 1;%Value to the right of inequality
Aeq = [2,1];%Coefficient of equality variable
beq = 1;%Value to the right of the equation
[x,y] = fmincon(fun,x0,A,b,Aeq,beq)%solve x And the function value at this time y
```

result: ## 6, Optimization with nonlinear constraints Objective function:

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

Constraints:

```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
```

matlab solution:

1. Main function

```%% Main function
options=optimset('largescale','off');
[x,y] = fmincon(@fun,rand(3,1),[],[],[],[],zeros(3,1),[], @nonlcon, options)
```

2. Objective function (end with end)

The objective function is a minimization function. fun is a function. fun accepts a vector or array x and returns a real scalar f, that is, the objective function value calculated at x.

```%% objective function
function f=fun(x)
f=sum(x.^2)+8;
end```

3. Add constraints (end with end)

Nonlinear constraint, nonlcon is a function that accepts a vector or array X and returns two arrays c(x) and ceq(x).

```%% Nonlinear constraints
function [c,ceq]=nonlcon(x)
c=[-x(1)^2+x(2)-x(3)^2
x(1)+x(2)^2+x(3)^3-20];  %Nonlinear inequality constraints
ceq=[-x(1)-x(2)^2+2
x(2)+2*x(3)^2-3]; %Nonlinear equality constraint
end```

Total code: (first define the objective function and constraint conditions respectively, and finally solve them with the main function)

```clc
clear all
%% Main function
options=optimset('largescale','off');
[x,y] = fmincon(@fun,rand(3,1),[],[],[],[],zeros(3,1),[], @nonlcon, options)

%% objective function
function f=fun(x)
f=sum(x.^2)+8;
end

%% Nonlinear constraints
function [c,ceq]=nonlcon(x)
c=[-x(1)^2+x(2)-x(3)^2
x(1)+x(2)^2+x(3)^3-20];  %Nonlinear inequality constraints
ceq=[-x(1)-x(2)^2+2
x(2)+2*x(3)^2-3]; %Nonlinear equality constraint
end
```

function: ## 7, Nonlinear constraint

Find the minimum point of the objective function in the circle under boundary constraints. Objective function:

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

Further, the anonymous function can be obtained:

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

Constraints:

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

In this step, you can get:

```lb = [0,0.2];
ub = [0.5,0.8];```

At the same time, find in the circle with [1 / 3,1 / 3] as the center and radius of 1 / 3, and write a function code as follows:

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

There are no linear constraints, so set these parameters to [] (null)

```A = [];
b = [];
Aeq = [];
beq = [];```

Therefore, all codes are:
Function code:

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

Main code:

```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)
```

function: # Summary:

I have been studying for the third day and deeply feel the improvement of my ability, not only in programming, but also in other aspects. I feel the speed of codeword!!! Thank you very much, boss @Sichuan rookie

Posted on Wed, 10 Nov 2021 13:41:20 -0500 by DapperDanMan