Interpolate points with reference to a surface
Mostrar comentarios más antiguos
Hello everyone,
I have one 3D surface and two points outside the surface. If I plot these two points and the surface together using plot3 command, then the line passes through the surface.
But I want the line to lie or fit on the surface. Could anyone suggest me how to do this? Whether I have to do interpolation, if yes then how? Picture is attached.
Thank you!

Respuestas (1)
Gautam
el 26 de Feb. de 2024
Hello Sachal,
I understand that you have a surface plot with two points, and you want to connect the two points with a line that fits the surface.
Assuming that you have the equation that describes the surface, a workflow to achieve this would be as follows:
- Generate a plane that passes through the points
- Find the points of intersection of the plane and the surface
- Plot the points of intersection
I am using an ellipsoid as an example for the surface. The figure below shows the ellipsoid with the points.

Using the function “plot3” connects the points with a straight line as shows below

Following the steps mentioned, we generate a plane that passes through the points

The next step is to find the points of intersection of the plane and the surface.
We do this by finding all the points of on the plane that satisfy the equation of the ellipsoid.
Finally, we plot the points of intersection of the ellipsoid and the plane.

The figure below shows the plot without the plane

Below is the MATLAB code for the entire process
[X,Y,Z] = ellipsoid(10,10,10,20,10,10,50); %Get the X, Y and Z grid for the ellipsoid
E = surf(X,Y,Z); %Plot the Ellipsoid
E.EdgeAlpha = 0.2;
E.FaceAlpha = 0.7
axis equal
hold on
%Defining the co-ordinates of the points
x = [5 10];
y = [0 10];
z = [10 20];
%Plotting the points and the straight line joining the points
plot3(x,y,z,"r-*", "LineWidth", 2, "MarkerSize",10)
% Generating the grid for the plane passing through the points
xPlane = meshgrid(linspace(x(1), x(end), 500));
yPlane = meshgrid(linspace(y(1), y(end), 500));
zPlane = meshgrid(linspace(z(1), z(end), 500))';
surface = surf(xPlane,yPlane,zPlane);
surface.FaceColor = [0 0.5 0.3];
surface.EdgeAlpha = 0;
% Calculate and plot the intersection curve
% Substitute the points of plane into the ellipsoid equation and find where it is satisfied
inside = ((xPlane-10)/20).^2 + ((yPlane-10)/10).^2 + ((zPlane-10)/10).^2 - 1;
threshold = 0.01;
% Only keep the points where the plane intersects the ellipsoid
isect_x = xPlane(abs(inside) < threshold);
isect_y = yPlane(abs(inside) < threshold);
isect_z = zPlane(abs(inside) < threshold);
% Plot the intersection points
plot3(isect_x, isect_y, isect_z, 'm-', "LineWidth",2);
hold off;
You can refer to the link below for more information on generating grid for plotting the plane
- meshgrid: https://www.mathworks.com/help/releases/R2021a/matlab/ref/meshgrid.html?&s_tid=doc_srchtitle
Thank You,
Gautam Murthy
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!