Matlab fit to three dimensions function

조회 수: 22 (최근 30일)
standy
standy 2019년 8월 28일
댓글: the cyclist 2019년 8월 28일
Hi
I got three input vectors: x1, x2, x3 and output vectory y. How do i fit to my custom function?
y=a0+a1*x1+a2*x2+a3*x3+a11*x1^2+a22*x2^2+a33*x3^2+a12*x1*x2+a13*x1*x3+a23*x2*x3
Obviously I want to get parameters a1, a2, ...
For two variables (x1, x2) i can do it like:
f = fit([x1, x2], y, 'poly33');
But I'm struggling to do that for the function above.
Any help appreciated.
  댓글 수: 2
the cyclist
the cyclist 2019년 8월 28일
Do you also have the Statistics and Machine Learning Toolbox available, or only the Curve Fitting Toolbox?
standy
standy 2019년 8월 28일
Yes, i got this toolbox.

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

답변 (2개)

Bruno Luong
Bruno Luong 2019년 8월 28일
편집: Bruno Luong 2019년 8월 28일
n = length(y);
A = [ones(n,1) x1(:) x2(:) x3(:) x1(:).^2 x2(:).^2 x3(:).^2 x1(:).*x2(:) x1(:).*x3(:) x2(:).*x3(:)] \ y(:)

the cyclist
the cyclist 2019년 8월 28일
편집: the cyclist 2019년 8월 28일
I would do it like this:
% Set the random number generator seed, for reproducibility
rng default
% Create some random data
N = 1000;
x1 = randn(N,1);
x2 = randn(N,1);
x3 = randn(N,1);
% Create a response variable with known coefficients, and some noise
y = 2 + 3*x1 + 5*x2 + 7*x3 ...
+ 11*x1.^2 + 13*x2.^2 + 17*x3.^2 ...
+ 19*x1.*x2 + 23*x1.*x3 + 29*x2.*x3 ...
+ 31*randn(N,1);
% Fit a quadratic model
mdl = fitlm([x1 x2 x3],y,'quadratic')
% % The above is equivalent to the following model, written out in full Wilkinson notation
% mdl = fitlm([x1,x2,x3],y,'y ~ x1 + x2 + x3 + x1^2 + x2^2 + x3^2 + x1:x2 + x1:x3 + x2:x3');
Almost all of this code is me creating the data, to illustrate everything. Since you have the data already, you should only need
mdl = fitlm([x1 x2 x3],y,'quadratic')
The resulting model object, mdl, has methods for lots of information about the model fit.
  댓글 수: 4
standy
standy 2019년 8월 28일
Ahhh
Makes sense now. I got the output like:
Linear regression model:
y ~ 1 + x1*x2 + x1*x3 + x2*x3 + x1^2 + x2^2 + x3^2
Estimated Coefficients:
Estimate SE tStat pValue
__________ __________ ________ _______
(Intercept) 1.4267 1.1233 1.2701 0.23281
x1 -0.015338 0.024769 -0.61922 0.54962
x2 -0.90665 3.3137 -0.27361 0.78995
x3 0 0 NaN NaN
x1:x2 0.0031125 0.012077 0.25772 0.80185
x1:x3 0 0 NaN NaN
x2:x3 0.765 3.0553 0.25038 0.80736
x1^2 6.8008e-05 0.00011935 0.56982 0.58137
x2^2 0 0 NaN NaN
x3^2 0 0 NaN NaN
Does it mean that a0=1, a1=-0.015338 etc?
the cyclist
the cyclist 2019년 8월 28일
Yes

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

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by