📄 gradfmu.m
字号:
function g = gradfmu (f,p,q,x,mu)
%-----------------------------------------------------------------------
% Usage: g = gradfmu (f,p,q,x,mu)
%
% Description: Numerically approximate the gradient of the generalized
% objective function F(x) using central differences.
%
% F(x) = f(x) + mu*(P(x) + Q(x))
%
% Inputs: f = name of objective function to minimize: f(x)
% p = name of equality constraint function: p(x) = 0
% q = name of inequality constraint function: q(x) >= 0
%
% The forms of f, p, and q are:
%
% function y = f(x)
% function u = p(x)
% function v = q(x)
%
% When f is called with n by 1 vector x, it must
% return the scalar y = f(x). When p is called with
% n by 1 vector x,it must compute r by 1 vector
% u = p(x). When q is called with n by 1 vector x,
% it must compute s by 1 vector v = q(x).
%
% x = n by 1 vector of independent variables
% mu = penalty paramter (mu >= 0)
%
% Outputs: g = n by 1 gradient vector g(k) = dF(x)/dx(k)
% where:
%
% F(x) = f(x) + mu*(P(x) + Q(x))
%
% Here P(x) and Q(x) are the penalty terms:
%
% P(x) = p'(x)p(x)
% Q(x) = min([0,q(x)])'min([0,q(x)])
%------------------------------------------------------------------------
% Initialize
chkvec (x,4,'gradfmu');
em = 0.001;
n = length (x);
g = zeros (n,1);
% Compute gradient
for i = 1 : n
x(i) = x(i) + em;
g(i) = fmu (f,p,q,x,mu);
x(i) = x(i) - 2*em;
g(i) = (g(i) - fmu(f,p,q,x,mu))/(2*em);
x(i) = x(i) + em;
end
%------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -