requationequation-solving

Solve linear system in R


I have a system of 3 equations :

g + v_1 = 8  +1/2 *v_1 +1/4 *v_2 + 1/4 *v_3
g + v_2 = 16 +1/2 *v_1 +0   *v_2 + 1/2 *v_3
g + v_3 = 7  +1/4 *v_1 +1/4 *v_2 + 1/2 *v_3

Setting v_3 = 0 one can obtain v_1=1.33,v_2 =7.47,v_3=0,g=9.2.

How can I solve this system in R ?

A = matrix(c(1/2,1/4,1/4,
             1/2,0,1/2,
             1/4,1/4,1/2),3,byrow=TRUE);A
q = c(8,16,7)

Solution

  • There is the built in solve() function for this task. First a little algebra is required. Simplifying your equations becomes:

     -8  = -1/2 *v_1 +1/4 *v_2 + 1/4 *v_3 - g
     -16 = +1/2 *v_1 -1   *v_2 + 1/2 *v_3 - g
     -7  = +1/4 *v_1 +1/4 *v_2 - 1/2 *v_3 - g
    

    There are 4 unknowns; g, v1, v2 and v3 but only 3 equations. An assumption is required: v3=0

     -8  = -1/2 *v_1 +1/4 *v_2  - g
     -16 = +1/2 *v_1 -1   *v_2 - g
     -7  = +1/4 *v_1 +1/4 *v_2  - g
    

    Now your matrix is:

    A = matrix(c(-1/2, 1/4, -1,
                  1/2,  -1, -1,
                  1/4, 1/4, -1), 3, byrow=TRUE)
    

    the constants are:

    B = -c(8,16,7)
    

    Use the solve() function.

     solve(A, B)
     #[1] 1.333333 7.466667 9.200000