Z must be a matrix, not a scalaqr or vector, matlab - 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));

Related

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)

Error in mesh(X,Y,Z) in matlab

This is my code:
load mwe16_2.dat;
x = mwe16_2(:,1); % x contains the 1st column(500 values) of ‘mwe16_2’
y = mwe16_2(:,2); % y contains the 2nd column (500 values)of ‘mwe16_2’
z = mwe16_2(:,3); % z contains the 3rd column(500 values) of ‘mwe16_2’
[X, Y, Z] = meshgrid (x, y, z);
mesh (X, Y, Z)
While running the code it is showing the error:
*Error in plot3_d (line 13) mesh(X,Y,Z) in Matlab*
Can someone say the reason for the error and how to correct it?
[X,Y,Z] = meshgrid(x,y,z) produces three 3D arrays from x, y and z vectors. Now if you take a look at mesh you will see that you are not providing this function with proper input.
If you want to illustrate the set of points defined by three vectors of x, y and z, you can consider using scatter3:
figure; scatter3(x, y, z)

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

Matlab using mesh to plot (Error using mesh (line 76) X, Y, Z, and C cannot be complex)

I'm trying to plot this function but keep getting 'Error using mesh (line 76)
X, Y, Z, and C cannot be complex'. I found another question that suggested the use of abs on the sqrt function but this does not give the desired output. The output given should look like this
function [ X,Y,Z] = plotComplexFunction( )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x = linspace(-1,1);
y = linspace(-2*pi,2*pi);
[X,Y] = meshgrid(x,y);
i = sqrt(-1);
Z = exp(X+(i*Y));
mesh(X,Y,Z)
end
It looks like the plot was made using only the real part of Z.
Changing the line where you call mesh to:
mesh(X,Y,real(Z));
produces this plot:

Plotting a 3D graph of `sqrt(1+1/(kr)^2)`

I am trying to plot the following equation in MATLAB:
ratio = sqrt(1+1/(kr)^2)
With k and r on the x and y axes, and ratio on the z axis. I used meshgrid to create a matrix with values for x and y varying from 1 to 10:
[x,y] = meshgrid([1:1:10],[1:1:10]);
The problem now is to create values for z. I've tried to just type the whole equation in, but that gives this result:
>> Z = sqrt(1+1/(x .* y)^2)???
Error using ==> mldivide
Matrix dimensions must agree.
So what I did is go to through the whole process manually, which produces the right graph in the end:
z = z^2;
z = 1 ./ z;
z = 1 + z;
z = sqrt(z);
mesh(x,y,z)
Is there a more elegant way to do this? Or a way to type in the equation and let MATLAB handle the rest?
Try this:
Z = sqrt(1+1./(x .* y).^2);
surf(Z);
The problem that you had is related to using / instead of ./ and ^2 instead of .^2