3d plot with given 2d data - matlab

I want to understand how the 2d data is related to z axis to get the 3d plots
let us say that i have x=[-1:0.1:1], vector
and y=[1 2 3 4 5 4 3 2 1 0]
a plot of y Vs x will have peak of 5 and slope down to both sides at x=0.5
how to relate these data in 3d to get the bell shape surface, with similar characteristics.

You can view a line/curve plot as a function of a single variable, y=f(x), and typically, x and y are both vectors. For e.g., you can plot the Gaussian bell curve as
x=linspace(-3,3,1000);
y=exp(-x.^2/2);
plot(x,y)
A surface plot, on the other hand, is a function of two variables, z=f(x,y) where x and y can be either vectors or matrices and z is a matrix. meshgrid is a very handy function that generates 2D x and y arrays from 1D vectors by proper replication.
It is the z matrix that you plot either as a 2D image (values of z are represented by colors) or a 3D plot (values of z are represented as heights along the z-axis). For e.g., a 3D Gaussian bell curve can be plotted as
x=linspace(-3,3,1000);y=x'; %'
[X,Y]=meshgrid(x,y);
z=exp(-(X.^2+Y.^2)/2);
surf(x,y,z);shading interp
This is how the respective plots should look like

Related

Plotting one point in 3D matrix for all slices in 2D plane

I have 3D matrix (100*50*10) and I want to plot one specific point in all slices. Let us say point (10*6*:). The plot should be in 2D plane
Example (I have this coordinate for point that I want to plot)
x (10*6*1)
x (10*6*2)
x (10*6*3)
x (10*6*4)
x (10*6*5)
x (10*6*6)
x (10*6*7)
x (10*6*8)
x (10*6*9)
x (10*6*10)
I tried plot (x(10,6,:)) but I got error
plot(squeeze(x(10,6,:)))
see: https://www.mathworks.com/help/matlab/ref/squeeze.html
x(10,6,:) is still a 3D matrix, and needs to be reduced to a 1D form before plotting it. This is where the squeeze function comes in.

How can i create a 3D surface Plot in MATLAB?

I need to plot 3 -D plots in MATLAB. Here is one example;
x=0:1:10
for y=1:1:100
Z=1-(1-qfunc(sqrt(2*10.^(x/10)))).^y
end
I need to plot x y and Z in three axes, where Z is function of x and y.
How can i create a surface plot of Z along the x and y axis in MATLAB?
You can use surf and/or mesh functions as follows:
[X,Y] = meshgrid(0:10,1:100);
Z=1-(1-qfunc(sqrt(2*10.^(X/10)))).^Y;
mesh(X,Y,Z)
where creates a mesh, and:
surf(X,Y,Z)
creates a surface. For more options check this page.
remember that the data should be converted to a meshgrid and then you will be able to use those functions.

Contour Plot in matlab

I have made a contour plot in matlab using the inbuilt contour function. It plots a group of lines in a figure, each of which represents a contour. I would like to obtain the data points that comprise each of these contours. How can I do this?
So given a contour plot how would I get the actual underlying data points that make up the equation for each contour line. For example, if the contours ended up being straight lines and one of the contour lines went through the origin, I would like to be able to obtain data points that describe this line. e.g. [0 0.1 0.2 0.3 0.4 ; 0 0.25 0.5 0.75 1].
Thanks.
[C,h] = contour(...) returns a contour matrix, C, that contains the x, y coordinates and contour levels for contour lines derived by the low-level contourc function, and a handle, h, to a contourgroup object. The clabel function uses contour matrix C to label the contour lines. ContourMatrix is also a read-only contourgroup property that you can obtain from the returned handle.
If X or Y is irregularly spaced, contour calculates contours using a regularly spaced contour grid, and then transforms the data to X or Y.
By the way, this text was taken from Matlab documentation...

Matlab 3D plot on Cylindrical Axes

I've run simulations which have given me data points corresponding to X number of different radii, and Y number of angles each one was evaluated at. This means that I have X times Y data points which I need to plot.
I am currently plotting it in an non-ideal fashion: I am using the x and y axes as the r and theta axes. This means that my data appears as a sinusoidal trend which increases with radius on a Cartesian grid, not the circle which it physically represents. This is how I am currently plotting my data:
surf(r_val, th_val, v_val);
What I wish to do is plot my data on a cylindrical axis, such like that of the function polar(), but in R3 space. I would rather not download a toolbox, or modify the existing polar function; if there is no other solution then I will obviously end up doing this anyways.
Thanks for your help!
G.
Also, I am using Matlab 2012a
EDIT:
r_val = 1x8 vector containing unique radii
th_val = 1x16 vector containing unique angles
v_val = 8x16 matrix containing voltages corresponding to each position
NOTE: (after answered)
The truly ideal solution does not exist to this problem, as Matlab currently supports no true polar axes methods. Resource found here.
You should transform your coordinates to Cartesian coordinates before plotting them. MATLAB has builtin functions for perfroming coordiante transformations. See, for example pol2cart, which transforms polar or cylindrical coordinates to Cartesian coordinates. In your case you would simply use something like:
[x, y] = pol2cart(th_val, r_val);
surf(x, y, v_val);
Edit: Given that th_val and r_val are vectors of differing lengths it is necessary to first create a grid of points before calling pol2cart, along the lines of:
[R, T] = meshgrid(r_val, th_val);
[x, y] = pol2cart(T, R);
surf(x, y, v_val);

Plotting 3xN matrix(N number of 3D points) on same graph using matlab

I've a 3xN matrix W where N is 50
W(1,1) is x coordinate of a point
W(2,1) is y coordinate of same point
W(3,1) is z coordinate of same point
Similarly:
W(1,2) is x coordinate of another point
W(2,2) is y coordinate of same point
W(3,2) is z coordinate of same point
....
Now I want to 3d plot all these 3d points on same figure using matlab. How can I plot all these
points on same figure?
Is it possible to plot this matrix using a single function call(in matlab)?
I know that plot3 can be used but it can be used for one graph at a time.
So plot3(v(1,1),v(1,2),v(1,3)); is just a single point. But how do I plot all N points?
Is there an easier and better method?
I guess you can use plot3(w(1,:),w(2,:),w(3,:)).