Content

経済モデルは種々の変数の相互関係から成る多元多次連立方程式。ここでは4元1次方程式をRで解いてみましょう。

\[\left\{\begin{eqnarray} 5x&+&8y&-&6z&+&2s&=&24 \\ -4x&+&2y&+&3z&-&4s&=&10 \\ 3x&-&2y&+&8z&+&5s&=&18 \\ -3x&-&12y&+&4z&-&25s&=&8 \end{eqnarray}\right.\]

a <- matrix(data = rbind(c(5,8,-6,2),c(-4,2,3,-4),c(3,-2,8,5),c(-3,-12,4,-25)),nrow = 4,ncol = 4)
b <- c(24,10,18,8) # または b <- matrix(data = c(24,10,18,8),nrow = 4,ncol = 1)
result <- solve(a,b)
a;cat('----------------\n')
b;cat('----------------\n')
result;cat('----------------\n')
sapply(1:4,function(x)sum(result*a[x,]))
     [,1] [,2] [,3] [,4]
[1,]    5    8   -6    2
[2,]   -4    2    3   -4
[3,]    3   -2    8    5
[4,]   -3  -12    4  -25
----------------
[1] 24 10 18  8
----------------
[1]  3.536518  3.566355  3.045945 -1.968882
----------------
[1] 24 10 18  8

\[\left\{\begin{eqnarray} 5x&+&8y& &&-&4s&=&12 \\ & &2y&+&3z&+&7s&=&-10 \\ 3x& &&+&8z&-&3s&=&8 \\ 5x&+&5y&+&8z& &&=&18 \\ \end{eqnarray}\right.\]

a <- matrix(data = rbind(c(5,8,0,-4),c(0,2,3,7),c(3,0,8,-3),c(5,5,8,0)),nrow = 4,ncol = 4)
b <- c(12,-10,8,18) # または b <- matrix(data = c(12,-10,8,18),nrow = 4,ncol = 1)
result <- solve(a,b)
a;cat('----------------\n')
b;cat('----------------\n')
result;cat('----------------\n')
sapply(1:4,function(x)sum(result*a[x,]))
     [,1] [,2] [,3] [,4]
[1,]    5    8    0   -4
[2,]    0    2    3    7
[3,]    3    0    8   -3
[4,]    5    5    8    0
----------------
[1]  12 -10   8  18
----------------
[1]  39.620112 -18.983240 -10.648045   8.558659
----------------
[1]  12 -10   8  18