How to perform minimization with fminsearch and fmincon

조회 수: 4 (최근 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 CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by