Curve (Line) Fitting in 3D

조회 수: 24 (최근 30일)
Alec Day
Alec Day 2017년 4월 27일
편집: Sarvesh Singh 2018년 2월 15일
Hi, I've noticed John D'Errico answering a few of these questions but the polyfitn doesn't seem at first trial to do what I want. But I have some data in x,y,z coordinates. See attached for coordinates. I have manually selected x,y from images based on z = [0:2:100]. However I'm not sure how to fit a curve to this in 3D in order to remove error due to manual selection.
  댓글 수: 2
Star Strider
Star Strider 2017년 4월 27일
What function do you want to fit to your data?
Alec Day
Alec Day 2017년 5월 1일
I've ended up using polyfitn to find z = f(x) and z = f(y) to define a line in 3D space. seems to fit pretty well between z = 0-100.

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

채택된 답변

John D'Errico
John D'Errico 2017년 4월 27일
편집: John D'Errico 2017년 4월 27일
Here is your data, plotted:
PLOT EVERYTHING.
You have what appears to be a relationship where the dominant thing happening is a curve in (x,y,z). Think of it as a path through space, with a cloud of noise around that path.
NO. You CANNOT use polyfitn here. There is insufficient information to build a viable model, of the form z(x,y). Note that z(x,y) would be a surface, defined everywhere. To try to infer the coefficients of such a model would be essentially impossible to do well. If you try to use polyfitn, the first thing you would then do is send me a plaintive e-mail, asking why polyfitn does not do well on your data! Similarly, do NOT use a tool like gridfit. It too will fail to work well here, as your data is clustered fairly tightly around this path in the (x,y,z) space.
Hey, I'm just trying to forestall the inevitable plaintive e-mails!
Again, you have what appears to be a 3-d space curve , with noise that varies along the path, sometimes called heteroscedastic noise. Yes, it hurts my eyes just to try to type the word. But it just means that the noise distribution varies spatially, so the variance changes with location.
Next, it appears as if you have a problem with errors in all three variables. Nonlinear errors in variables problems can be nasty.
The real question is, what will you do with the resulting model? Are you looking for a nice smooth curve that goes through the middle of that banana shaped blob, just for a plot? Are you looking for predictive ability of some sort? Or are you really hoping for some sort of surface? (If the latter, abandon all hope, ye who enter here. You will need to get better data for that.)
  댓글 수: 3
Alec Day
Alec Day 2017년 4월 30일
Hi John, thanks for the very detailed and prompt response. What i need is as you wrote 'a nice smooth curve that goes through the middle of that banana shaped blob' I don't need to be able to predict f(x,y) outside of the defined values for z (0-100 as plotted). But just a way of having a smooth curve somewhere through the middle, which should be able to be repeatable given the same data-points.
John D'Errico
John D'Errico 2017년 5월 1일
As I said, this is tough to do. I once looked at some ideas for code to post for the general problem, but I could not make something work. What seems relatively easy by eye is not really so easy. That is often the case. It is sort of a 3-d errors in variables, with points projected onto a curve, which must then be a spline of some kind. It can be done, but it won't be robust/stable at all.

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

추가 답변 (1개)

Steffen B. Petersen
Steffen B. Petersen 2017년 12월 16일
I read your question such that you have a large number of points in 3D (x,y,z), and you want to fit a line through these points.
You should principal component analysis : First you 'whiten' your data by subtracting the mean of you data from each data point. Let us call your mean 'myaverage' - this is point in 3D space at the center of your data. Let us call the whitened data (X,Y,Z)
(X,Y,Z)=(x,y,z)-myaverage
Second you use pca to extract the coefficients - in this case a 3x3 matrix where the first column is the vector that has the largest varians over your dataset.
[coefficient]=pca(X,Y,Z);
You can now draw a line through your datapoints using
mypoint=myaverage+t*coefficient(:,1)
by varying the parameter t you find the points for your fitted line. From this point off you may want to compute the RMS deviation - but that probably is not your problem.
Regards
Steffen
  댓글 수: 1
Sarvesh Singh
Sarvesh Singh 2018년 2월 15일
편집: Sarvesh Singh 2018년 2월 15일
Hi Steffen,
I'm trying to implement this. Can you please elaborate what this parameter 't' is and what should be its typical value for the best fit. Also is it possible to fit any other curve instead of line using this method?
Regards,
Sarvesh

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

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by