필터 지우기
필터 지우기

'polyfit' for parametrized curves.

조회 수: 1 (최근 30일)
David
David 2022년 12월 8일
댓글: David 2022년 12월 8일
%
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?
  댓글 수: 1
Torsten
Torsten 2022년 12월 8일
Use "trapz" to calculate the line integral.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2022년 12월 8일
편집: John D'Errico 2022년 12월 8일
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)]
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
  댓글 수: 1
David
David 2022년 12월 8일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 12월 8일
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.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by