How to find min/max points on a surface inside a given area

7 views (last 30 days)
Hello!
I'e been sitting with this for hours and I need to find the highest and lowest point inside the given triangle and I cannot find any way to do this.
I suspect the highest point is in the upper left corner and the lowest point is lower centre and a bit to the left but how do I get the exact nubers?
And how can I plot the surface in only the triangle?
Very thankful for any answers :)
clf,clear,clc
y=linspace(-3, 1);
x=linspace(0,6);
[X,Y]=meshgrid(x,y);
Z=-9.*X.*Y-12.*X+27.*Y;
contourf(x,y,Z, 50);
hold on
% This is just to show the triangle
% Corners on (3,-3)(6,-2)(0,1)
a=[1 -3]; b=[0 3]; plot(b,a, 'k')
c=[3 6]; d=[-3 -2]; plot(c,d, 'k')
e=[6 0]; f=[-2 1]; plot(e,f, 'k')
  1 Comment
Fifteen12
Fifteen12 on 8 Feb 2023
This seems like more of a mathematics question than a MATLAB question, but I'll take a stab at it. You've given the elevation as Z = -9 .* X. * Y - 12 .* X + 27 .* Y. If you know the elevation everywhere, you can solve this using general optimization methods. I'm no expert on optimization, but I know outside a convenient analytical solution, normally a restricted boundary optimization problem is best solved numerically. How you do this is up to you-you can develop your own bisection method to converge to the solution, or you might can use the MATLAB Optimization toolbox. You'll likely want to use the linear programming toolbox: https://www.mathworks.com/help/optim/ug/linprog.html

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 9 Feb 2023
hello
try this
using inpolygon allows you to define the points inside your triangle
the max point is given by the red diamond (top left corner) and the min is the blue diamond
clf,clear,clc
N = 100;
y=linspace(-3, 1, N);
x=linspace(0,6, N);
[X,Y]=meshgrid(x,y);
Z=-9.*X.*Y-12.*X+27.*Y;
figure(1)
contourf(x,y,Z, 50);
hold on
% This is just to show the triangle
% Corners on (3,-3)(6,-2)(0,1)
a=[1 -3]; b=[0 3]; plot(b,a, 'k')
c=[3 6]; d=[-3 -2]; plot(c,d, 'k')
e=[6 0]; f=[-2 1]; plot(e,f, 'k')
% use inpolygon to select points inside the triangle
xv = [0 3 6 0];
yv = [1 -3 -2 1];
in = inpolygon(X,Y,xv,yv);
[r,c] = ind2sub(size(X),find(in));
Xin = X(in);
Yin = Y(in);
Zin = Z(in);
plot(Xin,Yin,'c.') % points inside
[mmax,imax]= max(Zin,[],'all','linear'); % nb linear indice of max value
plot(Xin(imax),Yin(imax),'rd','markersize',15) % points inside
[mmin,imin]= min(Zin,[],'all','linear'); % nb linear indice of max value
plot(Xin(imin),Yin(imin),'bd','markersize',15) % points inside
figure(2) % plot the surface only for the triangle area
ZZ = NaN(N); % create some "empty" plot correponding to the points outside the triangle
ZZ(in) = Zin; % now fill the inside with the correct values
contourf(x,y,ZZ, 50);
hold on
plot(Xin(imax),Yin(imax),'rd','markersize',15) % points inside
plot(Xin(imin),Yin(imin),'bd','markersize',15) % points inside
  2 Comments
Mattias Hertzberg
Mattias Hertzberg on 13 Feb 2023
Hello and thank you very much. This was way more complicated than what I first thought :) Thank you kind sir

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!