Is there a way to mark tangent lines on a graph plotted using the curve fitting app?

4 views (last 30 days)
Hi,
I've used the curve fitting app to plot a 2D graph using a set of data points. Is there a way to mark tangent lines on specific points on this curve fit?
Thanks

Answers (1)

Shivang
Shivang on 6 Mar 2024
Hi Delika,
I understand that you've using the Curve Fitter app to fit a curve to a set of data. And you would like to mark the tangent lines at particular points on this curve.
This can be done manually by using the equation of the fit created by the Curve Fitter app. The steps to be follwed are listed below:
  • Export the fit from the Curve Fitter app to the Workspace, using the "Export" option in the app's toolstrip. Assume the fit object is named "fitResult".
  • Once we have the equation of the fit, we need to find the derivative. I will assume that the fit is polynomial, for which the derivative is already well known. You can use symbolic computation to find the equation of the derivative in general. Refer to the documentation of the "diff" function in MATLAB: https://www.mathworks.com/help/releases/R2020a/symbolic/diff.html
% Assuming a quadratic fit: ax^2 + bx + c
coeffs = coeffvalues(fitResult);
a = coeffs(1);
b = coeffs(2);
% For a quadratic fit, the derivative is: 2ax + b
  • Now we can calculate the slope of these tangent lines, and plot these lines at the specified points.
x_points = [5, 10]; % Points at which you want the tangent lines
x_range = linspace(min(x_data), max(x_data), 1000); % Assuming x_data is your data
figure;
hold on;
plot(x_data, y_data, 'ro'); % Plot original data points
plot(x_range, fitResult(x_range), 'b-'); % Plot the fit
for i = 1:length(x_points)
x_tan = x_points(i);
y_tan = fitResult(x_tan); % Evaluate the fit at the tangent point
% Calculate the slope of the tangent line
slope = 2*a*x_tan + b; % Adjust this based on the actual derivative
% Define the tangent line equation: y = mx + c
% Calculate c using the point (x_tan, y_tan)
c = y_tan - slope*x_tan;
% Plot the tangent line
x_tangent = linspace(x_tan-1, x_tan+1, 100); % Adjust the range as needed
y_tangent = slope*x_tangent + c;
plot(x_tangent, y_tangent, 'k--', 'LineWidth', 2); % Plot each tangent line
end
hold off;
Hope this helps!
-Shivang

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!