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

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:

Related

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

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)

Plot square root in a contour

I have two variables x and y. I decide to plot the square root of their difference using contour as follows:
x=0:0.1:100;
y=0:0.1:100;
G=sqrt(x-y);
test2 = G;
test2(~(G<0)) = nan;
[C,h]=contourf(x,y,G,'ShowText','off');
set(gca,'FontSize',20)
However I get this error : Error using contourf (line 69)
Z must be size 2x2 or greater.
If that is resolved, I want to reach my goal and plot the actual function which relies on x, y and G itself as follows:
Function = 2 sqrt(x) / G * acoth((sqrt(x) + y/2 )/G )
I give an example
x=0:0.1:100;
y=0:0.1:100;
[X, Y]=meshgrid(x,y);
G=sqrt(X-Y);
test2 = G;
test2(~(G<0)) = nan;
[C,h]=contourf(X,Y,abs(G),'ShowText','off');
set(gca,'FontSize',20)
Input for contour should be 2D array, but your arrays are 1D.
Here, G is complex number. When you plot G, you should plot absolute G.
The result will be like below.
Regarding your function,
H=((2*sqrt(X))./G).*acoth((sqrt(X) + Y/2 )./G );

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

ezplot in MATLAB, how to plot using a function handle?

I've tried this:
linefunca = #(xa,ya) aa*xa + ba*ya + ca;
figure(1)
imshow(Pica);
hold on;
ezplot(linefunca,[1,1072,1,712]);
But I'm returned with this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ezplotfeval/applyfun (line 80)
z(i) = feval(f,x(i),y(i));
Error in ezplotfeval (line 65)
z = applyfun(x,y);
Error in ezplot>ezimplicit (line 257)
u = ezplotfeval(f, X, Y);
Error in ezplot (line 153)
hp = ezimplicit(cax, f{1}, vars, labels, args{:});
Error in ps3 (line 313)
ezplot(linefunca,[1,1072,1,712]);
aa,ba,ca are all known values (column vectors). The x and y limits are the size of the image that I'm working with. I'm trying to plot a set of epipolar lines. Any suggestions?
EDIT:
lt = length(aa);
linefunca = #(x,y,t) aa.*x(t) + ba.*y(t) + ca(t);
figure(1)
imshow(Pica);
hold on;
for t=1:lt
ezplot(#(x,y,t) linefunca(x,y,t),[1,lt]);
end
As far as I know, ezplot can not plot a series of lines like plot. A way to work around this would be to add a parameter k to the anonymous function, which is used to select the current line. You can then go through all lines in a for loop and plot them one-by-one.
Further: as it is stated on the ezplot help page, you have to use the array functions .*, ./ and .^ , so ezplot can use vectors to evaluate the function.
N = 5;
aa = rand(N,1); ba = rand(N,1); ca = rand(N,1);
linefunca = #(xa,ya,k) aa(k).*xa + ba(k).*ya + ca(k);
hold on
for k=1:N
ezplot(#(x,y)linefunca(x,y,k),[-5,5,-5,5]);
end
hold off