Matlab fit to three dimensions function
Mostra commenti meno recenti
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 Commenti
the cyclist
il 28 Ago 2019
Do you also have the Statistics and Machine Learning Toolbox available, or only the Curve Fitting Toolbox?
standy
il 28 Ago 2019
Risposte (2)
Bruno Luong
il 28 Ago 2019
Modificato: Bruno Luong
il 28 Ago 2019
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
il 28 Ago 2019
Modificato: the cyclist
il 28 Ago 2019
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 Commenti
standy
il 28 Ago 2019
the cyclist
il 28 Ago 2019
Modificato: the cyclist
il 28 Ago 2019
Unless I misunderstood, you want all possible linear terms and squared terms. That's a quadratic polynomial. Notice that right underneath, in some commented-out code, I show the full notation if I had written out all terms.
standy
il 28 Ago 2019
the cyclist
il 28 Ago 2019
Yes
Categorie
Scopri di più su Linear and Nonlinear Regression in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!