Suppose I have the following equations:
x + 2y + 3z = 20 2x + 5y + 9z = 100 5x + 7y + 8z = 200
How do I solve these equations for x
, y
and z
? I would like to solve these equations, if possible, using R or any other computer tools.
solve() function in R Language is used to solve the equation. Here equation is like a*x = b, where b is a vector or matrix and x is a variable whose value is going to be calculated. Parameters: a: coefficients of the equation.
If you have two different equations with the same two unknowns in each, you can solve for both unknowns. There are three common methods for solving: addition/subtraction, substitution, and graphing. This method is also known as the elimination method.
This should work
A <- matrix(data=c(1, 2, 3, 2, 5, 9, 5, 7, 8), nrow=3, ncol=3, byrow=TRUE) b <- matrix(data=c(20, 100, 200), nrow=3, ncol=1, byrow=FALSE) round(solve(A, b), 3) [,1] [1,] 320 [2,] -360 [3,] 140
For clarity, I modified the way the matrices were constructed in the previous answer.
a <- rbind(c(1, 2, 3), c(2, 5, 9), c(5, 7, 8)) b <- c(20, 100, 200) solve(a, b)
In case we need to display fractions:
library(MASS) fractions(solve(a, b))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With