Multiple variables in non linear regression
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Sebastian Daneli
am 7 Mai 2020
No, not really. Excel is on the other hand much "easier", at least somewhat more "straightforward"
Antworten (2)
Star Strider
am 7 Mai 2020
Bearbeitet: Star Strider
am 7 Mai 2020
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 Kommentare
Star Strider
am 8 Mai 2020
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
am 7 Mai 2020
Bearbeitet: Ameer Hamza
am 7 Mai 2020
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 Kommentare
the cyclist
am 7 Mai 2020
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.
Siehe auch
Kategorien
Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!