2D fitting lines in 3D plot - matlab

I wish to put 2D curve fitting to different axis within a 3D plot. I am attaching an image for reference.
on this actual data set:
As you can see the curve fitting for x and y axis is at z=0 value, I need that at say z=10.
Further, when I try to do curve fitting for x and z data set or y and z data set, the fitted curve instead of appearing on the the X-Z plane or Y-Z plane, is appearing on the X-Y plane.
All help is appreciated.

Related

How do I calculate the area of a projection created by the command "view"?

How do I calculate the area of a projection? For example, using the following code, I get a projection on the X-Z plane.
[x,y,z]=peaks;
surf(x,y,z);
xlabel('x');ylabel('y');zlabel('z')
view([0,0])
X-Z Plane Projection
I want to be able to determine the area of the projection of any surf plot that I create. Is there a command or function for this?
Short answer
polyarea(x(1,:),max(z))+polyarea(x(1,:),min(z))
Explanation
The plot you want to calculate its area is,
In area calculation you need only the borders of the projection,
plot(x(1,:),max(z))
hold on
plot(x(1,:),min(z),'r')
and the output is,
The total area is the summation of both areas (upper border to x axis and lower border to x axis),
>> polyarea(x(1,:),max(z))+polyarea(x(1,:),min(z))
>> 28.5947
If you want to get the projection area at an arbitrary view angle, you can use the viewmtx function to project the surface onto the viewing plane, and then use boundary and polyarea to extract the boundary and calculate the area. Something like this:
% draw your surface
[x,y,z]=peaks;
surf(x,y,z);
xlabel('x');ylabel('y');zlabel('z')
axis equal;
%extract the viewing angle, and calculate the resulting transformation matrix
[az,el]=view(gca);
T= viewmtx(az,el);
% transform the surface points by this transformation matrix
pts= [x(:),y(:),z(:),ones(numel(x),1)]';
tpts= T*pts;
tpts=(tpts(1:3,:))';
% now "tpts" has the surface points projected onto the viewing plane
figure, plot( tpts(:,1), tpts(:,2), 'bo'); axis equal;
% calculate the border of this region, and calculate its area.
border = boundary(tpts(:,1), tpts(:,2));
projectedArea = polyarea(tpts(border,1), tpts(border,2));
This approach is based off of the help for viewmtx.

How can I create a slice of a surface plot to create a line? (Matlab)

Given some function z = f(x,y), I'm interested in creating a (1D) line plot along an arbitrary cutting plane in x,y,z. How do I do this in Matlab? Slice, for example, provides a higher dimensional version (colormap of density data) but this is not what I'm looking for.
E.g.:
z = peaks(50);
surf(z);
%->plot z along some defined plane in x,y,z...
This has been asked before, e.g. here, but this is the answer given is for reducing 3D data to 2D data, and there is no obvious answer on googling. Thanks.
If the normal vector of the plane you want to slice your surface will always lay in the xy plane, then you can interpolate the data over your surface along the x,y coordinates that are in the slicing line, for example, let the plane be defined as going from the point (0,15) to the point (50,35)
% Create Data
z=peaks(50);
% Create x,y coordinates of the data
[x,y]=meshgrid(1:50);
% Plot Data and the slicing plane
surf(z);
hold on
patch([0,0,50,50],[15,15,35,35],[10,-10,-10,10],'w','FaceAlpha',0.7);
% Plot an arbitrary origin axis for the slicing plane, this will be relevant later
plot3([0,0],[15,15],[-10,10],'r','linewidth',3);
Since it is a plane, is relatively easy to obtain the x,y coordinates alogn the slicing plane with linspace, I'll get 100 points, and then interpolate those 100 points into the original data.
% Create x and y over the slicing plane
xq=linspace(0,50,100);
yq=linspace(15,35,100);
% Interpolate over the surface
zq=interp2(x,y,z,xq,yq);
Now that we have the values of z, we need against what to plot them against, that's where you need to define an arbitrary origin axis for your splicing plane, I defined mine at (0,15) for convenience sake, then calculate the distance of every x,y pair to this axis, and then we can plot the obtained z against this distance.
dq=sqrt((xq-0).^2 + (yq-15).^2);
plot(dq,zq)
axis([min(dq),max(dq),-10,10]) % to mantain a good perspective

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.

