14.7: Solving Systems of Equations

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    14.7: Solving Systems of Equations

    The solve function can also solve systems of equations. This is another way to solve systems of linear equations, like the ones we solved using matrices and the backslash operator in the Linear Algebra chapter.

    Solving a Linear System

    Consider the following 3 by 3 system:

    \(4*x1 - 2*x2 + x3 = 7\)

    \(x1 + x2 + 5*x3 = 10\)

    \(-2*x1 + 3*x2 - x3 = 2\)

    We can solve this system symbolically using solve.

    syms x1 x2 x3
    expr1 = 4*x1 - 2*x2 + x3 == 7;
    expr2 = x1 + x2 + 5*x3 == 10;
    expr3 = -2*x1 + 3*x2 - x3 == 2;
    
    solution = solve(expr1, expr2, expr3)
    
    x1_solution = solution.x1
    x2_solution = solution.x2
    x3_solution = solution.x3
    
    numeric_solution = double([solution.x1 solution.x2 solution.x3])
    
    

    The result is returned as a structure. Each field of the structure stores the solution for one unknown variable. We use dot notation, such as solution.x1, to access each result:

    solution =
    
    struct with fields:
    
        x1: 124/41
        x2: 121/41
        x3: 33/41
    
    
    x1_solution =
    
        124/41
    
    
    x2_solution =
    
        121/41
    
    
    x3_solution =
    
        33/41
    
    
    numeric_solution =
    
        3.0244 2.9512 0.8049
    Note

    For purely numeric linear systems, solving Ax = b is usually faster and more appropriate. Symbolic solve is helpful when exact symbolic answers are needed or when the equations are not purely numeric.

    Solving a Nonlinear System

    Symbolic solve is not limited to linear equations. It can also solve many nonlinear systems.

    For example, consider the following system:

    \(x^2 + y^2 = 25\)

    \(y = x + 1\)

    syms x y
    eq1 = x^2 + y^2 == 25;
    eq2 = y == x + 1;
    sol = solve(eq1, eq2, x, y)
    
    x= double(sol.x)
    double(sol.y)
    
    

    This example finds the intersection points of a circle and a line. Since the line may cross the circle at more than one point, the solution can contain more than one value for x and y.

    sol =
    
    struct with fields:
    
        x: [2×1 sym]
        y: [2×1 sym]
    
    
    x =
    
        -4
        3
    
    
    y =
    
        -3
        4

    This page titled 14.7: Solving Systems of Equations was last modified on Tue, 30 Jun 2026 02:11:43 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.