# 'polyfit' for parametrized curves.

2 views (last 30 days)
David on 8 Dec 2022
Commented: David on 8 Dec 2022
%
Hello! My question is about the 'polyfit' routine, i have a collection of (x,y) points that are similar to a star, and i want to make a function out of thoose points in order to calculate a line integral. However, it's not a biyecitve function, so. It is possible for polyfit to fit a parametrized curve (x(t), y(t))? If not, there is another routine that makes that possible?
Torsten on 8 Dec 2022
Use "trapz" to calculate the line integral.

John D'Errico on 8 Dec 2022
Edited: John D'Errico on 8 Dec 2022
Can polyfit be made to solve this in a parameetric form? NO. Why not? Because even if it COULD be used for that purpose, the result you want to make is a piecewise linear function. Polyfit cannot solve that problem!
Can you solve it in a different way? Of course. But you will need to use a pair of splines, of a very specific type. If the curve really is represented as piecewise linear star segments, then you need linear spline segments. For example...
t = linspace(0,2*pi,11)';
xy = r.*[cos(t),sin(t)]
xy = 11×2
3.0000 0 0.8090 0.5878 0.9271 2.8532 -0.3090 0.9511 -2.4271 1.7634 -1.0000 0.0000 -2.4271 -1.7634 -0.3090 -0.9511 0.9271 -2.8532 0.8090 -0.5878
plot(xy(:,1),xy(:,2),'-o')
axis equal
So a 5 pointed star. Nobody can ever say all of my answers are pointless. ;-) Without merit, maybe.
We can create a simple function that can evaluate x(t) and y(t) separately as piecewise linear functions using griddedInterpolant.
x_t = griddedInterpolant(t,xy(:,1))
x_t =
griddedInterpolant with properties: GridVectors: {[11×1 double]} Values: [11×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
y_t = griddedInterpolant(t,xy(:,2));
Now, you should be able to perform a line integral along that path. since there are two parametric functions. For example, you can now plot x)t as functino of t, or evaluate it at any point.
tint = linspace(0,2*pi);
plot(tint,x_t(tint),'r',tint,y_t(tint),'g')
legend('x(t)','y(t)')
And integral will have no problems.
fxy = @(x,y) x.^2 + y.^2;
f_t = @(t) fxy(x_t(t),y_t(t));
integral(f_t,0,2*pi)
ans = 26.0272
David on 8 Dec 2022
That was exactly the answer i was looking for! Thank you, and congratulations, because all of your answers helped me, and all were so clear. <3

Walter Roberson on 8 Dec 2022
It is possible for polyfit to fit a parametrized curve (x(t), y(t))?
No, polyfit is strictly restricted to polynomials of the form (t, y(t)), never to (t, x(t), y(t)) .
In the Curve Fitting Toolbox, fit can be used to fit (t, x(t), y(t)) to a variety of different functions, such as 'poly23' (degree 2 in one of the independents, degree 3 in the other independent)
You can also use fit() to fit custom functions.
However, fit() expects the functions to be continuous with continuous first derivatives, and so would not be suitable for functions with sharp corners.