Clear Filters
Clear Filters

optimization problem to find variables

2 views (last 30 days)
Akhil
Akhil on 30 Jan 2024
Answered: ag on 5 Feb 2024
In the following code, i am trying to optimze the function f in order to find values of x,y and w. We are provided with values of a,b,reg1 and reg2.
In the code, i want to find values of x,y and w such that
value of f and g becomes equal. Is the following code is correct. I am only keeping some section of the entire code
function f = objective(vars, a, b, reg1, reg2)
x = vars(1:800);
y = vars(801:1600);
w = vars(1601:end);
f = 0;
g=0;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
f = (((sqrt((x(r1)-a(i)) + (y(r1)-b(i))))./(sqrt((x(r2)-a(i)) + (y(r2)-b(i))))) );
g=w(r1)*w(r2);
f=g;

Answers (1)

ag
ag on 5 Feb 2024
Hi Akhil,
To find out the values x,y and w such that value of f and g becomes equal, you can break the loop once the desired condition is hit. This can be done by adding a "if" block inside the for loop as shown below:
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
f = (((sqrt((x(r1)-a(i)) + (y(r1)-b(i))))./(sqrt((x(r2)-a(i)) + (y(r2)-b(i))))) );
g=w(r1)*w(r2);
if f == g % if block, to check if f and g are equal
%storing the optimal values of x, y and w corresponding to "r1" and
%"r2"
xOptim1 = x(r1);
xOptim2 = x(r2);
yOptim1 = y(r1);
yOptim2 = y(r2);
wOptim1 = w(r1);
wOptim2 = w(r2);
break; %break out of the loop with the stored values.
end
end
Hope this helps!

Community Treasure Hunt

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

Start Hunting!