Creating a surface correctly - matlab

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)

Related

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.
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)

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.

Z must be a matrix, not a scalaqr or vector, matlab

I am trying to make a 3d plot but i am getting an error and im not sure how to solve it. I know that there are other questions out there similar to mine but i tried some of them and it did not work.
fh = sin(x)*cos(y).^3 + 2*cos(x).^5*sin(y)
[X,Y] = meshgrid(1:0.5:10,1:20);
surf(X,Y,fh)
Error using surf (line 82)
Z must be a matrix, not a scalar or vector.
The Z data in this case is what you are passing to surf as fh. It looks like fh is the function you want to use to compute Z, but you need to use the gridded values you generated for X and Y to evaluate it. As your code is now, it is evaluating the function using x and y (case matters!), which you haven't defined for us. Try this instead:
[X, Y] = meshgrid(1:0.5:10, 1:20);
Z = sin(X).*cos(Y).^3 + 2.*cos(X).^5.*sin(Y);
surf(X, Y, Z);
Notice that I used the .* operator (element-wise multiplication) instead of the * operator (matrix multiplication) in the equation.
You could also do this by defining an anonymous function that evaluates the formula for a given set of data:
fh = #(x, y) sin(x).*cos(y).^3 + 2.*cos(x).^5.*sin(y);
[X, Y] = meshgrid(1:0.5:10, 1:20);
surf(X, Y, fh(X, Y));

Retain meshgrid structure when subsetting a meshgrid

When I subset an array constructed from meshgrid, I cannot work out how to keep its meshgrid structure. Thus, you cannot use it in a call to mesh or surface. I will demonstrate this in my example of constructing the unit sphere.
Possible alternate titles for this question:
How do you make the top half of the meshgrid sphere from scratch in Matlab?
How do you use mesh to plot a subset of a meshgrid?
This is motivated using the following toy example of constructing a sphere of unit radius in Matlab from scratch, so that it is like the one generated by:
[x, y, z] = sphere(100)
mesh(x, y, z)
Using the equation for a sphere:
Define a meshgrid and z to be:
x = linspace(-1, 1, 201);
y = linspace(-1, 1, 201);
[x, y] = meshgrid(x, y);
z = sqrt(1 - x.^2 - y.^2);
So far so good, except z takes imaginary values where the sphere does not exist over the xy-plane, that is, anywhere outside of the unit circle.
A call to mesh now returns an error:
>> mesh(x, y, z)
Error using mesh (line 58)
X, Y, Z, and C cannot be complex.
Thus, a logical step is to remove all complex values:
% get logical vector index where real z is
LI = z == real(z)
x = x(LI)
y = y(LI)
z = z(LI)
But now x, y, and z are no longer 3d arrays, and calling mesh gives another error:
>> mesh(x, y, z)
Error using mesh (line 58)
Z must be a matrix, not a scalar or vector.
So, in general I have no idea how to preserve the meshgrid structure when subsetting the data. Hence, I can't generate the top half of this sphere from scratch.
In general, you can "exclude" values from plotting while maintaining the matrix structure by using NaN value. In your case, try this:
LI = z == real(z);
z(~LI) = NaN;
mesh(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.