Solving Linear Equations -

Python for Numerical Methods


Solving Linear Equations with Matrices

Solutions with Dot Product of the Inverse of Matrix A, and Matrix B

First Example. We will perform two sample problems. The first is outlined below. Suggested reading is Numerical Methods for Engineers, Fifth Edition by Chapra and Canale.

import numpy as np
#Solve the following set of three linear equations
# 4x + 3y + 2z = 25
# -2x + 2y + 3z = -10
# 3x - 5y + 2z = -4

A = np.array([[4, 3, 2], [-2, 2, 3], [3, -5, 2]])
B = np.array([25, -10, -4])
X = np.linalg.inv(A).dot(B)
print(X)
#Dot product of the inverse of matrix A, and matrix B.
#Output [ 5. 3. -2.]
# x, y, z equals respectively 5, 3, -2



Second Example. Solve the set of five linear equations below. Note that we must display the matrix in the proper form for the numpy algorithm. The Output is shown in the comments.

# Solve the following five linear equations
# 6v - x = 50
# -3v + 3w = 0
# -w + 9x = 160
# -w - 8x + 11y -2z = 0
# -3v - w + 4z = 0

#The 5 equations can be displayed as:
# 6v + 0w - 1x + 0y + 0z = 50
# -3v + 3w + 0x + 0y + 0z = 0
# 0v - 1w + 9x + 0y + 0z = 160
# 0v - 1w - 8x + 11y - 2z = 0
# -3v - 1w + 0x + 0y + 4z = 0

import numpy as np
A=np.array([[6,0,-1,0,0],[-3,3,0,0,0],[0,-1,9,0,0],[0,-1,-8,11,-2],[-3,-1,0,0,4]])
B=np.array([50,0,160,0,0])
# Dot product of the inverse of matrix A, and matrix B
X=np.linalg.inv(A).dot(B)
print(X)
#Output [11.50943396 11.50943396 19.05660377 16.99828473 11.50943396]






This website consists of example problems from numerical methods for engineers. The first examples apply to roots, plotting roots, maximums, mininums, and optimization problems. You have enough examples so that you become familiar with the syntax used in Python. The examples have been tested and the output of the programs are listed in the comments for each. All programs were run on Python using the PyCharm Community interface. They were run on an older laptop with 8GB of RAM. If you have never used Python, I recommend the manual Introduction to Engineering Python by Steve Larsen. It is availble on Amazon.











Introduction to Engineering Python: For First Year Engineering Students