Solve System of Equations in R
Last Updated :
23 Jul, 2025
In this article, we will discuss how to solve a system of equations in R Programming Language.
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.
Syntax: solve(a, b)
Parameters:
- a: coefficients of the equation
- b: vector or matrix of the equation
Example 1: Solving system equation of three equations
Given Equations:
x + 2y + 3z = 20
2x + 2y + 3z = 100
3x + 2y + 8z = 200
Matrix A and B for solution using coefficient of equations:
A->
1 2 3
2 2 3
3 2 8
B->
20
100
200
To solve this using two matrices in R we use the following code:
R
# create matrix A and B using given equations
A <- rbind(c(1, 2, 3),
c(2, 2, 3),
c(3, 2, 8))
B <- c(20, 100, 200)
# Solve them using solve function in R
solve(A, B)
Output:
80 -36 3.99999999999999
which means x=80, y=-36 and z=4 is the solution for linear equations.
Example 2: Solving system equation of three equations
To get solutions in form of fractions, we use library MASS in R Language and wrap solve function in fractions.
Given Equations:
19x + 32y + 31z = 1110
22x + 28y + 13z = 1406
31x + 12y + 81z = 3040
Matrix A and B for solution using coefficient of equations:
A->
19 32 31
22 28 13
31 12 81
B->
1110
1406
3040
To solve this using two matrices in R we use the following code:
R
# Load package MASS
library(MASS)
# create matrix A and B using given equations
A <- rbind(c(19, 32, 31),
c(22, 28, 31),
c(31, 12, 81))
B <- c(1110, 1406, 3040)
# Solve them using solve
# function wrapped in fractions
fractions(solve(A, B))
Output:
[1] 159950/2243 -92039/4486 29784/2243
which means x=159950/2243 , y=-92039/4486 and z=29784/2243 is the solution for the above given linear equation.
Example 3: Solving Inverse matrix
R
# create matrix A and B using given equations
A <- matrix(c(4, 7, 3, 6), ncol = 2)
print(A)
print("Inverse matrix")
# Solve them using solve function in R
print(solve(A))
Output:
[,1] [,2]
[1,] 4 3
[2,] 7 6
[1] "Inverse matrix"
[,1] [,2]
[1,] 2.000000 -1.000000
[2,] -2.333333 1.333333
Similar Reads
How to Unnest dataframe in R ? In this article, we will discuss how to unnest dataframes in R Programming Language. Unnesting of dataframe refers to flattening it. Method 1: Using do.call approach The do.call() method in base R constructs and executes a function call from a function using its corresponding argument list. Syntax
3 min read
How to extract the dataframe row with min or max values in R ? The tabular arrangement of rows and columns to form a data frame in R Programming Language supports many ways to access and modify the data. Application of queries and aggregate functions, like min, max and count can easily be made over the data frame cell values. Therefore, it is relatively very ea
5 min read
How to find common elements from multiple vectors in R ? In this article, we will discuss how to find the common elements from multiple vectors in R Programming Language. To do this intersect() method is used. It is used to return the common elements from two objects. Syntax: intersect(vector1,vector2) where, vector is the input data. If there are more th
2 min read
Convert Character Matrix to Numeric Matrix in R In this article, we are going to see how to convert a given character matrix to numeric in R Programming Language. Converting the Character Matrix to Numeric Matrix we will use as.numeric() & matrix() Functions. Functions Usedas.numeric() function: This function is used to convert a given column
3 min read
Combine two DataFrames in R with different columns In this article, we will discuss how to combine two dataframes with different columns in R Programming Language. Method 1 : Using plyr package The "plyr" package in R is used to work with data, including its enhancements and manipulations. It can be loaded and installed into the working space by the
5 min read
Create Two 2x3 Matrix and Add, Subtract, Multiply and Divide the Matrixes in R In this article, we will create two matrices with 2 rows and 3 columns each, then add, subtract, multiply and divide these two matrices in R Programming language. We can create rows and columns in a matrix by using nrow and ncol parameters, nrow specifies the number of rows and ncol specifies the nu
2 min read