Transcription of Solving Linear Equations with Python
1 Solving Linear Equations with PythonHans-Petter Textbook with lots of Practical ExamplesAdditional Python Linear Equations NumPy Least Square MethodContents Python can be used to solve a large amount of Linear Equations using built-in functions Typically, you will use the NumPylibraryLinear Equations in Python The Python Standard Library consists basic Math functions, for more advanced Math functions, you typically want to use the NumPy Library If you don t have Python yet and want the simplest way to get started, you can use the Anaconda Distribution -it includes Python , NumPy, and other commonly used packages for scientific computing and data science.
2 Or use pip install numpy Equations = = !" Solution:(assuming !"is possible ) !! !+ !" "+ !# #+ = ! "! !+ "! "+ "# #+ = " Given the following Linear Equations :These Equations can be set on the following general form:Where A is a matrix, x is a vector with the unknowns and b is a vector of constants = "" "# $" $# = " % $ = " % $Example "+2 %=53 "+4 %=6 Given the following Linear Equations : "=5 2 %3(5 2 %)+4 %=615 6 %+4 %=615 2 %=615 6=2 %2 %=9 %=92= "=5 9= 4 "= 4 %= 's solve these Equations manuallyFinal solution:From eq(1) we get:We put this in eq(2):This gives:ExampleGiven the following Linear Equations : '+2 (=53 '+4 (=6 This gives.))
3 = We want to put the Equations on the following general form: =1234 =56 = ! " = $! The solution is given by:Example -PythonPython code:import numpyas npimport laA = ([[1, 2], [3, 4]])b = ([[5], [6]])Ainv= (A)x = (b)print(x)This gives the following solution:[[-4. ][ ]]This means: "= 4 %= is the same as the solution we got from our manual calculationsNote! The A matrix must be square and of full-rank, the inverse matrix needs to Python (Alt2) Python code:import numpyas npA = ([[1, 2], [3, 4]])b = ([[5], [6]])x = (A, b)print(x)This gives the following solution:[[-4. ][ ]]This means: "= 4 %= is the same as the solutions we got from the other methodsx = (A, b)We can also use the ()functionNote!
4 The A matrix must be square and of full-rank, the inverse matrix needs to ExampleGiven the following Linear Equations : "+2 %=53 "+4 %=67 "+8 %=9 This gives: = We want to put the Equations on the following general form: =123478 =569 = " %Note! The A matrix is not square, the inverse matrix does not to exists!We have 3 Equations and 2 unknows ( ", %)Non-Quadratic ExampleGiven the following Linear Equations : "+2 %=53 "+4 %=67 "+8 %=9import numpyas npA = ([[1, 2], [3, 4],[7, 8]])b = ([[5],[6],[9]])x = (A, b)print(x)The Python code examples gives the following error: LinAlgError: Last 2 dimensions of the array must be squareNon-Quadratic Example -PythonThe previous Python code examples gives the following error: LinAlgError: Last 2 dimensions of the array must be squareThis is because the A matrix is not square, the inverse matrix does not to exists!
5 In many cases we cannot find the inverse matrix, , when the matrix is not quadratic. Finding the inverse matrix for large matrices is also has different functions that can handle Example Least SquarePython code:import numpyas npA = ([[1, 2], [3, 4],[7, 8]])b = ([[5],[6],[9]])x = (A, b, rcond=None)[0]print(x)The results becomes:[[ ][ ]]Least Square Method = !"= # $% # Given:The Least Square Method is given by:The Least Square Method works for Non-Quadratic matrices as well. The Least Square fitData PointsLeast Square Method -Pythonimport numpyas npA = ([[1, 2], [3, 4],[7, 8]])b = ([[5],[6],[9]])x = (A, b, rcond=None)[0]print(x)x_ls= ( () * (A)) * () * bprint(x_ls)Implementing Least Square Method from scratch:Compare built-in LSM and LMS from scratchComparing Different MethodsGiven the following:import numpyas npA = ([[4, 3, 2], [-2, 2, 3],[3, -5, 2]])b = ([[25],[-10],[-4]])x_alt1 = (A).
6 Dot(b)print(x_alt1)x_alt2 = (A, b)print(x_alt2)x_alt3 = (A, b, rcond=None)[0]print(x_alt3)x_alt4 = ( () * (A)) * () * bprint(x_alt4)All 4 methods gives:[[ 5.][ 3.][-2.]]Meaning: =5, =3, = 24 +3 +2 =25 2 +2 +3 = 103 5 +2 = 4 Additional Python HalvorsenUniversity of South-Eastern.