Build 3D surface from one 2D top-down image surface

I am wondering if there is a way to build a random 3D surface from only one (top-down) 2D image of this surface. The fact is that the 3D surface needs the z-coordinates (the heights and the depths) and the 2D (top-down) image gives only the x and y coordinates.
I believe that the main problem is that we can't get the real ranges of the dimensions (x,y,z) of the surface from one 2D (top-down) image but we can get some kind of normalized scaling which is not the real one (it's just similar).
For example:
If we have an image with a surface (2D) and we want 3D of this surface (x,y,z) we can have easily the x and the y coordinates from the image. We can't have the real range of the amplitude (z coordinate) in each point of the surface but only the gray-tones scaling. Is there any ideas on how could we take the real sizes of the amplitudes of a surface from one 2D (top-down) image?
Left is a sample of 2D top-down image and Right is a surface which created by the 2D
http://www.sendspace.com/file/9wzx0u
p.s.
I can't post an image because of my reputation, so I uploaded one on sedspace.com.
Read in the image:
A = imread(filename)
Plot the surface plot using the magnitude of the value read in for each x and y from the file:
surf(A)

Plotting a surface from a set of interior 3D scatter points in MATLAB

I have a large (~60,000) set of triplet data points representing x,y, and z coordinates, which are scattered throughout a Cartesian volume.
I'm looking for a way to use Matlab to visualize the non-convex shape/volume described by the maximum extent of the points.
I can of course visualize the individual points using scatter3, but given the large number of points the details of the shape are obscured by the noise of the dots.
As an analogy, imagine that you filled a hour glass with spheres of random sizes such as BBs, ping pong balls, and kix and then were given the coordinates of the center of each of each object. How would you take those coordinates and visualize the shape of the hour glass containing them?
My example uses different sized objects because the spacing between data points is non-uniform and effectively random; it uses an hourglass because the shape is non-convex.
If your surface enclosing the points can be described as a convex polyhedron (i.e. like the surface of a cube or a dodecahedron, without concave pits or jagged pointy parts), then I would start by creating a 3-D Delaunay triangulation of the points. This will fill the volume around the points with a series of tetrahedral elements with the points as their vertices, and you can then find the set of triangular faces that form the outer shell of the volume using the convexHull method of the DelaunayTri class.
Here's an example that generates 200 random points uniformly distributed within the unit cube, creates a tetrahedral mesh for these points, then finds the 3-D convex hull for the volume:
interiorPoints = rand(200,3); %# Generate 200 3-D points
DT = DelaunayTri(interiorPoints); %# Create the tetrahedral mesh
hullFacets = convexHull(DT); %# Find the facets of the convex hull
%# Plot the scattered points:
subplot(2,2,1);
scatter3(interiorPoints(:,1),interiorPoints(:,2),interiorPoints(:,3),'.');
axis equal;
title('Interior points');
%# Plot the tetrahedral mesh:
subplot(2,2,2);
tetramesh(DT);
axis equal;
title('Tetrahedral mesh');
%# Plot the 3-D convex hull:
subplot(2,2,3);
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','c')
axis equal;
title('Convex hull');
You could treat your data as a sample from a three-dimensional probability density, and estimate that density on a grid, e.g. via a 3d histogram, or better a 3d kernel density estimator. Then apply a threshold and extract the surface using isosurface.
Unfortunately, hist3 included in the Statistics Toolbox is (despite its name) just a 2d histogram, and ksdensity works only with 1d data, so you would have to implement 3d versions yourself.