MATLAB nonlinear programming optimization problem

        The object of nonlinear programming research is the numerical optimization problem of nonlinear functions. It is a discipline formed in the 1950s. Its theory and application have developed very rapidly. With the development of computers, nonlinear programming has become more and more widely used. For Different problems propose special algorithms

Standard form of nonlinear programming

In practical work, we often encounter planning problems in which at least one of the objective function and constraints is a nonlinear function, that is, a nonlinear programming problem. The standard form of a nonlinear programming problem is

\min f(x)

s.t.\left\{\begin{matrix} g_i(x)\leqslant 0,i=1,2...,m \\ h_j(x)\leqslant 0,j=1,2...,r \end{matrix}\right.

Nonlinear programming problems can be divided into the following three categories according to constraints

1. Unconstrained nonlinear programming model

\min f(x)

x\subseteq \textbf{R}^n

2. Equality constrained nonlinear programming model

\min f(x)

s.t. h_j(x)=0,j=1,2,...,r

3. Inequality constrained nonlinear programming model

\min f(x)

s.t. g_i(x)=0,i=1,2,...,r

Among them, the unconstrained nonlinear programming model and the equality constrained nonlinear programming model are relatively simple. This article mainly introduces the inequality constrained nonlinear programming model, which can be solved with the matlab function fmincon.

The MATLAB calling format is as follows

x=fmincon(f,x0,A,b)
x=fmincon(f,x0,A,b,Aeq,beq)
x=fmincon(f,x0,A,b,Aeq,beq,lb,ub)
x=fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon)
x=fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)

Among them, x0 is the original point, A and b are the coefficient matrix and right-hand column vector of the inequality constraint respectively, lb and ub are the lower and upper bounds of the variable x respectively, and options are the specified optimization parameters for minimization.

MATLAB specific examples

        There is a fund of 1 million yuan, which is required to be used within 5 years. If the fund is used x 10,000 yuan within one year, a benefit of \sqrt{2x}10,000 yuan can be obtained (the benefit cannot be used again). The unused funds can be deposited in the bank with an annual interest rate of 5%. , try to formulate a fund use plan to maximize the sum of benefits over the five years.

        Let’s convert it into a mathematical problem first. Assume that the variable x_irepresents ithe amount of funds used in year 1. zThen there is the following optimization problem

\max z=\sqrt{2x_1}+ \sqrt{2x_2}+ \sqrt{2x_3}+ \sqrt{2x_4}+ \sqrt{2x_5}

x_1\leqslant 100

1.05x_1+x_2\leqslant 105

1.1025x_1+1.05x_2+x_3\leqslant 110.25

1.1576x_1+1.1025x_2+1.05x_3+x_4\leqslant 115.76

1.2155x_1+1.1576x_2+1.1025x_3+1.05x_4+x_5\leqslant 115.76

x_i\geqslant 0,i=1,2,3,4,5

Write the MATLAB program based on the above as follows

%%  非线性规划问题求解
%   Copyright 2023, NUDT
%
%   nudt04
%  
%   说明:
%       不等式非线性规划问题求解
%
%   输入:
%       目标函数和约束条件
%   输出:
%       最优解
%
%   原始作者:帅小吉
%   建立日期:2023年10月7日
%
%   更新历史:
%         无
clear all
clc
%% 求解过程
x0=[10,10,10,10,10];%初始值
A=[1.05,1,0,0,0
    1.1025,1.05,1,0,0
    1.1576,1.1025,1.05,1,0
    1.2155,1.1576,1.1025,1.05,1];%不等式矩阵
b=[105;110.25;115.76;121.55];%不等式矩阵右侧
Aeq=[];%等式矩阵
beq=[];%等式右侧
lb=[0;0;0;0;0];%变量下界约束
ub=[];%变量上界约束
[x,fval]=fmincon(@fun1,x0,A,b,Aeq,beq,lb,ub)%函数求解
%% 目标函数
function f=fun1(x)
    f=-sqrt(2*x(1))-sqrt(2*x(2))-sqrt(2*x(3))-sqrt(2*x(4))-sqrt(2*x(5));
end

Obtain the optimal solution to this problem through MATLAB algorithm

In the same way, you can also use the partial function method to compile the objective function and constraints into functions for solution. Examples are as follows:

%%  非线性规划问题求解
%   Copyright 2023, NUDT
%   nudt04
%   说明:
%       不等式非线性规划问题求解
%   输入:
%       目标函数和约束条件
%   输出:
%       最优解
%   原始作者:帅小吉
%   建立日期:2023年10月7日
%   更新历史:
%         无
clear all
clc
clear all
clc
x0=[1;1;1;1;1];
vlb=[0;0;0;0;0];
vub=[100;100;100;100;100];
A=[];
b=[];
Aeq=[];
beq=[];
[x,fval]=fmincon(@fu,x0,A,b,Aeq,beq,vlb,vub,@myco)
%% 约束条件
function [g,ceq]=myco(x)
g(1)=x(1)-100;
g(2)=1.05*x(1)+x(2)-105;
g(3)=1.1025*x(1)+1.05*x(2)+x(3)-110.25;
g(4)=1.1576*x(1)+1.1025*x(2)+1.05*x(3)+x(4)-115.76;
g(5)=1.2155*x(1)+1.1576*x(2)+1.1025*x(3)+1.05*x(4)+x(5)-121.55;
ceq=0
end
%% 目标函数
function f=fu(x)
f=-(sqrt(2*x(1))+sqrt(2*x(2))+sqrt(2*x(3))+sqrt(2*x(4))+sqrt(2*x(5)));
end

The running results are consistent and the algorithm is effective

Guess you like

Origin blog.csdn.net/weixin_53750855/article/details/133634132