Say you have the following system of equations:
100x + 200y + 200z = 250
95x + 180y + 190z = 220
85x + 210y + 210z = 240
with additional constraints on each variable given by:
x >= .55
x <= .75
y >= .80
y <= .95
z >= .10
z <= .25
I have used the lpSolveAPI library in R and there is no solution, which may be true. That is fine; I don't dispute that.
What I am looking for is the solution such that the total error is minimized (i.e. the value of the left hand side vs. the right hand side). What are the best values of x, y and z such that the sum of all the errors of each equation is minimized.
How could I structure this in R using lpSolveAPI? The example below has 3 equations with 3 variables but I'd like to solve a more complex example with 50 equations of the same 3 variables. For reference, a similar approach but where there is a solution is here: Solving a feasible system of linear equations using Linear Programming
I have a feeling I need to modify the objective function in some way. Any thoughts?
Best Answer
I would add slack variables $s_i$ to each of your 3 equations in the system. Thus, these variables represent the error for each function. However, we have to be careful about the sign of each slack variable because we want the sum of their magnitudes to be minimized in the objective function. So we want to minimize $\sum |s_i|$. Can you take it from here? There are excellent resources on here about introducing more variables to get rid of the absolute values in the objective function of an LP.
Edit: For example add constraints $$t_{ip} - t_{im} = s_i$$
$$\forall t \geq 0$$
And the objective is minimize $\sum_i t_{ip} + t_{im}$