Fitting a curve (not a surface) for set of 3d points
1 view (last 30 days)
Show older comments
PASUNURU SAI VINEETH
on 6 May 2022
Commented: Star Strider
on 7 May 2022
We have a set of 3d points but could only generate a best fit surface using curve fitting tool. Is there a way to get the best fit curve? After obtaining such curve, how does one extract the sample points between the end points of the original data?
2 Comments
Accepted Answer
Star Strider
on 6 May 2022
I have no idea what model you want to fit to the data.
This does a linear regression —
A = [-27.9887180593615 -1340.75546563430 -2467.65439789032
-28.1559650942894 -1264.59303658420 -2404.50824038076
-29.7919277205741 -1193.39703512010 -2300.00965848429
-31.3981715714723 -1120.17736318384 -2235.27618908221
-31.5451983267780 -1051.10973092073 -2140.30777056763
-32.8491580387443 -984.646322264197 -2063.56414843781
-33.2796553901372 -916.991538791753 -1996.11865926438
-35.8395784569732 -852.031242733576 -1911.15081465986
-37.1566640499132 -790.346389117523 -1815.82738997354
-38.3204915465027 -730.214519353841 -1746.18203218839
-40.1649399572649 -673.409308255575 -1674.26444109182
-41.3035654666637 -620.400594024487 -1587.01503697274
-42.1747052709711 -566.984092840253 -1500.55643509168
-46.1774120240010 -510.506804241280 -1356.63240367480
-46.8217316506543 -465.341776809379 -1312.21258689622
-48.1823821035027 -419.235048839213 -1236.80682112429
-46.8035473839173 -379.332398226134 -1160.38961081556
-50.4447955974995 -332.367954503435 -1049.62531161825
-51.6744777763725 -299.816833129747 -985.059960723105
-53.7621363830258 -263.954442877378 -898.073182985995
-57.1994027666078 -225.149444101340 -777.496668735717
-56.6340768082701 -199.990460428311 -708.719962466197
-58.8969572639191 -165.998414537714 -571.734657662908
-59.2922728658662 -147.613479302020 -529.946182294136
-63.0658812601543 -122.123656702184 -427.943227983156
-64.9755480437970 -102.744870160475 -328.893225669812
-64.7611060843518 -85.0805137022426 -223.268913127146
-66.8461254640729 -72.8327833085849 -156.365146230629
-68.9474184651645 -60.7799766280343 -50.0131596499198];
figure
stem3(A(:,1), A(:,2), A(:,3), 'p')
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Original Data')
DM = [A(:,[1 2]) ones(size(A(:,1)))]; % Design Matrix
B = DM \ A(:,3); % Linear Regression
fprintf('\nRegression Coefficients:\n\tB(1) = %10.3f\n\tB(2) = %10.3f\n\tB(3) = %10.3f\n',B)
Zhat = DM * B; % Fitted 'Z' Values
ResidNorm = norm(A(:,3)-Zhat) % Norm Of Residuals
figure
scatter3(A(:,1), A(:,2), A(:,3), 'p', 'filled')
hold on
plot3(A(:,1), A(:,2), Zhat, '-r')
hold off
grid on
legend('Data','Linear Regression', 'Location','best')
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Data & Fitted Linear Regression')
The regress function and fitlm provide statistics on the parameters and linear fit. To do a nonlinear regression (with an appropriate model), use fitnlm.
.
4 Comments
Star Strider
on 7 May 2022
My linear regression was simply a demonstration because no model or other description of the data or problem was provided.
That appears to be essentially an exact fit.
A simpler model works as well:
DM = [ax.^2 ay.^2 ones(size(az))];
I doubt that it is possible to improve it, or to smooth it significantly, other than by interpolating ‘DM’ to a matrix with fewer points.
.
More Answers (0)
See Also
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!