How to perform minimization with fminsearch and fmincon

2 views (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 17 Jan 2024
%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 Comment
Walter Roberson
Walter Roberson on 17 Jan 2024
Edited: Walter Roberson on 17 Jan 2024
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);

Sign in to comment.

Categories

Find more on Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!