Interpolating points in a 3D space with one line

9 views (last 30 days)
Hello,
I have 5 points (black) in a 3D space. I would like to interpolate these points with a linear LINE and get the coefficients of this linear function.
I have just found how to interpolate these points with a surface so far by using the 'linearinterp' command in the curve fitting toolbox (see the attached figure).
Thanks for your help!

Accepted Answer

Star Strider
Star Strider on 4 Jul 2022
Try something like this —
rpm = rand(5,1);
dosage = rand(5,1)*0.1;
mass_of_leaves = rand(5,1);
B = [dosage mass_of_leaves ones(size(dosage))] \ rpm
B = 3×1
4.6860 0.3402 -0.0069
x = linspace(min(mass_of_leaves), max(mass_of_leaves), numel(mass_of_leaves));
y = linspace(min(dosage), max(dosage), numel(dosage));
z = [x(:) y(:) ones(size(x(:)))] * B;
figure
plot3(mass_of_leaves, dosage, rpm, 'p')
hold on
plot3(x, y, z, '-k')
hold off
grid on
xlabel('mass of leaves')
ylabel('dosage')
zlabel('rpm')
mdl = fitlm([mass_of_leaves dosage], rpm)
mdl =
Linear regression model: y ~ 1 + x1 + x2 Estimated Coefficients: Estimate SE tStat pValue _________ ________ ________ ________ (Intercept) -0.006867 0.061301 -0.11202 0.92104 x1 0.34018 0.086056 3.953 0.058441 x2 4.686 0.72903 6.4277 0.023359 Number of observations: 5, Error degrees of freedom: 2 Root Mean Squared Error: 0.0479 R-squared: 0.98, Adjusted R-Squared: 0.96 F-statistic vs. constant model: 49.2, p-value = 0.0199
B = mdl.Coefficients.Estimate
B = 3×1
-0.0069 0.3402 4.6860
[ypred,yci] = predict(mdl, [x(:) y(:)]);
figure
plot3(mass_of_leaves, dosage, rpm, 'p')
hold on
plot3(x, y, ypred, '-k')
plot3(x, y, yci, '--k')
hold off
grid on
xlabel('mass of leaves')
ylabel('dosage')
zlabel('rpm')
.

More Answers (2)

Jonas
Jonas on 4 Jul 2022
have a look into the polyfitn() function on FEX

Matt J
Matt J on 4 Jul 2022
Edited: Matt J on 4 Jul 2022
You can use linear3dFit() from this FEX submission
Also, a 3D line is not given by a single equation, so it is not clear what you mean by "the coefficients".

Categories

Find more on Interpolation 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!