Add plot to an existing surface
4 ビュー (過去 30 日間)
古いコメントを表示
Giuseppe Giaquinta
2014 年 11 月 21 日
コメント済み: Giuseppe Giaquinta
2025 年 2 月 20 日
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!