Built with Tutify.io

    AIStudyLabs

    R solve() Equation with Examples

    R solve() Equation with Examples

    R solve() is a generic function that solves the linear algebraic equation a %*% x = b for x, where b can be either a vector or a matrix. For example 10 * x = 20, in this equation, 10 is the coefficient; 20 is a constant and solve() calculates x which is 2.

    1. Quick Examples of solve() Function in R

    Following are quick examples of solve() function that solves the different equations.

    
    # calculate x using solve()
    solve(10, 20)
    solve(3, 6)
    solve(4, 20)
    
    # with two variables
    a <- matrix(c(3,1,4,1),nrow=2,ncol=2)
    b <- matrix(c(10,4),nrow=2,ncol=1)
    print(a)
    print(b)
    res <- solve(a,b)
    print(res)
    
    # with matrix
    a <- c(2, 1) 
    b <- c(5, 3) 
    xyz <- rbind(a, b) 
    print(xyz)
    solve(xyz)
    
    # with 4x4 matrix
    a <- c(2, 1, 3, 3) 
    b <- c(5, 3, 5, 4) 
    c <- c(6, 5, 9, 6)
    d <- c(1, 3, 2, 2)
    xyz <- rbind(a, b, c, d) 
    print(xyz)
    res <- solve(a=xyz) 
    print(res)
    

    <a id="h-2-solve-syntax"></a>

    2. solve() Syntax

    Below is the syntax of the solve() equation function.

    
    # Syntax of solve()
    solve(a, b, …)
    

    The following are the parameters.

    3. R solve() Equation Example

    By using solve() function in R you can solve algebraic equations like a %x% = b. The solve() function takes arguments a and b as arguments and calculates x. Also, use this function to calculate complex equations like finding the value for x and y. Let's see the first example which finds the value of x.

    
    # One variable example
    # 10x = 20
    # What is the x?
    solve(10, 20)
    
    # Output
    # [1] 2
    

    Below are some more examples.

    4. R solve() with Matrix

    solve() also takes a matrix as an argument for param a. Let's create two vectors using combine function c(). and using rbind() function let's bind these into a matrix. The below example demonstrates finding values for two variables x and y.

    
    # Two variable example
    # 3x + 4y = 10
    # x + y = 4
    # What is x and y?
    
    # Example
    a <- matrix(c(3,1,4,1),nrow=2,ncol=2)
    b <- matrix(c(10,4),nrow=2,ncol=1)
    print(a)
    print(b)
    res <- solve(a,b)
    print(res)
    
    # x value is -6
    # y value is 4
    

    Yields below output. The row and column names of the equation result are taken from the column names of a and of b respectively. If b is missing the column names of the result are the row names of a.

    5. Using Just Param a and default b

    Let's pass the one param a to the R solve() function and leave the b as default.

    
    # solve() with matrix
    a <- c(2, 1) 
    b <- c(5, 3) 
    xyz <- rbind(a, b) 
    print(xyz)
    solve(xyz)
    

    Yields below output.

    Let's check another example with 4 x 4 matrix.

    
    # with 4x4 matrix
    a <- c(2, 1, 3, 3) 
    b <- c(5, 3, 5, 4) 
    c <- c(6, 5, 9, 6)
    d <- c(1, 3, 2, 2)
    xyz <- rbind(a, b, c, d) 
    print(xyz)
    res <- solve(b=xyz) 
    print(res)
    

    Yields below output.

    6. Conclusion

    In this article, you have learned R solve() is a generic function that solves the linear algebraic equation a %*% x = b for x, For example 10 * x = 20, in this equation, 10 is the coefficient; 20 is a constant and solve() calculates x which is 2.

    Related Articles

    References