Add plot to an existing surface
4 views (last 30 days)
Show older comments
Giuseppe Giaquinta
on 21 Nov 2014
Commented: Giuseppe Giaquinta
on 20 Feb 2025
Hi, i have a surface made with curve fitting tool. Now i'd like to add a 3d line to the same plot to see the intersection point between the line and the surface. Is it possible? thanks
0 Comments
Accepted Answer
Vedant Shah
on 20 Feb 2025
To add a 3D line to a plot that features a surface generated using the Curve Fitting Tool in MATLAB, the “hold” function can be utilized. To find the intersection point between the line and the surface, the “feval” function can be used. For further details on these functions, please refer to the MATLAB documentation using the following commands in the MATLAB command line window:
web(fullfile(docroot, '/matlab/ref/hold.html'))
web(fullfile(docroot, '/matlab/ref/feval.html'))
web(fullfile(docroot, '/optim/ug/fsolve.html'))
Below is a sample code snippet that demonstrates how to achieve the desired functionality:
% Assuming `fittedmodel` is your generated fit function
[fitresult, gof] = fittedmodel(X, Y, Z);
hold on;
% Define the line
t = linspace(-5, 5, 1000);
x_line = t;
y_line = t;
z_line = 7*t + 1; % Example line: z = 2*x + 1
% Plot the line
plot3(x_line, y_line, z_line, 'r', 'LineWidth', 2);
hold off;
% Define the intersection function
intersection_func = @(t) feval(fitresult, t, t) - (7*t + 1);
t_intersect = fsolve(intersection_func, 0); % Initial guess at t = 0
% Calculate intersection coordinates
x_intersect = t_intersect;
y_intersect = t_intersect;
z_intersect = 7*t_intersect + 1;
% Plot the intersection point
hold on;
plot3(x_intersect, y_intersect, z_intersect, 'ko', 'MarkerSize', 15, 'MarkerFaceColor', 'g');
hold off;
disp(['Intersection Point: (', num2str(x_intersect), ', ', num2str(y_intersect), ', ', num2str(z_intersect), ')']);
It is assumed that the plot generated using the Curve Fitting Tool is saved in a MATLAB file as “fittedmodel.m”. The above code plots the line using “plot3” function and then employs the “feval” function to find the intersection point.
The result obtained using a sample dataset is illustrated below:
In the sample output, the green point at the center represents the intersection point.
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!