0

I have this function below and I need to calculate the minimum and maximum of this function U, and also plotting the maximum and minimum value in 3D graph together with the function.

How can I write the code?


[x,y]=meshgrid(0:0.1:pi,0:0.1:2*pi);% x-theta,y-phi

a=90.7;b=36.2;c=12.9; 
E=1.44;

U=-E.^2*(a.*sin(x).^2.*cos(y).^2+b.*sin(x).^2.*sin(y).^2+c.*cos(x).^2);

meshc(x,y,U)

xlabel('\theta')
ylabel('\Phi ')

zlabel('U')

I tired this way to find max but I don't know if i did it correct

max(U,[],1) %max row-wise
max(U,[],2) %max column-wise

and for the minimum it didn't work the same idea, also I didn't get the exact value of the maximum

Yammus
  • 3
  • 2

2 Answers2

1

As stated above, to simply find the maximum/minimum of your sampled function, use m = min(U(:)); M = max(U(:)). To be able to plot them, what you are missing are the coordinates that give you those values. For this, you will need the second output of the min/max functions, which gives you the index where the extreme happens.

A possible implementation (possibly not the best one) would be (might not work perfectly, I don't have matlab at hand):

[Ms,I] = max(U,[],1); %row-wise maximum and their indexes
[M,j] = max(Ms); %maximum among all rows and its index

Now i = I(j) is the location of the maximum. You can finally do plot3(x(i,j),y(i,j),U(i,j),'ro') to plot a big red circle in the maximums location, or whatever you like.

Note: I might have it backwards and it might be x(j,i), and so on. Just check. Of course you can do the same thing for min().

EDIT: I just remembered the ind2sub function , which solves all your problems. Following the syntax used above:

[M,ind] = max(U(:));
[i,j] = ind2sub(size(U),ind)

The rest holds the unchanged.

Puff
  • 503
  • 1
  • 4
  • 14
0

You can simply use something like

max(max(U))

this will find the maximum for your 2D matrix. For the minimum you just have to replace max with min.

AkariYukari
  • 338
  • 4
  • 16