2D Plotting in matlab - matlab

I need to draw a boundary on a x-y plane with the followings given:
a) a matrix called 'decision_region' with size(41,41), filled with either 1 or 0. Those 1's and 0's are separated by a boundary which is y =x.
That is for decision_region(i,j), with positions that satisfied i > j, are all 1's.
b) x, and y array with size(1,41).
I know how to use surf(x,y,decision_region) to create a surface plot. But how can I get a y = x plot in a x-y plane?

Your question is not very clear, but from what I understand
hold on
plot(x, y)
should overlay the desired boundary over the surface.

Related

How to calculate the volume under two intersection surface?

I like to calculate the volume under the two intersection plane. The two plane is draw use this code.
P1 = [575,0,400];
P2 = [287.5,0,662];
P3 = [575,3500,154];
normal = cross(P1-P2, P1-P3)
syms x y z
P = [x, y, z]
ep1=dot(normal, P-P1)
% get the equation
Z = solve(ep1,z)
% draw the first plane
ezsurf(Z,[287.5,575,0,3500])
hold on
% draw the second horizontal plane
[x,y]=meshgrid(0:500:3500)
z = ones(8,8)*440
surf(x, y, z)
So I must calculate the volume under the first plane.
I used this code, but I don't know how to construct matrix Zm used the the symbols equation Z. And how can I use meshgrid and surf not ezsurf draw the first plane.
%f=#(x,y)(interp2(Zm,Xq,Yq))
% I want to calculate volume under the plane ranged by Xmin=2.875, Xmax=575,Ymin=0,Ymax=3500
%volume = quad2d(f,(287.5),575,0,3500)
%volume = integral2(f,287.5,575,0,3500)
Thanks a lot.
As an alternative strategy that might be easier to understand I would suggest, instead of interpolating, using some geometry formulas to calculate the area. You can break down the 3D shape into a simple triangular prism and an irregular tetrahedron. There are well defined generalized formulas for both of these.
http://mathcentral.uregina.ca/QQ/database/QQ.09.03/peter2.html

Plotting contour line from three matrices

I have two vectors A(1,512), B(1,8) , and one matrix C(8,512). I am trying to plot contour by using contour(X,Y,Z). I do not know to do it.
A vector represents distance, B vector is frequency, and C matrix is velocity.
This can be done by using function contourf(X,Y,Z)
C1=C';
contourf(B,A,C1);
Transposed of C used because length(B) must equal size(C,2) and length(A) must equal size(C,1)
I quote from the documentation:
contourf(X,Y,Z), contourf(X,Y,Z,n), and contourf(X,Y,Z,v) draw filled contour plots of Z using X and Y to determine the x and y values.

Generate a 3D surface plot by fitting over many 2D plots with varying z value

