How to locate the x and y co-ordinate of minimum value from a contour plot automatically ?

38 views (last 30 days)
I am trying to plot a contour and find it's x and y co-ordinate of the minimum value from the contour plot. Here is a sample code.
clear variables
format long
close all
clc
a = 10;
b = 20;
c_dom = 1:10;
d_dom = 11:20;
L_c_dom = length(c_dom);
L_d_dom = length(d_dom);
e = zeros(L_c_dom, L_d_dom);
for j_c = 1:L_c_dom
for j_d = 1:L_d_dom
c = c_dom(j_c);
d = d_dom(j_d);
e(j_c, j_d) = a*b*c*d;
end
end
[x, y] = meshgrid(c_dom, d_dom);
[C, h] = contour(x, y, e);
Above figure is the contour plot. Now, I need to locate the position (x and y co-ordinates) of it's minimum value automatically. Since this is a sample code so I know that the first value of variable 'c_dom' and 'd_dom' are the x and y co-odrinates. But in reality my actual code is a bit complicated and the contours are bunch of concentric circles. I checked online but I am just getting the use of min() command to get the minimum value but I didn't find any procedure to locate it's position (x and y co-ordinates). I did another technique where I found out the minimum value of the contour and plotted the value and highlighted it. After clicking at that point I can get x and y co-ordinates from the plot itself. But unfortunately it's a very tedius job and if I had to do a parametric study where my variable 'a' and 'b' are also a vector, then it is a very lengthy process. So, I need to find a way to locate x and y co-ordinates automatically. Any help will be greatly appreciated.

Accepted Answer

Turlough Hughes
Turlough Hughes on 22 Aug 2021
Edited: Turlough Hughes on 22 Aug 2021
Try:
[min_e,index] = min(e,[],'all','linear');
x_min = x(index);
y_min = y(index);
hold on, plot(x_min,y_min,'ok','MarkerFaceColor','k');
This gives the location of x and y (the index) corresponding to the minimum in e.
  3 Comments
gaurav pandey
gaurav pandey on 13 Sep 2023
Hello,
Thanks for your input and it works for me.
However, my contour has three minima. Could you please help me to locate these three lowest minima and their corresponding location of x and y.
Thank you in advance

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!