How to find minimum of a 3D function in a double for loop plotted on a slice?

I have 24 subplots of a 3D function (costf) plotted on a slice using two forloops,h and fov, as:
The program is:
figure
count = 1;
for h = 1000:1000:4000
for fov = [0.2, 0.5, 1, 2, 5, 10]
H = genHeight(h);
a = 0.01:0.01:0.05;
r = 4:20;
[X, Y, Z] = meshgrid(r, a, H);
xslice = r;
yslice = a;
zslice = H;
subplot(6, 4, count)
slice(X, Y, Z,costf, xslice, yslice, zslice)
shading interp
hCB=colorbar
hCB.Label.String='Cost function';
xlabel('CER_{ref}(\mum)')
ylabel('\alpha_{ref}(1/m)')
xlim([4, 20])
zlabel('Height(m)')
view([-221.831289102975 32.404205593246]);
count = count + 1;
end
end
I want to find the minimum value of the function "costf" in each loop, alternatively in each subplot and spot the minimum value in each subplot with asterisk. My cost function is a 3d function with dimensions, as:

 Akzeptierte Antwort

Matt J
Matt J am 20 Apr. 2022
Bearbeitet: Matt J am 20 Apr. 2022
figure
count = 1;
for h = 1000:1000:4000
for fov = [0.2, 0.5, 1, 2, 5, 10]
....
tf=costf==min(costf(:));
hold on
scatter3(X(tf),Y(tf),Z(tf),'*')
hold off
end
end

8 Kommentare

Wiqas Ahmad
Wiqas Ahmad am 20 Apr. 2022
Bearbeitet: Wiqas Ahmad am 20 Apr. 2022
The code works but I think some problem exists if I'm not wrong. All the elements in the "tf" are zero except one at the end. I don't know why It doesn't take the values or the minimum is zero at all levels?
Tf is a 3D logical array.
Matt J
Matt J am 20 Apr. 2022
Bearbeitet: Matt J am 20 Apr. 2022
Your code doesn't show how "val" is created. Is it the same as tf?
Assuming so, the locations where tf=1 are the locations where costf is minimum. In this case, it appears that costf has a unique global minimum. Why is that surprising?
Isn't it necessary to take the minimum of "costf" on the respective slice instead of the global minimum over the complete x/y/z field ?
@Matt J Thank you for your explanation. It makes my idea more vivid. BTW the val is actually the tf values.
@Wiqas Ahmad I'm glad, but if your question is now resolved, please Accept-click the answer.
@Torsten My objective is to find the global minimum over the complete volume. However, your question is of great interest to me. It can give a new look to my work. I will highly appreciate your efforts if you proceed witth a code of finding a local minimum of a cost function (costf) on each slice considering both of the loops.
if you proceed witth a code of finding a local minimum of a cost function (costf) on each slice
A straight-forward modification:
tf=costf==min(costf,[],[1,2]);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by