MATLAB Answers

# Fitting data to a polar equation

조회 수: 58(최근 30일)
Omar Ashour 14 Feb 2021
댓글: Omar Ashour 16 Feb 2021
I have some data that looks like this: I have it in Cartesian coordinates but can convert it to polar coordinates of course. Now I need to fit it to a somewhat messy function of the form shown in the code below using the Curve Fitting Toolbox. The problem is that the function is in polar coordinates, and I have no clue how to write the custom fitting function to work with this (i.e. in the form y = f(x))
Here the fitting parameters should be kappa, k00, k01, etc. Any idea how to do this? The parameters in teh code snipped below are tuned to look somewhat similar to my data, but I need a more accurate fit not done by eye.
A = @(theta, kappa, mu, nu) cos(nu*kappa)*cos(mu*theta);
theta = linspace(0, 2*pi, 500);
kappa = 5.0;
k00 = 1.0;
k01 = 2.0;
k02 = -5.0;
k03 = -1.7;
k31 = 0.0;
k60 = 0.7;
k120 = 0.1;
FS = k00*A(theta, kappa, 0, 0) + k01*A(theta, kappa, 0, 1) + k02*A(theta, kappa, 0, 2) ...
+ k03*A(theta, kappa, 0, 3) + k31*A(theta, kappa, 3, 1) + k60*A(theta, kappa, 6, 0)...
+ k120*A(theta, kappa, 12, 0);
polarplot(theta, FS);
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Omar Ashour 14 Feb 2021
So to conclude, convert your data to polar coordinates using cart2pol and then do the fit using phi as the independent variable. What you'll see while fitting is something completely different from how you're used to looking at the data of course, but plot the fit afterwards in whichever way is appropriate to get the proper result.
Still open to better suggestions on how to do this.
Cheers.

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

### 채택된 답변

John D'Errico 14 Feb 2021
편집: John D'Errico 14 Feb 2021
It seems from this last comment that you re making some progress. There are some points I should make.
First, when you convert to a polar form, remember that a tool like cart2pol works around the origin. So if your data is offset, then you would need to translate it, so the origin is directly in the center. A mean shift is probably good much of the time, but if that translation is not perfect, then you would probably see a sinusoidal bias introduced into the expression r(theta).
Next, in the case of the relationship you show, a simple model is probably not too bad. Essentially, you would be creating a simple Fourrier series approximation. But if that is inadequate, you might be forced to use other model forms, perhaps regression spline based models.
plot(kx,ky,'o') The problem here, is that a simple fit of the form y=f(x) must fail, because this is not a single valued relationship.
After a polar transformation, we would see:
[theta,R] = cart2pol(px,py);
plot(theta,R,'o') And this is now a single valued function, thus R = f(theta). So in theory, things can be done.
We might now wish to fit this data using what is essentially a fourier series approximation. There will be a DC bias in there, so a constant term. The model would be of the general form:
R(theta) = a0 + a1*sin(b1*theta) + a2*cos(b2*theta) + a3*sin(b3*theta) + a4*cos(b4*theta) + ...
You could use as many terms there as you can estimate. Good starting values would be important of course. Is all good now? NO! You still have a significant problem.
At least in this data, we see cusps at every peak. That corresponds to essential singularities in the derivative, but still singularities. Can a functional form on the one I showed above represent a function with singularities well? Sadly, no. No simple trig series can represent a function with singulariites in it without infinitely many terms. Those cusps become a problem. And sadly, any other tool you use, such as a regression spline will also see a problem, since most functions do not handle singularities well, even if they are only in the derivatives.
##### 댓글 수: 1표시숨기기 없음
Omar Ashour 16 Feb 2021
Brilliant, thank you so much!

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

R2020b

### Community Treasure Hunt

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

Start Hunting!