2019-06-10

Solving a system of linear equations in R

Problem

We want to solve a system of linear equations in R.

2x + 3y + 3z = 20
  x + 4y + 3z = 15
5x + 3y + 4z = 30

Solution

We use the function solve.

a <- rbind(c(2, 3, 3), 
           c(1, 4, 3), 
           c(5, 3, 4))
b <- c(20, 15, 30)
solve(a, b)
[1] -1.25 -6.25 13.75
If we'd like the resulst in fractions, we use the function fractions from the package MASS.

library(MASS)
fractions(solve(a, b))
[1]  -5/4 -25/4  55/4

Related posts

Spanish version

No hay comentarios:

Publicar un comentario

Nube de datos