Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/notebooks/Chapter 1 - Linear Equation System.ipynb
Views: 449
We begin by exploring the fundamentals of plotting in Python, while simultaneously revisiting the concept of systems of linear equations.
Visualisation of A System of Two Linear Equations
Consider a linear system of two equations: Easy to solve: . Let's plot the linear system.
How to Draw a Plane
Before drawing a plane, let's refresh the logic of Matplotlib 3D plotting. This should be familiar to you if you are a MATLAB user.
First, create meshgrids.
Mathematically, meshgrids are the coordinates of Cartesian products. To illustrate, we can plot all the coordinates of these meshgrids
Try a more complicated meshgrid.
Now consider the function , is in the dimension. Though Matplotlib is not meant for delicate plotting of 3D graphics, basic 3D plotting is still acceptable.
For example, we define a simple plane as Then plot
Or we can plot it as a surface, Matplotlib will automatically interpolate values among the Cartesian coordinates such that the graph will look like a surface.
Visualisation of A System of Three Linear Equations
We have reviewed on plotting planes, now we are ready to plot several planes all together.
Consider this system of linear equations And solution is . Let's reproduce the system visually.
We are certain there is a solution, however the graph does not show the intersection of planes. The problem originates from Matplotlib's rendering algorithm, which is not designed for drawing genuine 3D graphics. It merely projects 3D objects onto 2D dimension to imitate 3D features.
Visualisation of A System With Infinite Numbers of Solutions
Our system of equations is given
Rearrange to solve for
The solution of the system is , where is a free variable.
The solution is an infinite line in , to visualise the solution requires setting a range of and , for instance we can set
which means
We can pick one inequality to set the range of , e.g. second inequality: .
Then plot the planes and the solutions together.
Reduced Row Echelon Form
For easy demonstration, we will be using SymPy frequently in lectures. SymPy is a very power symbolic computation library, we will see its basic features as the lectures move forward.
We define a SymPy matrix:
Think of it as an augmented matrix which combines coefficients of linear system. With row operations, we can solve the system quickly. Let's turn it into a row reduced echelon form.
Take out the first element in the big parentheses, i.e. the rref matrix.
If you don't like fractions, convert it into float type.
The last column of the rref matrix is the solution of the system.
Example: rref and Visualisation
Let's use .rref()
method to compute a solution of a system then visualise it. Consider the system:
Extract the augmented matrix into a SymPy matrix:
In case you are wondering what's : they are the indices of pivot columns, in the augmented matrix above, the pivot columns reside on the -th and -nd column.
Given that the matrix is not of full rank, the solutions are expressed in a general form: To illustrate, let's select three distinct values for , specifically , and compute three unique solutions:
We can visualise the general solution, and the 3 specific solutions altogether.
Example: A Symbolic Solution
Consider a system where all right-hand side values are indeterminate:
We define as SymPy objects, then extract the augmented matrix
We can immediately achieve the symbolic solution by using .rref()
method.
Of course, we can substitute values of , and to get a specific solution.
Example: Polynomials
To determine a cubic polynomial that intersects the points , , , and , we consider the general form of a cubic polynomial:
Substituting each point into this equation yields:
It turns to be a linear system, the rest should be familiar already.
The augmented matrix is
The last column is the solution, i.e. the coefficients of the cubic polynomial.
Cubic polynomial form is:
Since we have the specific form of the cubic polynomial, we can plot it
Now you know the trick, try another 5 points: , , , , . And polynomial form is The augmented matrix is
Solving The System of Linear Equations By NumPy
Set up the system , generate a random and
Let's verify if
They are technically zeros, due to some round-off errors omitted, that's why there is in front .