Borrar filtros
Borrar filtros

Modifying function to get only one value

2 visualizaciones (últimos 30 días)
Akhil
Akhil el 2 de Abr. de 2024
Respondida: Manikanta Aditya el 8 de Abr. de 2024
How to modify the following function such that output values (x,y,w) come out either for:
abs((w(r2) - ((x(r1)-a(i))^2 + (y(r1)-b(i))^2)) > (w(r1) * ((x(r2)-a(i))^2 + (y(r2)-b(i))))^2)
or
abs((w(r2) - ((x(r1)-a(i))^2 + (y(r1)-b(i))^2)) < (w(r1) * ((x(r2)-a(i))^2 + (y(r2)-b(i))))^2),
the original code is mentioned below
function f = objective(vars, a, b, reg1, reg2)
x = vars(1:26);
y = vars(27:52);
w = vars(53:end);
f = 0;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
f = f + max(0,(abs((w(r2) - ((x(r1)-a(i))^2 + (y(r1)-b(i))^2)) - (w(r1) * ((x(r2)-a(i))^2 + (y(r2)-b(i))))^2)));
end
  1 comentario
Torsten
Torsten el 2 de Abr. de 2024
Editada: Torsten el 2 de Abr. de 2024
I don't understand your question. But an answer that is surely suitable in your case is: use an if-statement.

Iniciar sesión para comentar.

Respuestas (1)

Manikanta Aditya
Manikanta Aditya el 8 de Abr. de 2024
Hello,
To modify the function so that it outputs values (x), (y), and (w) based on the conditions you've specified, you can incorporate an if statement as suggested by @Torsten.
The conditions you've mentioned seem to be criteria for selecting or processing specific cases rather than directly affecting the output format.
However, it's not entirely clear how you want to use these conditions to modify the output directly since the original function accumulates a scalar value 'f' based on all iterations.
function f = objective(vars, a, b, reg1, reg2)
x = vars(1:26);
y = vars(27:52);
w = vars(53:end);
f = 0;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
condition1 = abs((w(r2) - ((x(r1)-a(i))^2 + (y(r1)-b(i))^2)) > (w(r1) * ((x(r2)-a(i))^2 + (y(r2)-b(i))^2))^2);
condition2 = abs((w(r2) - ((x(r1)-a(i))^2 + (y(r1)-b(i))^2)) < (w(r1) * ((x(r2)-a(i))^2 + (y(r2)-b(i))^2))^2);
if condition1 || condition2
% Modify this part as needed based on how you want to use the conditions
f = f + max(0, (abs((w(r2) - ((x(r1)-a(i))^2 + (y(r1)-b(i))^2)) - (w(r1) * ((x(r2)-a(i))^2 + (y(r2)-b(i))^2))^2)));
else
% Optionally, handle cases where neither condition is met
% For example, you could simply skip these cases or handle them differently
end
end
end
This modification checks each of the conditions you've specified (condition 1 and condition 2) and only performs the calculation and accumulation off 'f' if either of the conditions is true.
Hope this helps, Thank you.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by