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:

 採用された回答

Matt J
Matt J 2022 年 4 月 20 日
編集済み: Matt J 2022 年 4 月 20 日
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 件のコメント

Wiqas Ahmad
Wiqas Ahmad 2022 年 4 月 20 日
編集済み: Wiqas Ahmad 2022 年 4 月 20 日
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 2022 年 4 月 20 日
編集済み: Matt J 2022 年 4 月 20 日
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?
Torsten
Torsten 2022 年 4 月 20 日
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 ?
Wiqas Ahmad
Wiqas Ahmad 2022 年 4 月 20 日
@Matt J Thank you for your explanation. It makes my idea more vivid. BTW the val is actually the tf values.
Matt J
Matt J 2022 年 4 月 20 日
@Wiqas Ahmad I'm glad, but if your question is now resolved, please Accept-click the answer.
Wiqas Ahmad
Wiqas Ahmad 2022 年 4 月 20 日
@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.
Matt J
Matt J 2022 年 4 月 20 日
編集済み: Matt J 2022 年 4 月 20 日
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]);
Wiqas Ahmad
Wiqas Ahmad 2022 年 4 月 20 日
@Matt JThank you so much, Sir

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeContour Plots についてさらに検索

タグ

質問済み:

2022 年 4 月 20 日

コメント済み:

2022 年 4 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by