Fitting a curve to 3D data
Show older comments
Hi people, I have a problem with fitting 3D data. I have a 3D matrix (99x386x384) and each dimension of the matrix represents x, y and z coordinates and value inside the matrix s value at a certain point value(x,y,z). Basically, I have a cluster of data points. I want to fit that data to the 3D curve/volume. Does anyone know how to do it?
10 Comments
KSSV
on 7 Mar 2022
Why you want to fit data to 3D volume?
Bjorn Gustavsson
on 7 Mar 2022
So you have a scalar s that varies in 3-D? And you want some specific advice how to best represent that volume-distribution with a simple "few-parameter" function? What does the function look like? Is it something like a 3-D Gaussian? Smooth linear increase in an arbitrary gradient direction? Does s obey some physics-based conservation-laws, like a convection-diffusion dominated continuity-equation?
So many question - and I havent even started yet...
Nikola Segedin
on 7 Mar 2022
Nikola Segedin
on 7 Mar 2022
Walter Roberson
on 7 Mar 2022
The best technique is going to depend a lot on the mathematical model that you are using.
You indicate that you have a cluster of data points. Is that along the lines that most of the locations in the array have an s value indicating "not present" and that the other ones form a shape that you want to model along with the s value? For example s==0 being unoccupied and all other values being meaningful?
Or is it a cuboid in which all s values are relevant?
Nikola Segedin
on 7 Mar 2022
Walter Roberson
on 7 Mar 2022
Degree 16 polynomials are almost always garbage for coordinates outside +/- 1. The high order terms grow so quickly that they overwhelm numeric accuracy considerations.
It would be more likely that a trig or exponential or Gaussian or spherical bessel was involved, with what you found being similar to a truncated Taylor expansion. (A truncated Taylor expansion of a trig function gets very bad by half a period but the original trig can still be quite meaningful.
Nikola Segedin
on 7 Mar 2022
Torsten
on 7 Mar 2022
I wonder why people always want analytical expressions, especially for something like a surface in 4d.
Use
sq = interp3(X,Y,Z,S,xq,yq,zq)
if you want a good approximation sq to your data in a query point (xq,yq,zq).
Bjorn Gustavsson
on 7 Mar 2022
@Nikola Segedin if it looks like a Gaussian then tweak the Gaussian instead of jumping to polynomial fits. Perhaps something like this would be better:
Or some similar modifications...
Answers (1)
Bjorn Gustavsson
on 7 Mar 2022
If you need to "get an idea to start" you can start from here:
function err = your_3D_error_fcn(pars,x,y,z,S,sigmaS,idx_is_OK_linear,fit_fcn)
S_model = fit_fcn(pars,x,y,z);
err = sum((S(idx_is_OK_linear)-S_model(idx_is_OK_linear)).^2./sigmaS(idx_is_OK_linear).^2)
end
This function you could use to find the best fiting parameters for a model-function using fminsearch:
fit_G3D = @(pars,x,y,z) pars(1)*exp(-(x-pars(2)).^2/pars(3)^2).* ...
exp(-(x-pars(4)).^2/pars(5)^2).* ...
exp(-(x-pars(6)).^2/pars(7)^2);
% Guessing parameters for an initial 3-D Gaussian with peak at 1 centred at
% [x,y,z] = [2 3 4] with widths in all directions equal to 1/2
pars0 = [1, 2,1/2,3,1/2,4,1/2];
pars_best = fminsearch(@(pars) your_3D_error_fcn(pars,x,y,z,S,sigmaS,idx_is_OK_linear,fit_G3D),pars0);
Here you'll have to provide the 3-D coordinates of x, y, z and s as well as an array with the linear indices to the good points and the standard-deviation of s (if that doesn't apply just remove it from the error-function). The error-function you'll have to adjust to something that suits your problem.
HTH
1 Comment
Nikola Segedin
on 7 Mar 2022
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!