Add plot to an existing surface
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Vedant Shah
2025년 2월 20일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!