# Can i get arc length out of polyval?

2 views (last 30 days)
Daniel Wurster on 25 May 2019
Commented: John D'Errico on 25 May 2019
Hello, i would like to know if i can get the arc lenght out of a polyval.
I work on the following steps:
1) Have data (vector) x and y(x)
2) fit a function between both data -> here polyval
3) now i want to calcuculate the arc lenght
I got a bigger project, but a simple function with the three steps would be like:
% 1) based on
x = 1:10;
y = 2+x.^2.;
% 2) fit
p = polyfit(x,y,3);
% create a function handle for the integral
f = @(arg) polyval(p,arg);
%now the function for the arc length
r = sqrt(1+(diff(f)).^2.);
b = integral(r,0,10);
%plot it
xpl = 1:0.01:10;
plot(x,y,'r+',xpl,f(xpl));
Error report i get:
/* Undefined function 'diff' for input arguments of type 'function_handle'.
Error in asdfasdf (line 12)
r = sqrt(1+(diff(f)).^2.); */
-> What can i do to solve the error report?

Star Strider on 25 May 2019
Try this (lightly edited version of your code):
dfdx = @(f,x) (f(x + 1E-8) - f(x)) ./ 1E-8; % Simple Numeric Derivative
% 1) based on
x = 1:10;
y = 2+x.^2.;
% 2) fit
p = polyfit(x,y,3);
% create a function handle for the integral
f = @(arg) polyval(p,arg);
%now the function for the arc length
r = @(x) sqrt(1+(dfdx(f,x)).^2.);
b = integral(r,0,10);
##### 3 CommentsShow 1 older commentHide 1 older comment
Star Strider on 25 May 2019
As always, my pleasure!
That is a simple numerical differentiation function. It takes function handle ‘f’ and independent variable ‘x’ as arguments, and returns the numerical derivative of ‘f’ at ‘x’.
Numerical differentiation functions can get much more elaborate. See for example the Wikipedia article on Numerical differentiation (link).
John D'Errico on 25 May 2019
I might note that the line in question is just a numerical derivative. However, you can compute the analytical derivative simply enough, since it ia a polynomial.
polyder does that for you, already a part of MATLAB.