Modifying function to get only one value

3 Ansichten (letzte 30 Tage)
Akhil am 2 Apr. 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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Torsten am 2 Apr. 2024
Bearbeitet: Torsten am 2 Apr. 2024
I don't understand your question. But an answer that is surely suitable in your case is: use an if-statement.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Manikanta Aditya am 8 Apr. 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.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Solver Outputs and Iterative Display finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by