optimization problem to find variables

2 ビュー (過去 30 日間)
Akhil
Akhil 2024 年 1 月 30 日
回答済み: ag 2024 年 2 月 5 日
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;

回答 (1 件)

ag
ag 2024 年 2 月 5 日
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!

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by