Finding length of curve created using 'fit' function

5 次查看(过去 30 天)
So I've created a polynomial curve using the 'fit' function based on a range of coordinates. Is there any way of finding the length of the curve that has been created?
line1 = fit(middle_points(:,1),middle_points(:,2),'poly3');
plot(line1);
Thanks for the help in advance.

回答(2 个)

Star Strider
Star Strider 2014-5-23
编辑:Star Strider 2014-5-24
Using the techniques in Arc Length (and every calculus book I’ve seen), I would use the x and y line coordinates from the line you have already calculated, create in integrand using hypot and then trapz to do the integration:
p = [3 -5 -7 -11]; % Polynomial
x = linspace(0,5); % Independent variable
y = polyval(p,x); % Calculate y-values
CLF = hypot(diff(x), diff(y)); % Calculate integrand from x,y derivatives
CL = trapz(CLF) % Integrate to calculate arc length
Using cumtrapz is also a possibility if you are interested in the intermediate values of the arc length.

John D'Errico
John D'Errico 2014-5-23
I'll make some fake data.
x = linspace(-pi/2,pi/2,20);
y = sin(x);
% fit a quintic (degree 5) polynomial
P5 = polyfit(x,y,3);
% differentiate the polynomial
P5der = polyder(P5);
fun = @(x) sqrt(1 + polyval(P5der,x).^2);
integral(fun,-pi/2,pi/2)
ans =
3.8224
Note that I've also got a function called arclength on the file exchange that will return the arc length, WITHOUT needing to go through the intermediate of a polynomial fit. That polynomial fit may introduce errors that are unacceptable.
  1 个评论
Prabs
Prabs 2014-5-24
Hey John, I tried the arclength function that you've created. Doesnt seem to be working correctly for me. I've got x coordinates and y coordinates of the points that I've plotted, and I used 'fit' to plot the curve of best fit. Now in your arclength function, I'm using the x coordinates as px and the y coordinates as py. Are those the correct inputs I should be using? Correct me if I'm wrong please. Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by