Get curve length and fitting curve formula from curve fitting

14 Ansichten (letzte 30 Tage)
tengteng QQ
tengteng QQ am 4 Nov. 2021
Bearbeitet: Rupesh am 29 Feb. 2024 um 10:55
Hello everyone, I get a (1 x 1Cfit) file after using f = fit(m,n,'smoothingspline','SmoothingParam',0.000000001);
May I ask how to get curve length and fitted curve formula from curve fitting? Thank You!

Antworten (1)

Rupesh
Rupesh am 22 Feb. 2024
Bearbeitet: Rupesh am 29 Feb. 2024 um 10:55
Hi tengteng QQ,
I understand that you're looking to both retrieve the formula of your fitted smoothing spline curve and calculate its total length after using MATLAB's fit function. Consider below code as a reference to get an idea about both the tasks.
Step 1 : Getting the Fitted Curve Formula
f = fit(m, n, 'smoothingspline', 'SmoothingParam', 0.000000001);
disp(f)
This command will show you the type of spline used and its coefficient structure.
Step 2: Calculating the Curve Length
For calculating the length of the curve, you'll need to perform numerical integration on its derivative. The MATLAB code below outlines this process:
% Assuming 'm' is the independent variable and 'f' is your fitted curve object
% First, compute the derivative of the fitted curve
df_dm = differentiate(f, m);
% Next, set up a fine grid of points for more accurate integration
m_fine = linspace(min(m), max(m), 1000);
df_dm_fine = differentiate(f, m_fine);
% Calculate the small elements of arc length
differential_arc_length = sqrt(1 + df_dm_fine.^2);
% Integrate these elements to get the total curve length
curve_length = trapz(m_fine, differential_arc_length);
% Finally, display the calculated length
disp(['Curve Length: ', num2str(curve_length)]);
By using “m_fine”, you create a dense set of points for a precise calculation, and “trapz” performs the numerical integration to give you the curve's approximate total length. You can also adjust the number of points in “m_fine” if you need to refine the accuracy of your result.
You can consider below sample code for better understanding of algorithm, and I have also attached corresponding output for the script at the end.
% Define the sequences for m and n
m = [200, 400, 600, 800];
n = [10, 20, 30, 40]; % Replace with your actual n values
% Fit a smoothing spline to the data
f = fit(m', n', 'smoothingspline', 'SmoothingParam', 0.000000001);
% Display the type of spline used (no explicit formula will be shown)
disp(f)
% Calculate the derivative of the fitted curve
df_dm = differentiate(f, m);
% Generate a fine grid of points for accurate integration
m_fine = linspace(min(m), max(m), 1000);
n_fine = feval(f, m_fine);
df_dm_fine = differentiate(f, m_fine);
% Compute the differential arc length elements
differential_arc_length = sqrt(1 + df_dm_fine.^2);
% Numerically integrate to find the total curve length
curve_length = trapz(m_fine, differential_arc_length);
% Display the total curve length
disp(['Curve Length: ', num2str(curve_length)]);
% Plot the original data points
figure;
plot(m, n, 'o', 'DisplayName', 'Original Data');
hold on;
% Plot the fitted curve
plot(m_fine, n_fine, '-', 'DisplayName', 'Fitted Curve');
% Annotate the curve length on the plot
text(m(end), n_fine(end), sprintf('Curve Length: %.2f', curve_length), ...
'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
% Add labels, legend, and grid
xlabel('m');
ylabel('n');
title('Smoothing Spline Fit and Curve Length');
legend('show');
grid on;
hold off;
Expected Outputs :
Smoothing spline:
f(x) = piecewise polynomial computed from p
Coefficients:
p = coefficient structure
Curve Length: 600.7495
I have also attached my output graph in the solution for better explanation about Smoothing Spline Fit and Curve length
You can refer to following documents for more information regarding Curve length and Curve fitting
Hope this helps!

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by