I would like to achieve a 3D surface plot. My outputs are as follows.
For a particular value of z, I get y for each value of x (x ranges like 0:0.1:1.4).
Then I vary z and, for the same range of x, I get y values.
The result can be visualised as 2D plots at discrete z values, consisting of the range of x and its corresponding y. Here is my original plot:
I would like to create a 3D surface plot instead, like a blanket wrapped over the above 2D plots.
Here's an example for the two types of plots:
figure
hold on
grid on
view(30,40)
x = 0:.01:4;
z = .3:.3:3;
y = NaN(numel(x), numel(z));
for k = 1:numel(z)
y(:,k) = abs((4-x).*sin(x/(1+z(k)))); % compute a vector as function output
% for input vector x, for each z. Store as a column in matrix y
plot3(x,repmat(z(k),size(x)),y(:,k)) % plot in 3D space
end
figure
surf(x,z,y.','edgecolor','none') % surface plot
view(30,40)
grid on

drawing 3d contour plot from 3d vector

I want to draw a contour plot for 3D data.
I have a force in x,y,z directions I want to plot the contour3 for that
the dimensions of the Fx = 21x21X21 same for Fy and Fz
I am finding force = f*vector(x,y,z)
Then
Fx(x,y,z) = force(1)
Fy(x,y,z) = force(2)
Fz(x,y,z) = force(3)
I did the following but it is not working with me ?? why and how can I plot that
FS = sqrt(Fx.^2 + Fy.^2 + Fz.^2);
x = -10:1:10;
[X,Y] = meshgrid(x);
for i=1:length(FS)
for j = 1:length(FS)
for k=1:length(FS)
contour3(X,Y,FS(i,j,k),10)
hold on
end
end
end
This is the error I am getting
Error using contour3 (line 129)
When Z is a vector, X and Y must also be vectors.
Your problem is that FS is not the same shape as X and Y.
Lets illustrate with a simple example:
X=[1 1 1
2 2 2
3 3 3];
Y=[1 2 3
1 2 3
1 2 3];
Z=[ 2 4 5 1 2 5 5 1 2];
Your data is probably something like this. How does Matlab knows which Z entry corresponds to which X,Y position? He doesnt, and thats why he tells you When Z is a vector, X and Y must also be vectors.
You could solve this by doing reshape(FS,size(X,1),size(X,2)) and will probably work in your case, but you need to be careful. In your example, X and Y don't seem programatically related to FS in any way. To have a meaningful contour plot, you need to make sure that FS(ii,jj,k)[ 1 ] corresponds to X(ii,jj), else your contour plot would not make sense.
Generally you'd want to plot the result of FS against the variables your are using to compute it, such as ii, jj or k, however, I dont know how these look like so I will stop my explanation here.
[ 1 ]: DO NOT CALL VARIABLES i and j IN MATLAB!
I'm not sure if this solution is what you want.
Your problem is that contour and contour3 are plots to represent scalar field in 2D objects. Note that ball is 2D object - every single point is defined by angles theta and phi - even it is an object in "space" not in "plane".
For representation of vector fields there is quiver, quiver3, streamslice and streamline functions.
If you want to use contour plot, you have to transform your data from vector field to scalar field. So your data in form F = f(x,y,z) must be transformed to form of H = f(x,y). In that case H is MxN matrix, x and y are Mx1 and Nx1 vectors, respectively. Then contour3(x,y,H) will work resulting in so-called 3D graph.
If you rely on vector field You have to specify 6 vectors/matrices of the same size of corresponding x, y, z coordinates and Fx, Fy, Fz vector values.
In that case quiver3(x,y,z,Fx,Fy,Fz) will work resulting in 6D graph. Use it wisely!
As I comment the Ander's answer, you can use colourspace to get more dimensions, so You can create 5D or, theoretically, 6D, because you have x, y, z coordinates for position and R, G, B coordinates for the values. I'd recommend using static (x,y,R,G,B) for 5D graph and animated (x,y,t,R,G,B) for 6D. Use it wisely!
In the example I show all approaches mentioned above. i chose gravity field and calculate the plane 0.25 units below the centre of gravity.
Assume a force field defined in polar coordinates as F=-r/r^3; F=1/r^2.
Here both x and yare in range of -1;1 and same size N.
F is the MxMx3 matrix where F(ii,jj) is force vector corresponding to x(ii) and y(jj).
Matrix H(ii,jj) is the norm of F(ii,jj) and X, Y and Z are matrices of coordinates.
Last command ensures that F values are in (-1;1) range. The F./2+0.5 moves values of F so they fit into RGB range. The colour meaning will be:
black for (-1,-1,-1),
red for (1,-1,-1),
grey for (0,0,0)
Un-comment the type of plot You want to see. For quiver use resolution of 0.1, for other cases use 0.01.
clear all,close all
% Definition of coordinates
resolution=0.1;
x=-1:resolution:1;
y=x;
z=-.25;
%definition of matrices
F=zeros([max(size(x))*[1 1],3]); % matrix of the force
X=zeros(max(size(x))*[1 1]); % X coordinates for quiver3
Y=X; % Y coordinates for quiver3
Z=X+z; % Z coordinates for quiver3
% Force F in polar coordinates
% F=-1/r^2
% spherical -> cartesian transformation
for ii=1:max(size(x))
for jj=1:max(size(y))
% temporary variables for transformations
xyz=sqrt(x(ii)^2+y(jj)^2+z^2);
xy= sqrt(x(ii)^2+y(jj)^2);
sinarc=sin(acos(z/xyz));
%filling the quiver3 matrices
X(ii,jj)=x(ii);
Y(ii,jj)=y(jj);
F(ii,jj,3)=-z/xyz^2;
if xy~=0 % 0/0 error for x=y=0
F(ii,jj,2)=-y(jj)/xyz/xy*sinarc;
F(ii,jj,1)=-x(ii)/xyz/xy*sinarc;
end
H(ii,jj)=sqrt(F(ii,jj,1)^2+F(ii,jj,2)^2+F(ii,jj,3)^2);
end
end
F=F./max(max(max(F)));
% quiver3(X,Y,Z,F(:,:,1),F(:,:,2),F(:,:,3));
% image(x,y,F./2+0.5),set(gca,'ydir','normal');
% surf(x,y,Z,F./2+.5,'linestyle','none')
% surf(x,y,H,'linestyle','none')
surfc(x,y,H,'linestyle','none')
% contour3(x,y,H,15)

How to create a 2d color map plot from x and y coordinates and a vector of values?

I'm trying to plot a 2 dimensional signal on a specific plane in a 3d model. I have the matrix:
xyzp (nx3)
that contains all the points which are closest to the plane (e.g. when the plane is in the z direction, all the z coordinates are fairly similar).
and I have a vector:
signal (nx1)
that contains a value for each point in xyzp.
when I use:
"surf([xyzp(:,[1,2]),signal)" or "mesh([xyzp(:,[1,2]),signal])"
The plot I get doesn't look at all like the intersection of the plane with the model from any angle (I expected "view(2)" to show the signal in the Z direction), so I assume I didn't use the plot function correctly.
Can anyone show me an example? For instance - A circle on an xy plane with some random signal indicated by color
surf and mesh can be used when the points form a rectangular grid on the xy plane.
In the general case (points are arbitrarily placed), you can use scatter3. For purposes of illustration, consider the following example xyzp and signal:
[x y] = ndgrid(-1:.01:1);
x = x+.3*y; %// example values which do not form a rectangular grid
z = x+y; %// example z as a function of x, y
xyzp = [x(:) y(:) z(:)];
signal = z(:)+x(:)-y(:); %// example values
Then
scatter3(xyzp(:,1), xyzp(:,2), xyzp(:,3), 1, signal, '.');
produces the following figure.
Since scatter3 plots each point separately, the picture is not as smooth as it would be with surf. But this seems hard to improve if the coordinates do not a have any "structure" (as surf requires) .