How to fit data in an array to a multidimensional function?

조회 수: 2 (최근 30일)
Philipp Müller
Philipp Müller 2022년 12월 16일
편집: Torsten 2022년 12월 20일
Hey there,
I have an 10x20x6 double Array. Those data inside should now be fit in an optimal way (e.g. nonlinear LS) to a function f(x,y,z).
The arrays colums corresponds to the x axis, the rows to the y axis and the depth to the z axis. The array entries should correspond to the functions value, respectively.
The function f has the form f = a1*x^2 + a2*x + b1*y^2 + b2*y + c1*z^2 + c2*z + d
The aim is, to find a1, a2, b1, b2, c1, c2 and d
I'd be very gratefull, if someone could explain to me, how to handle such a multidimensional fitting

채택된 답변

Torsten
Torsten 2022년 12월 16일
Generate four 1d column vector X,Y,Z and F of your data where F(i) is the value of your function corresponding to F(X(i),Y(i),Z(i)).
Then you can easily obtain the fit coefficients as
A = [X.^2,X,Y.^2,Y,Z.^2,Z,ones(size(X))];
b = F;
sol = A\b;
a1 = sol(1)
a2 = sol(2)
b1 = sol(3)
b2 = sol(4)
c1 = sol(5)
c2 = sol(6)
d = sol(7)
  댓글 수: 2
Philipp Müller
Philipp Müller 2022년 12월 20일
Hey, thanks for your help,
but I wounder, how to put the array data in a 1d Vector. Its length would be 10*20*6 and so the part
sol = A\b;
returs a huge matrix, where
a1 = sol(1)
a2 = sol(2)
b1 = sol(3)
b2 = sol(4)
c1 = sol(5)
c2 = sol(6)
d = sol(7)
are all zero
Torsten
Torsten 2022년 12월 20일
편집: Torsten 2022년 12월 20일
If your coordinate vectors are x(1:20),y(1:10) and z(1:6) and your 10x20x6 matrix is M,
count = 0;
X = zeros(10*20*6,1);
Y = zeros(10*20*6,1);
Z = zeros(10*20*6,1);
F = zeros(10*20*6,1);
for i = 1:10
for j = 1:20
for k = 1:6
count = count + 1;
X(count) = x(j);
Y(count) = y(i);
Z(count) = z(k);
F(count) = M(i,j,k);
end
end
end
A = [X.^2,X,Y.^2,Y,Z.^2,Z,ones(size(X))];
b = F;
sol = A\b;
a1 = sol(1)
a2 = sol(2)
b1 = sol(3)
b2 = sol(4)
c1 = sol(5)
c2 = sol(6)
d = sol(7)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by