How to perform minimization with fminsearch and fmincon

2 ビュー (過去 30 日間)
Akhil
Akhil 2024 年 1 月 17 日
編集済み: Walter Roberson 2024 年 1 月 17 日
syms x [1 26]
syms y [1 26]
syms w [1 26]
f = 0;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
f = f + w(r2) * sqrt((x(r1)-a(i))^2 + (y(r1)-b(i))^2) - w(r1) * sqrt((x(r2)-a(i))^2 + (y(r2)-b(i))^2);
end
expr_func = matlabFunction(f, 'Vars', {[x, y, w]});
In the above code, i want to perform minimzation using fmin search. Also, how to ensure that f = f + w(r2) * sqrt((x(r1)-a(i))^2 + (y(r1)-b(i))^2) - w(r1) * sqrt((x(r2)-a(i))^2 + (y(r2)-b(i))^2) >=0

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 1 月 17 日
%guesses for initial conditions
x0 = linspace(-1,1,26);
y0 = linspace(-2,2,26);
w0 = linspace(-3,3,26);
xyw0 = [x0, y0, w0];
%the search
bestxyw = fminsearch(expr_fun, xyw0);
bestx = bestxyw(1:26);
besty = bestxyw(27:52);
bestw = bestxyw(53:78);
  1 件のコメント
Walter Roberson
Walter Roberson 2024 年 1 月 17 日
編集済み: Walter Roberson 2024 年 1 月 17 日
Ensuring that the partial expressions are >= 0.
This can be done with fmincon but not with fminsearch.
syms x [1 26]
syms y [1 26]
syms w [1 26]
sym parts [26 1]
f = sym(0);
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
partial = w(r2) * sqrt((x(r1)-a(i))^2 + (y(r1)-b(i))^2) - w(r1) * sqrt((x(r2)-a(i))^2 + (y(r2)-b(i))^2);
parts(i) = partial;
f = f + partial;
end
expr_func = matlabFunction(f, 'Vars', {[x, y, w]});
part_func = matlabFunction(-parts, 'Vars', {[x, y, w]});
A = []; b = [];
Aeq = []; beq = [];
lb = []; ub = [];
nonlcon = part_func;
%guesses for initial conditions
x0 = linspace(-1,1,26);
y0 = linspace(-2,2,26);
w0 = linspace(-3,3,26);
xyw0 = [x0, y0, w0];
%the search
bestxyw = fmincon(expr_fun, xyw0, A, b, Aeq, beq, lb, ub, nonlcon);
bestx = bestxyw(1:26);
besty = bestxyw(27:52);
bestw = bestxyw(53:78);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOptimization についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by