Search code examples
matlabplotmatlab-figure

Plotting a function of two variables and pointwise minimization


How can I get a 3d-plot the following function in MATLAB?

f(x,y)=log(1+ (min(x,y))^2/(4*y));

I want to create a 3d-plot of f as a function of x and y. x and y are non-negative so they can range from 0 to any positive number like 10.

I tried to plot this with surf and meshgrid but it didn't work since I have a pointwise minimization.


Solution

  • first, create x and y with meshgrid :

    [x,y] = meshgrid(0:0.5:10,0:0.5:10);
    

    then compute function and plot :

    k(:,:,1)=x;
    k(:,:,2)=y;
    % because x and y are 2d matrices in meshgrid, I defined 3d matrix k to compute minimum in third dimension:
    z=log((1+ (min(k,[],3)).^2)./(4*y));
    surf(x,y,z)