产生要在MATLAB中优化的非线性函数

问题描述 投票:0回答:1

我正在尝试最小化此非线性函数

enter image description here

使用MATLAB中的BFGS优化方法。但是,我花了很多时间弄清楚如何为n = 500编写函数代码。我没有运气就尝试过这些变化:

1)S_symsum = @(x) (100*(x(2*(1:250))-x(2*(1:250)-1)^2)^2+(1-x(2*(1:250)-1))^2);

2)k = 1:N/2; V = (100*(x(2*k)-x(2*k-1).^2).^2+(1-x(2*k-1)).^2); f = sum(V);

3)f = symsum(100*(x(2*k)-x(2*k-1).^2).^2+(1-x(2*k-1)).^2,k,1,500)

matlab optimization nonlinear-optimization
1个回答
0
投票

我使用N = 5进行说明,为您的优化只需将其更改为500。

% given length of x, here N
N = 5;
% supposed x is a row vector 
 x = sym('x', [1,N]);
mySum = 0;
 for i = 1:N/2
    mySum =   mySum + 100.*(x(2*i) - (x(2*i-1)).^2) + (1-x(2*i-1)).^2;
 end

% mySum is a function of x, but a syms function
% not suitable for using fmincon optimizer 
% convert sum to function handle
% 'vars', {x} allows you to pass a row vector as input
% 'vars', {x.'} allows you to pass a column vector as input

f = matlabFunction(mySum, 'vars', {x});

% starts optmization using fmincon
x0 = 1:5;
A = [];
b = [];
Aeq = [2, 1, 3, 2, 5];
beq = 100;
sol =  fmincon(f,x0,A,b,Aeq,beq);

%sol
sol =

   1.0e+09 *

    0.1948   -0.2528    1.7829   -0.4372   -0.9222

© www.soinside.com 2019 - 2024. All rights reserved.