Finding length of curve created using 'fit' function

11 views (last 30 days)
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.

Answers (2)

Star Strider
Star Strider on 23 May 2014
Edited: Star Strider on 24 May 2014
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 on 23 May 2014
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 Comment
Prabs
Prabs on 24 May 2014
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!