Solved – Solving “n” equations with 3 unknowns

lmoptimizationrregression

I'm new to R and I'm trying to solve a system of equations. I have about 380 equations where i have 3 unknowns per equation. I can use three equations and solve by using "solve()" and it works great. I used this link to make that works:

Solving simultaneous equations with R

Now, how do I optimize my result so it best fits across the rest of the 377 equations? I saw an optimization page on CRAN but I couldn't make much use of it. I'm still researching and I will post any solution that I find.

Best Answer

If your equations are linear, as suggested by the link you provide, you need to do linear regression to solve all the equations simultaneously:

# creating some random data
set.seed(123)
N = 280
# Ax = b is the system to solve
A = matrix(sample(1:100, 3*N, replace=TRUE), ncol=3, nrow=N)
colnames(A) = c("x", "y", "z")
b = sample(1:100, N, replace=TRUE)

# first system
ind0 = sample(N, 3) # 3 random equations
solve(A[ind0,], b[ind0])

# x         y         z 
# -6.054476  6.898068 -4.457295

# second system
ind1 = sample(N, 3) # another 3 random equations
solve(A[ind1,], b[ind1])

# x         y         z 
# 0.5521134 0.5771540 0.5728314

# solving for all equations
sol = lm(b ~ . + 0, data=data.frame(A, b))

coef(sol)

# x         y         z 
# 0.1514154 0.3657049 0.3767679
Related Question