We are trying to do nonlinear least squares fitting in R. We have reference code in MATLAB as below.
A = [ -4.09549023 -1.5924967 -5.2775267 0.7195365 -1.4681932;
-0.09302291 0.2538085 0.6080219 0.1413484 -0.4614447;
1.00000000 1.0000000 1.0000000 1.0000000 1.0000000;]
b = [-0.52701539;0.08974224;1.00000000]
[w,res] = lsqnonneg(A, b)
w =
0
0
0.1377
0.6700
0.1922
res =
1.2519e-32
If we try the same thing in R, the results are entirely different.
A <- rbind(cbind(4.09549023, -1.5924967, -5.2775267, 0.7195365, -1.4681932),
cbind(-0.09302291, 0.2538085, 0.6080219, 0.1413484, -0.4614447),
cbind(1.00000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000))
b <- rbind(-0.52701539,0.08974224,1.00000000)
sol <- lsqnonneg(A,as.vector(b))
$x
[1] 0.2212015 0.1986838 0.1890215 0.2080815 0.1830117
$resnorm
[1] 4.889664e-10
$residual
[,1]
[1,] 2.871547e-11
[2,] 4.866847e-10
[3,] -3.743827e-11
The problem is, though the input values of A
and b
are the same, and the methods invoked both in R and MATLAB have the same purpose (viz., non-negative least squares fitting) the residuals are different in both cases. resnorm
= 4.889664e-10 in R and res
= 1.2519e-32 in MATLAB. If anyone can point out some obvious mistake in the results, or suggest some alternative method in R to match the method in MATLAB, it would be really appreciated.