3D curve fitting
Afficher commentaires plus anciens
I am a beginner in MATLAB, and now I have obtained a point cloud data for 3D curve fitting relative to these points, not surface fitting. Is there any method that can achieve good 3D curve fitting? thanks
11 commentaires
Mathieu NOE
le 12 Juin 2023
you could look at this (File exchange)
tabf
le 12 Juin 2023
Mathieu NOE
le 13 Juin 2023
do you have started a code ? do you have some data ?
tabf
le 15 Juin 2023
Mathieu NOE
le 15 Juin 2023
do you mind sharing your code and data ?
tabf
le 15 Juin 2023
Mathieu NOE
le 15 Juin 2023
Modifié(e) : Mathieu NOE
le 15 Juin 2023
I wonder if you want to fit a model or simply smooth the data and get something like this :

if this is what you want , simply download this FEX submission :
and use this code
x = N(:, 1);
y = N(:, 2);
z = N(:, 3);
u = smoothn({x,y,z},1e4);
plot3(x,y,z,'r.',u{1},u{2},u{3},'k','linewidth',2)
axis tight square
tabf
le 15 Juin 2023
tabf
le 23 Juin 2023
Mathieu NOE
le 23 Juin 2023
this is a code to find a polynomial fit for the S shaped groove (trajectory)

N = readmatrix('S.txt');
x = N(:, 1);
y = N(:, 2);
z = N(:, 3);
% detrend the Z data
order = 1;
p = polyfitn([x,y],z,order);
pC = p.Coefficients; % get the polynomial coefficients
pTerms = p.ModelTerms;
% create the polynomial model (z = f(x,y))
zt = 0;
for k = 1:numel(pC)
zt = zt + pC(k)*(x.^pTerms(k,1)).*(y.^pTerms(k,2)); %
end
figure(1),
plot3(x,y,z,'r.',x,y,zt,'.k','linewidth',2); %
xlabel('X');
ylabel('Y');
zlabel('Z');
legend('raw data','fitted plane');
axis tight square
% apply detrend to the Z data
zd = z - zt;
figure(2),
plot3(x,y,zd,'.','linewidth',2); %
xlabel('X');
ylabel('Y');
zlabel('Z');
axis tight square
% keep the highets z points to get the S shape of the groove
id = (zd>0.85*max(zd));
xx = x(id);
yy = y(id);
% make sure x data is unique and sorted
[xx,ia,ic] = unique(xx);
yy = yy(ia);
% Fit a polynomial p of degree "degree" to the (x,y) data:
degree = 5;
p = polyfit(xx,yy,degree);
% Evaluate the fitted polynomial p and plot:
yyf = polyval(p,xx);
eqn = poly_equation(flip(p)); % polynomial equation (string)
Rsquared = my_Rsquared_coeff(yy,yyf); % correlation coefficient
figure(3);plot(xx,yy,'*',xx,yyf,'-')
xlabel('X');
ylabel('Y');
legend('data',eqn)
title(['Data fit , R² = ' num2str(Rsquared)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R² correlation coefficient computation
% The total sum of squares
sum_of_squares = sum((data-mean(data)).^2);
% The sum of squares of residuals, also called the residual sum of squares:
sum_of_squares_of_residuals = sum((data-data_fit).^2);
% definition of the coefficient of correlation is
Rsquared = 1 - sum_of_squares_of_residuals/sum_of_squares;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function eqn = poly_equation(a_hat)
eqn = " y = "+a_hat(1);
for i = 2:(length(a_hat))
if sign(a_hat(i))>0
str = " + ";
else
str = " ";
end
if i == 2
eqn = eqn+str+a_hat(i)+" * x";
else
eqn = eqn+str+a_hat(i)+" * x^"+(i-1)+" ";
end
end
eqn = eqn+" ";
end
tabf
le 24 Juin 2023
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Linear and Nonlinear Regression dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

