Multiple variables in non linear regression
古いコメントを表示
Hi,
Just downloaded Matlab with the ambition of trying to fit an equation i have to my data through adding curve fitting parameters.
The problem:
I have 3 independent vairables x1, x2, x3 and my output variable i am trying to fit them to is y
thus the equation looks like y=x1*x2*x3 and i want to add the following parameters A,m,n so the equation will looks roughly like
y=A*x1*(x2^m)*(x3^n) in order to fit my data
i know i cant use the curve fitting tool and am in need of help
Thanks in advance!
i am also using the 1 month trial version
4 件のコメント
Sebastian Daneli
2020 年 5 月 7 日
編集済み: Sebastian Daneli
2020 年 5 月 7 日
Try Rstudio, it's a free software for statistics, very popular, works like Matlab. You also have to download R, the programming language.
https://www.r-project.org/
Andrew Doyle
2020 年 5 月 7 日
Sebastian Daneli
2020 年 5 月 7 日
No, not really. Excel is on the other hand much "easier", at least somewhat more "straightforward"
Andrew Doyle
2020 年 5 月 7 日
回答 (2 件)
Star Strider
2020 年 5 月 7 日
編集済み: Star Strider
2020 年 5 月 7 日
Try this:
x1 = rand(1,10); % Independent Variable Vector
x2 = rand(1,10); % Independent Variable Vector
x3 = rand(1,10); % Independent Variable Vector
y = rand(1,10); % Dependent Variable Vector
xm = [x1(:) x2(:), x3(:)]; % X Matrix
yfcn = @(b,x) b(1).*x(:,1).*(x(:,2).^b(2)).*(x(:,3).^b(3)); % Objective Function
b0 = rand(3,1);
B = lsqcurvefit(yfcn,b0,xm,y(:))
That runs without error.
Make appropriate changes in the initial parameter estimates vector ‘b0’ to be compatible with your data.
EDIT —
I did not see that you had posted your data while I was writing my Answer.
Try this (with ‘y’ shortened by one element to be compatible with ‘xm’):
y = y(1:end-1);
xm = [x1(:) x2(:), x3(:)]; % X Matrix
yfcn = @(b,x) b(1).*x(:,1).*(x(:,2).^b(2)).*(x(:,3).^b(3)); % Objective Function
b0 = rand(3,1);
B = lsqcurvefit(yfcn,b0,xm,y(:))
producing:
B =
0.21033007874519
-1.94721481237717
0.123415136896506
Plotting it is not possible (would require four dimensions) so you need to determine if these are reasonable. If not, experiment with different values for the elements in the ‘b0’ vector to get the appropriate parameter values.
That consideration aside, and since ‘x2’ is constant, one way to plot it would be to leave out ‘x2’ from the plot and then select the view parameters to plot it as a 2D plot:
figure
plot3(x1, x3, y, 'p')
hold on
plot3(x1, x3, yfcn(B,xm), '-r')
hold off
grid on
xlabel('x_1')
ylabel('x_3')
view(0,90)
producing:

This appears to be a reasonably good fit!
15 件のコメント
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Star Strider
2020 年 5 月 7 日
Please see my edited Answer using your data (likely posted while you were posting your Comment). It is necessary to shorten ‘y’ by one element so that it has the same row size as ‘xm’.
Then my code works, and appears tror give a decent result!
Andrew Doyle
2020 年 5 月 7 日
Star Strider
2020 年 5 月 7 日
My pleasure!
Andrew Doyle
2020 年 5 月 7 日
Star Strider
2020 年 5 月 7 日
‘get the same error’
I am using the equation you specified as the objective function, and the data you provided.
I have no idea what you are doing otherwise. The data must have the same dimensions so that when combined in my ‘xm’ matrix (as I wrote it), it and the ‘y(:)’ vector have the same row length. I have no control over that without having the data you are actually using.
My code works with the data you posted, after editing the variables to be certain the row lengths were the same. That is the best I can do.
Andrew Doyle
2020 年 5 月 7 日
Star Strider
2020 年 5 月 7 日
My pleasure.
If you want to attach your data, I will do my best to fit it. I do not need to know the other details, my background being medicine and biomedical engineering, not mechanical engineeering or material science.
Andrew Doyle
2020 年 5 月 8 日
Andrew Doyle
2020 年 5 月 8 日
Andrew Doyle
2020 年 5 月 8 日
Star Strider
2020 年 5 月 8 日
It will likely not be a perfect match. I do not know if it is necessary to constrain the parameters (for example to be positive). I used the ga function with an unconstrained problem and with 100 different runs, and the best parameter set was:
7.437845749605835 1.070000000000000 -1.195894462198460
with a residual norm of:
54.741895805282986
with the function and residual norm calculated as:
xm = [x1(:) x2(:), x3(:)]; % X Matrix
yfcn = @(b,x) b(1) .* (x(:,1).^b(2) .* x(:,2).^b(3)) ./ x(:,3); % Objective Function
resnorm = @(theta) norm(y-yfcn(theta,xm));
Constrained to force all the parameters to be
, the parameters and residual norm are:
0.006956686973572 1.252512910246849 0.000000071048737
61.819773481310285
.
Ameer Hamza
2020 年 5 月 7 日
編集済み: Ameer Hamza
2020 年 5 月 7 日
If you have the optimization toolbox, then you can use lsqcurvefit. The following shows an example using random data
x1 = rand(100,1);
x2 = rand(100,1);
x3 = rand(100,1);
y = 2.*x1.*(x2.^3).*(x3.^2); % random y vectors with A=2, m=3, n=2
xdata = [x1 x2 x3];
model = @(A, m, n, x1, x2, x3) A.*x1.*(x2.^m).*(x3.^n);
model_lsq = @(param, xdata) model(param(1), param(2), param(3), ...
xdata(:,1), xdata(:,2), xdata(:,3));
sol = lsqcurvefit(model_lsq, rand(1,3), xdata, y)
A = sol(1);
m = sol(2);
n = sol(3);
Result
>> A
A =
2.0000
>> m
m =
3.0000
>> n
n =
2.0000
16 件のコメント
Andrew Doyle
2020 年 5 月 7 日
Ameer Hamza
2020 年 5 月 7 日
Yes, you can just type or paste the values. However, these must be column vector, so you need to put transpose operator (.') at the end like this
x1 = [1, 2, 3, ....].';
x2 = [9, 8, 7, ....].';
Andrew Doyle
2020 年 5 月 7 日
Ameer Hamza
2020 年 5 月 7 日
How did you put y-values? Can you show your values of x1, x2, x3, and y.
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Ameer Hamza
2020 年 5 月 7 日
Can you exactly paste the code you are trying to run?
Andrew Doyle
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
編集済み: Ameer Hamza
2020 年 5 月 7 日
Andrew Doyle
2020 年 5 月 7 日
Ameer Hamza
2020 年 5 月 7 日
Yes. The message indicates that you got a local minimum. That is why you get wrong 'y' values from the model. This is happening due to the very different scaling of x1, x2, and x3. Can you share the actual values of independent variables in this equation: t = A* (( E^m * f^n) / pi * d * l )). That will help in suggesting a suitable solution.
the cyclist
2020 年 5 月 7 日
x2 is a constant vector, which means that x2.^m is a constant vector, so that term is just multiplying by a constant factor. So, don't need that term.
Also, your y vector has 8 elements, and your x vector has 7 elements. Not sure what to do about that.
Andrew Doyle
2020 年 5 月 7 日
カテゴリ
ヘルプ センター および File Exchange で Get Started with Curve Fitting Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!