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:
[fitresult, gof] = fittedmodel(X, Y, Z);
t = linspace(-5, 5, 1000);
plot3(x_line, y_line, z_line, 'r', 'LineWidth', 2);
intersection_func = @(t) feval(fitresult, t, t) - (7*t + 1);
t_intersect = fsolve(intersection_func, 0);
x_intersect = t_intersect;
y_intersect = t_intersect;
z_intersect = 7*t_intersect + 1;
plot3(x_intersect, y_intersect, z_intersect, 'ko', 'MarkerSize', 15, 'MarkerFaceColor', 'g');
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.