# Solve System of Equations in R (3 Examples) | Using solve() Function

In this article, I’ll explain how to **solve a system of equations using the solve() function** in the R programming language.

Table of contents:

Let’s get started.

## Example 1: Basic Application of solve() Function in R

In this Example, I’ll illustrate how to apply the solve function to a single equation in R.

Let’s assume we want to solve the equation: *3x = 12*. Then we can use the following R code:

solve(3, 12) # Applying solve # 4 |

solve(3, 12) # Applying solve # 4

The RStudio console returns the value 4, i.e. *x = 4*.

## Example 2: Applying solve Function to Complex System of Equations

The solve command can also be used to solve complex systems of equations. Let’s assume that our system of equations looks as follows:

*5x + y = 15
10x + 3y = 9*

Then we can specify these equations in a right-hand side matrix…

mat_a1 <- matrix(c(5, 10, # Creating left-hand side matrix 1, 3), nrow = 2) mat_a1 # Print matrix # [,1] [,2] # [1,] 5 1 # [2,] 10 3 |

mat_a1 <- matrix(c(5, 10, # Creating left-hand side matrix 1, 3), nrow = 2) mat_a1 # Print matrix # [,1] [,2] # [1,] 5 1 # [2,] 10 3

…and a left-hand side matrix:

mat_b1 <- matrix(c(15, # Creating right-hand side matrix 9), nrow = 2) mat_b1 # Print matrix # [,1] # [1,] 15 # [2,] 9 |

mat_b1 <- matrix(c(15, # Creating right-hand side matrix 9), nrow = 2) mat_b1 # Print matrix # [,1] # [1,] 15 # [2,] 9

Afterwards, we can apply the solve function to these matrices:

solve(mat_a1, mat_b1) # Applying solve to matrices # [,1] # [1,] 7.2 # [2,] -21.0 |

solve(mat_a1, mat_b1) # Applying solve to matrices # [,1] # [1,] 7.2 # [2,] -21.0

The previous output of the RStudio console shows our result: *x = 7.2; y = -21*.

## Example 3: Using Identity Matrix as Right-hand Side of Linear System

The solve function sets the right-hand side matrix to the identity matrix, in case this matrix is not explicitly specified. In other words, the solve function is computing the inverse of a matrix, if no right-hand side matrix is specified.

Let’s do this in practice: First, we have to create another example matrix in R:

set.seed(96743) # Creating complex matrix mat_a2 <- matrix(rnorm(25), nrow = 5) mat_a2 # Print matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1.063239047 -1.4326992 -0.9790201 -0.4636753 1.37990358 # [2,] 0.254985749 0.4016807 1.1733589 -0.7508775 2.33918171 # [3,] -0.338361009 -0.1833490 -0.5049254 0.7144516 -1.86724624 # [4,] -0.009719763 0.2847016 0.8611929 0.7430495 0.01254588 # [5,] 0.380698865 0.8433700 1.5883904 -1.7543261 -0.29077861 |

set.seed(96743) # Creating complex matrix mat_a2 <- matrix(rnorm(25), nrow = 5) mat_a2 # Print matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 1.063239047 -1.4326992 -0.9790201 -0.4636753 1.37990358 # [2,] 0.254985749 0.4016807 1.1733589 -0.7508775 2.33918171 # [3,] -0.338361009 -0.1833490 -0.5049254 0.7144516 -1.86724624 # [4,] -0.009719763 0.2847016 0.8611929 0.7430495 0.01254588 # [5,] 0.380698865 0.8433700 1.5883904 -1.7543261 -0.29077861

Now, we can solve this matrix (i.e. computing the inverse) by using the solve function as follows:

solve(mat_a2) # Applying solve to single matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 0.15755772 -4.1085093 -5.0897583 1.93645828 0.4642408 # [2,] -0.84696305 -3.9617415 -5.5935527 1.01982458 0.0735072 # [3,] 0.33461449 2.0787370 2.8230927 0.02703553 0.1829891 # [4,] -0.06024530 -0.9485616 -1.1905739 0.95065045 -0.2303102 # [5,] -0.05892059 0.2084526 -0.2829356 -0.09461151 -0.2289467 |

solve(mat_a2) # Applying solve to single matrix # [,1] [,2] [,3] [,4] [,5] # [1,] 0.15755772 -4.1085093 -5.0897583 1.93645828 0.4642408 # [2,] -0.84696305 -3.9617415 -5.5935527 1.01982458 0.0735072 # [3,] 0.33461449 2.0787370 2.8230927 0.02703553 0.1829891 # [4,] -0.06024530 -0.9485616 -1.1905739 0.95065045 -0.2303102 # [5,] -0.05892059 0.2084526 -0.2829356 -0.09461151 -0.2289467

The previous output shows the inverse of our input matrix.

## Video, Further Resources & Summary

Some time ago I have released a video on my YouTube channel, which shows the contents of this article. You can find the video below:

*The YouTube video will be added soon.*

Besides the video, you could read the related tutorials that I have published on my website. Some tutorials can be found here:

This tutorial illustrated how to **apply the solve() function** in R programming. Don’t hesitate to let me know in the comments, in case you have further questions.

**5**/

**5**(

**1**vote )

### Subscribe to my free statistics newsletter: