Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving simultaneous equations with R

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.

like image 269
mlzboy Avatar asked Nov 16 '11 01:11

mlzboy


People also ask

How do you solve two equations in R?

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.

What are the 3 ways to solve simultaneous equations?

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.


2 Answers

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 
like image 139
MYaseen208 Avatar answered Sep 21 '22 15:09

MYaseen208


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)) 
like image 44
mpalanco Avatar answered Sep 17 '22 15:09

mpalanco