'polyfit' for parametrized curves.
2 views (last 30 days)
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?
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)';
rad = [1 3];
r = mod((1:11)',2)*diff(rad) + rad(1);
xy = r.*[cos(t),sin(t)]
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))
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);
And integral will have no problems.
fxy = @(x,y) x.^2 + y.^2;
f_t = @(t) fxy(x_t(t),y_t(t));
More Answers (1)
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.