Filter löschen
Filter löschen

Need help with a multivariable nonlinear regression script error trying to multiply 2 variables

1 Ansicht (letzte 30 Tage)
I am having troubles with the following script for a non linear regression. Any help would be much appreciated. (The M values are very small)
X = [vi,vi,Mpost,Mpre] y = Q V=950*10^(-6) i=6*10^(-6) modelfun = @(b,x) V*b(1)-((((x(:,3))*((x(:,1))))-(1-(i/V))*(x(:,4))*(x(:,2)))) beta0 = [-6000] mdl = fitnlm(X,y,modelfun,beta0)
X =
4.5853 4.5853 0.0000 0
4.5681 4.5681 0.0000 0.0000
4.5509 4.5509 0.0000 0.0000
4.5336 4.5336 0.0000 0.0000
4.5163 4.5163 0.0000 0.0000
4.4989 4.4989 0.0000 0.0000
4.4815 4.4815 0.0000 0.0000
4.4641 4.4641 0.0000 0.0000
4.4467 4.4467 0.0000 0.0000
4.4293 4.4293 0.0000 0.0000
4.4118 4.4118 0.0000 0.0000
4.3943 4.3943 0.0000 0.0000
4.3767 4.3767 0.0000 0.0000
4.3592 4.3592 0.0000 0.0000
4.3416 4.3416 0.0000 0.0000
4.3240 4.3240 0.0000 0.0000
4.3064 4.3064 0.0000 0.0000
4.2887 4.2887 0.0000 0.0000
4.2711 4.2711 0.0000 0.0000
4.2534 4.2534 0.0000 0.0000
4.2357 4.2357 0.0000 0.0000
4.2180 4.2180 0.0000 0.0000
4.2002 4.2002 0.0000 0.0000
4.1825 4.1825 0.0000 0.0000
4.1647 4.1647 0.0000 0.0000
4.1469 4.1469 0.0000 0.0000
y =
-415.0974
-403.3474
-412.2701
-398.9688
-395.1909
-376.4527
-344.7533
-315.6087
-279.5409
-227.0259
-161.0164
-126.6837
-104.0480
-63.0274
-53.1502
-40.5565
-40.5068
-37.6305
-31.9914
-25.5591
-21.4596
-28.7419
-27.9112
-25.2559
-22.9237
-18.1073
L =
8.9000e-05
M =
1.1000e-05
V =
9.5000e-04
i =
6.0000e-06
modelfun =
@(b,x)V*b(1)-((((x(:,3))*((x(:,1))))-(1-(i/V))*(x(:,4))*(x(:,2))))
beta0 =
-6000
Error using nlinfit (line 205) Error evaluating model function '@(b,x)V*b(1)-((((x(:,3))*((x(:,1))))-(1-(i/V))*(x(:,4))*(x(:,2))))'.
Error in NonLinearModel/fitter (line 1122) [model.Coefs,~,J_r,model.CoefficientCovariance,model.MSE,model.ErrorModelInfo,~] = ...
Error in classreg.regr.FitObject/doFit (line 220) model = fitter(model);
Error in NonLinearModel.fit (line 1430) model = doFit(model);
Error in fitnlm (line 94) model = NonLinearModel.fit(X,varargin{:});
Caused by: Error using * Inner matrix dimensions must agree.
trying to do it as a table yields the same result: >> tlb=table(vi,vi1,Mpost,Mpre,Q) L=89*10^(-6) M=11*10^(-6) V=950*10^(-6) i=6*10^(-6) modelfun = @(b,x)V*b(1)*(x(:,3)*x(:,1)-(1-(i/V))*x(:,4)*x(:,2)) beta0 = [-6000] mdl = fitnlm(tlb,modelfun,beta0)
tlb =
vi vi1 Mpost Mpre Q
______ ______ __________ __________ _______
4.5853 4.6026 6.9474e-08 0 -415.1
4.5681 4.5853 1.3851e-07 6.9474e-08 -403.35
4.5509 4.5681 2.0711e-07 1.3851e-07 -412.27
4.5336 4.5509 2.7527e-07 2.0711e-07 -398.97
4.5163 4.5336 3.4301e-07 2.7527e-07 -395.19
4.4989 4.5163 4.1032e-07 3.4301e-07 -376.45
4.4815 4.4989 4.772e-07 4.1032e-07 -344.75
4.4641 4.4815 5.4366e-07 4.772e-07 -315.61
4.4467 4.4641 6.097e-07 5.4366e-07 -279.54
4.4293 4.4467 6.7532e-07 6.097e-07 -227.03
4.4118 4.4293 7.4053e-07 6.7532e-07 -161.02
4.3943 4.4118 8.0533e-07 7.4053e-07 -126.68
4.3767 4.3943 8.6971e-07 8.0533e-07 -104.05
4.3592 4.3767 9.3369e-07 8.6971e-07 -63.027
4.3416 4.3592 9.9727e-07 9.3369e-07 -53.15
4.324 4.3416 1.0604e-06 9.9727e-07 -40.557
4.3064 4.324 1.1232e-06 1.0604e-06 -40.507
4.2887 4.3064 1.1856e-06 1.1232e-06 -37.631
4.2711 4.2887 1.2476e-06 1.1856e-06 -31.991
4.2534 4.2711 1.3092e-06 1.2476e-06 -25.559
4.2357 4.2534 1.3704e-06 1.3092e-06 -21.46
4.218 4.2357 1.4312e-06 1.3704e-06 -28.742
4.2002 4.218 1.4916e-06 1.4312e-06 -27.911
4.1825 4.2002 1.5517e-06 1.4916e-06 -25.256
4.1647 4.1825 1.6114e-06 1.5517e-06 -22.924
4.1469 4.1647 1.6707e-06 1.6114e-06 -18.107
L =
8.9000e-05
M =
1.1000e-05
V =
9.5000e-04
i =
6.0000e-06
modelfun =
@(b,x)V*b(1)*(x(:,3)*x(:,1)-(1-(i/V))*x(:,4)*x(:,2))
beta0 =
-6000
Error using nlinfit (line 205) Error evaluating model function '@(b,x)V*b(1)*(x(:,3)*x(:,1)-(1-(i/V))*x(:,4)*x(:,2))'.
Error in NonLinearModel/fitter (line 1122) [model.Coefs,~,J_r,model.CoefficientCovariance,model.MSE,model.ErrorModelInfo,~] = ...
Error in classreg.regr.FitObject/doFit (line 220) model = fitter(model);
Error in NonLinearModel.fit (line 1430) model = doFit(model);
Error in fitnlm (line 94) model = NonLinearModel.fit(X,varargin{:});
Caused by: Error using * Inner matrix dimensions must agree.
Best regards: Emil Stender

Antworten (1)

Star Strider
Star Strider am 25 Sep. 2015
You need to vectorise your model function:
modelfun = @(b,x) V.*b(1)-((((x(:,3)).*((x(:,1))))-(1-(i./V)).*(x(:,4)).*(x(:,2))));
I vectorised all the operations, even though some likely involve only scalars. See the documentation on Array vs. Matrix Operations for details.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by