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)
Related
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
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));
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)
I have the following code below, but I cannot test it since I do not have Matlab with me right now and I am afraid I might not have the time to test it by myself when I finally get it. I'm trying to plot both 3-d graphs and graphs of the level curves in the y and x axis (two dimensions only) of three different types of functions. I would appreciate if someone could point if there is something wrong with the code below.
**************************************************************
**plotting functions -- level curves and 3d graph**
x_val = linspace(0, 100, 200);
y_val = linspace(0, 100, 200);
[x, y] = meshgrid(x_val, y_val);
z = ln(x).+y.;
figure
contour3(y, x, z)
contour(y, x, z)
********************************
z = (x.^1/2)+y.;
figure
contour3(y, x, z)
contour(y, x, z)
*********************************
z = (x.^1/3)+y.;
figure
contour3(y, x, z)
contour(y, x, z)
ln is not a valid matlab symbol, in addition to the excess of dots mentioned and comment formatting above. The following runs on Matlab.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%plotting functions -- level curves and 3d graph%%
x_val = linspace(0, 100, 200);
y_val = linspace(0, 100, 200);
[x, y] = meshgrid(x_val, y_val);
z = log(x)+y;
figure
contour3(y, x, z)
figure
contour(y, x, z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = (x.^1/2)+y;
figure
contour3(y, x, z)
figure
contour(y, x, z)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
z = (x^1/3)+y;
figure
contour3(y, x, z)
figure
contour(y, x, z)
For starters, comments in MATLAB are "%" not "*".
You have a few mistakes, trying to do element-wise operators, I think.
Your three assignments of z have too many dots:
z = log(x)+y;
z = (x.^1/2)+y;
z = (x.^1/3)+y;
It is not necessary to use ".+", because MATLAB automatically adds matrices elementwise.
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);