Linear algebra applied to Linear Systems

In this module, you will learn how you can apply the concepts learned so far to solve for a system of linear equations.

Today's learning objectives

  • Understand how to solve for a given system of equations using linear algebra.

Solving Linear Systems of equations

In mathematics, an example of a simple linear equation is

ax + b = 0

where x is unknown, a are termed coefficients of the equation (or slope), and b is termed as a constant (or an intercept). A simple solution to such an equation is given by

x = -b / a

when a is not equal to zero. However, note that there is only one variable in this equation i.e., x.

A linear system of two or more variables (and hence equations) is termed as a system of linear equations or linear systems. Please watch the video below to refresh your knowledge on systems of linear equations.

What you observed in the video is solving for a linear system of equations graphically. However, while this is possible in the case of two variables, you can imagine that is can become very complex as you increase the number of variables. Here's where linear algebra comes to the rescue!

We now know enough linear algebra to represent a linear system of equations in matrix form. For example, a system of equations given below:

a1x1 + a2x2 = b1
a3x1 + a4x2 = b2

can be represented in matrix form as:

Ax = b

Where (in numpy notation):

A = [[a1, a2], [a3, a4]]
x = [[x1, x2]]
b = [[b1, b2]]

Note: what is the matrix(dot) product of A and x?

Linear algebra provides powerful tools using matrix inversion in order to solve such systems of linear equations even in very high dimensions.

In general, the solution to the system of equations introduced above is given by:

x = A-1b

Note: what does the -1 symbol represent?

Similar to matrix inversion, solving systems of linear equations is best left to a computer! In Python, the function linalg.solve which is provided by NumPy is used to solve for a system of linear equations. In today's blended learning workshop using CodeAcademy, you will gain some hands-on experience in using this function.

Assignment

  • Solve the given system of linear equations using the Python function linalg.solve.

  1. x1 + 2x2 = 1
    3x1 + 5x2 = 2


  2. 2x1 + x2 = 5
    x1 + x2 = 2


  3. 4x1 + 3x2 = 20
    9x2 - 5x1 = 26
    4.
    4x1 + 1x2 + 2x3 = 25
    2x1 - 2x2 + 3x3 = -10
    3x1 - 5x2 + 2x3 = -4

hint: after you have solved for x, verify your solution using np.allclose(np.dot(a, x), b)

  • Jane runs a cafe in Oslo and sold 20 croissants and 10 cappucinos in one day for a total of 350 euros. The next day she sold 17 croissants and 22 cappucinos for 500 euros. If the prices of a croissant and cappucino remained the same on both the days, what was the price of one croissant and one cappucino?

hint: write down the equations before converting them to linear algebra notation.