3D curve fitting
Mostra commenti meno recenti
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 Commenti
Mathieu NOE
il 12 Giu 2023
you could look at this (File exchange)
tabf
il 12 Giu 2023
Mathieu NOE
il 13 Giu 2023
do you have started a code ? do you have some data ?
tabf
il 15 Giu 2023
Mathieu NOE
il 15 Giu 2023
do you mind sharing your code and data ?
tabf
il 15 Giu 2023
Mathieu NOE
il 15 Giu 2023
Modificato: Mathieu NOE
il 15 Giu 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
il 15 Giu 2023
tabf
il 23 Giu 2023
Mathieu NOE
il 23 Giu 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
il 24 Giu 2023
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Linear and Nonlinear Regression in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

