Closed contour with least square differences to dataset
이전 댓글 표시
Trying to reproduce a data model where both X and Y values in a dataset are summarised using the fourier parameters of a closed loop (FPCC) - but the fit function only works with one value of Y for one value of X (whereas a closed loop would have multiple values of Y for one value of X). Does anyone have ideas on how to approach this?
댓글 수: 2
John D'Errico
2025년 9월 21일
편집: John D'Errico
2025년 9월 21일
So what then is the problem? Do exactly as I suggested.
- Convert to polar coordinates, by subtracting off the mean, and then using cart2pol.
- Model the result using a series of sin and cosine terms (plus a constant), getting a model in the form r(theta).
- Once you have a model r(theta), convert back to the original cartesian form, that is:
x = xmean + r(theta)*cos(theta)
y = ymean + r(theta)*sin(theta)
The model you will then generate is exactly what you seem to be asking to get, and I showed you exactly how to do that. Again, what is the problem?
답변 (2개)
댓글 수: 1
but the fit function only works with one value of Y for one value of X
Not true, strictly speaking. For example, below every x has two corresponding values y1 and y2. Appropriately, the fit() function finds an intermediate curve.
x=linspace(0,1)';
y1=sin(x);
y2=y1+1;
X=[x;x]; Y=[y1;y2];
f=fit(X,Y,'sin1');
plot(f,X,Y); axis padded
This can be quite difficult if you have some completely arbitrary form. But you tell us this is a simple closed form. And as long as that form is not too nasty looking, you can do it easily.
The solution approach I would take is to start with a transformation. You have a problem that is mappable to a circle. SO DO THAT FIRST! That is, convert to polar coordinates. Since you show no data at all, I need to make some up.
t = (0:100)'*2*pi/101;
xy = randn(1,2)*10 + [cos(t),sin(t)]*randn(2,2)*10 + randn(1,2)*20 + randn(numel(t),2)/5;
x = xy(:,1);
y = xy(:,2);
plot(x,y,'.')
The result is in fact, just a random ellipse. I could have been more interesting about what I did, maybe something oval, or made it less obvious, but I've not yet had breakfast. Please don't ask me to think. Anyway, pretend we don't know the true shape, as then we could have used a tool out there that can fit ellipses to data. So, how can we fit a curve to this? First, go polar. (It is getting cold.)
mux = mean(x);muy = mean(y);
[theta,rad] = cart2pol(x-mux,y-muy);
Now, plot it. But don't use a polar plot. We need to think of this as just a cartesian thing now. So plot will suffice.
plot(theta,rad,'.')
You can use the same transformation on the model form you have, and do a fit to your closed contour. In this case, I'm just going to use a fourier series approximation for a fit.
mdl = fit(theta,rad,'fourier4');
hold on
xhat = linspace(min(theta),max(theta),500);
plot(xhat,mdl(xhat),'r-')
Reconstructing the original curve is now simple. Just convert back from polar coordinates. Then add the means back in, and plot.
카테고리
도움말 센터 및 File Exchange에서 Geology에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



