Plotting a function of two variables and pointwise minimization - matlab

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.

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)

Related

How to make a 2D contour plot with given data point in Octave/MATLAB?

I have a matrix whose three columns correspond to x, y and f values. I want to make a contour plot of f(x,y) in the x,y plane from these data with Octave/MATLAB.
Let's say, the matrix M is
x1 y1 f1
x2 y2 f2
x3 y3 f3
. . .
. . .
I found the function contourf requires f to be a matrix (whereas I have a vector with corresponding points).
How to generate this plot?
The x, y, and z variables that you pass to contourf are all matrices of the same size. For every point you need an x, y, and z value. You can use meshgrid to make matrices that have all the combinations of x and y values.
This example is from the doc for contourf. I added some comments to explain what is happening
% Create a vector of x values
x = linspace(-2*pi,2*pi);
% Create a vector of y values
y = linspace(0,4*pi);
% Make matrices with all combinations of x and y values for plotting
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
contourf(X,Y,Z)
This is the result of the above code

Graphing a multi variable function in MATLAB

I have never used MATLAB before, so I am very lost. For my calculus class, we were tasked with finding a certain function and then using MATLAB to graph it. Finding the function was no problem. However, trying to graph it has me pulling my hair out. The function is z(x,y)= xy(x+y)(2x+y)(3x+y)(x-2y)(x-3y)(x-4y). Any help or advice is GREATLY appreciated.
You can define a anonymous function handle.
% define function
% .* denotes element wise multiplication
f = #(x,y) x.*y.*(x+y).*(2*x+y).*(3*x+y).*(x-2*y).*(x-3*y).*(x-4*y);
% define range and resolution for x and y
x = -20:0.5:20;
y = -20:0.5:20;
% create meshgrid for 3d plotting
[X, Y] = meshgrid(x,y);
% calculate z values (for meshgrid)
z = f(X, Y);
% plot the function
figure()
surf(x,y,z)
To explain further, since you want to calculate the z value for x and y pairs, you should use a element wise multiplication .*.
Then you have to create a meshgrid for the x and y values, to have all the possible x and y pairs in the two new matrices X and Y. Providing these to your function will calculate the corresponding z value for all these pairs. You can use these for plotting, e.g. surf.

How to plot using surf gird in 2D using function value

if the function F is available it is easy to draw surf plot i.e.
x=1:0.1:4;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=sin(X).^2+cos(Y).^2;
surf(X,Y,Z);
view(2) ;
in my case I calculated F function using least square:
for example I have x and y vectors
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2754.5 4043.3 5376.3 5050.4];
the linear function of these two vector is define by
F= [1149.73 , 37.63];
therefore the estimation is equal to
z= [ones(5,1) x']*F';
which is
z = [1149.73 1519.67 4051.96 5589.35 4912.65];
and if it is plotted
plot(x,y,'b.');
hold on;plot(x,y,'b-');
hold on; plot(x,z,'-r');
The linear z ( red line) is showing correctly. Now I want to plot it for all possible combination of x and y using grid and I need to have a mesh for all inputs
[X,Y] = meshgrid(x,y);
but how to make the Z matrix to show the intensity plot of function Z? The Z suppose to have high intensity close to z value and less value far from it. I should suppose to get something like this
Thanks
P.S: the F is calculated using pinv (least square).
You have to interpolate the scattered data to plot it on grid. Here is a simple example for your x, y and z vectors
xi=linspace(min(x),max(x),100)
yi=linspace(min(y),max(y),100)
[XI YI]=meshgrid(xi,yi);
ZI = griddata(x,y,z,XI,YI);
contourf(XI,YI,ZI)

Creating a surface correctly

I want to create and show the surface z=x*exp(-x^2-y^2) in the section x,y~[-10;10]. I am trying to use:
x=-10:10;
y=-10:10;
z=x*exp(-x^2-y^2);
[X,Y,Z]=meshgrid(x,y,z);
surf(X,Y,Z);
and getting:
"Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead."
I understand that x is a vector and so this is not a logical statement. Never the less, I do not have an Idea as to how to create this surface?
You'll want to use meshgrid before computing z so that you compute a value for z for each combination of x and y. Also you'll want to use element-wise operators (.^ and .*) to create z
% Create all permutations of x and y
[x, y] = meshgrid(-10:10,-10:10);
% Compute z for each permutation
z = x .* exp(-x.^2 - y.^2);
% Plot as a surface
surf(x, y, z)

How to make contour plots in matlab for a self-defined function f(x,y) in which x and y cannot take vector values?

We know that the usual way to make a contour plot in Matlab for a function Z(x,y) is
[X,Y] = meshgrid(-2:.2:2,-2:.2:3);
Z = X.*exp(-X.^2-Y.^2); (for example)
contour(X,Y,Z);
However, this way does not work for the following function f(x,y):
Suppose h_{ij}(x,y) is a large (e.g., 100x100) matrix, in which each component is a (self-defined) function of x and y. We define another function
f(x,y)=det(h_{ij}(x,y))
and want to make a contour plot of the function f(x,y).
The determinant in f=det(h) requires each component of the matrix h be a number. So f(x,y) can be calculated by Matlab only if x and y are numbers, not vectors. If we use [X,Y]=meshgrid(...), it means that each component of the matrix h is a vector, and f(X,Y) cannot be calculated.
Is there a way to make a contour plot for the above function f(x,y), in which x and y cannot take vector values?
Assuming that h is pre-defined to be a matrix of functions each of which takes two scalar arguments and outputs a matrix (or any valid input to the det function), and the subscripts i and j refer to the indices in X and Y of the arguments to that function, something like the following code should work (X and Y should be the same size as h):
applyh = #(fn, x, y) fn(x, y);
[I, J] = meshgrid(1:m, 1:n);
Z = arrayfun(#(i, j) det(applyh(h(i, j), X(i), Y(j))), I, J);
I think you're misunderstanding what meshgrid does - the output of meshgrid can be easily fed to a function as above. They are not vectors in each element (just a 2-D matrix). You can then plot Z as usual.