# Interpolating points in a 3D space with one line

4 ビュー (過去 30 日間)
Annemarie 2022 年 7 月 4 日

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). サインインしてコメントする。

### 採用された回答

Star Strider 2022 年 7 月 4 日
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') .
##### 2 件のコメント表示非表示 1 件の古いコメント
Star Strider 2022 年 7 月 4 日
As always, my pleasure!

サインインしてコメントする。

### その他の回答 (2 件)

Jonas 2022 年 7 月 4 日
have a look into the polyfitn() function on FEX
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Matt J 2022 年 7 月 4 日

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".

サインインしてコメントする。

### カテゴリ

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!