Home:ALL Converter>Generalised Matrices (to solve system of linear equations) in R

Generalised Matrices (to solve system of linear equations) in R

Ask Time:2013-02-06T20:01:20         Author:Tanya Kiddle

Json Formatter

I need to solve a system of linear equations in R - which I have been able to do just fine. Please see code below:

A<-matrix(c(1:5,2,1,2:4,3,2,1:3,4:2,1,2,5:1),nrow=5) #Creates a matrix of the coefficients
A #Displays the matrix of coefficients (below)

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    2    1    2    3    4
[3,]    3    2    1    2    3
[4,]    4    3    2    1    2
[5,]    5    4    3    2    1

kv<-matrix(c(7,-1,-3,5,17),nrow=5) #Creates a column vector of the known values

kv #Displays the column vector

     [,1]
[1,]    7
[2,]   -1
[3,]   -3
[4,]    5
[5,]   17

solve(A,kv) #Solves the continuous equation

     [,1]
[1,]   -2
[2,]    3
[3,]    5
[4,]    2
[5,]   -4

The problem is I now need to generalise my solution to it can be used on systems of equations of the same structure but of a larger size - WITHOUT keying in all the values of matrix A as I have above.

Is anyone able to point me in the correct direction of how I can do the matrix of coefficients but in a way that the program can be used to solve other systems?

Any help would be gratefully received Thanks

Author:Tanya Kiddle,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14728602/generalised-matrices-to-solve-system-of-linear-equations-in-r
yy