You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 59 Next »

Here, we will aim to provide you with a number of exercises and solutions with a varying degree of difficulty. There will also occur exercises that requires you to use functions that has not yet been described in Matrices and linear algebra.

If you have a suggestion on a topic you would like to see some more exercises (or tutorials) on, please send us a mail or leave a post on our Forum. We are more than happy to recieve feedback.

Page content

Determinant

Exercise 1: Simple matrix

Compute the determinant

Solution: Simple matrix
import numpy as np 

# Defining the matrix
A = np.array([[1, 3, 2],
            [4, 1, 3],
            [2, 5, 2]])

det = np.linalg.det(A)
print(det)


# Output:
16.999999999999993   # e.i. det(A) = 17

Exercise 2: Complex numbers

This example is taken from an example exam in TMA4110 - Calculus 3, check out the exam and the solution for hand calculations (only in norwegain). This is task 4.

Comput the determinant

Solution: Complex Numbers
import numpy as np 

# Defining the matrix
# Notice how we can define complex numbers in python by adding the letter "j"
# "j" is used to denote an imaginary unit instead of "i", because Python follows engineering,
# where "i" denotes the electric current as a function of time (especially electrial engineering)
A = np.array([[3, 1-1j, 1j, 4],
            [3, 1, 1-2j, 4+7j],
            [6j, 2+2j, -2, 3j],
            [-3, -1+1j, 1, 3-4j]], dtype=complex)

det = np.linalg.det(A)
print(det)


# Output:
(-15-15j)   # e.i. det(A) = -15-15i

Eigenvalues and eigenvectors

Exercise 1

Find the eigenvalues and corresponding eigenvectors to matrix A, where


Solution

Exercise 2

Find the eigenvalues and corresponding eigenvectors to the following matrix:


Solution

Exercise 3

Find the eigenvalues and corresponding eigenvectors to the following matrix:


Solution

Inverse

Exercise 1

Find the inverse of the matrix


Solution
import numpy as np 

A = np.array([[2, 2, 0],
            [0, 0, 1],
            [4, 2, 0]])
inv_A = np.linalg.inv(A)
print(inv_A)


# Output:
[[-0.5  0.   0.5]
 [ 1.   0.  -0.5]
 [ 0.   1.   0. ]]

Least squares solution to a linear system

Exercise 1

This example is taken from an example exam in TMA4110 - Calculus 3, check out the exam and the solution for hand calculations (only in norwegain). This is task 5.

Use the least squares method to find an approximation to the linear system

First, lets write the equations as matrices on the format , where

To solve the problem in NumPy, we will use the function numpy.linalg.lstsq (or with SciPys scipy.linalg.lstsq) which is currently not described in Matrices and linear algebra. We recommend you to read the function documentation before proceeding.

Solution
import numpy as np 

# Defining the matrices
A = np.array([[1, 2],
            [3, 4],
            [5, 6]])
b = np.array([[1],[2],[3]])

x, residuals, rank, s = np.linalg.lstsq(A, b, rcond=None)   
# "rcond=None" sets new default for rcond, see function documentation

print(x)    # "x" is the solution


# Output:
[[-5.97106181e-17]
 [ 5.00000000e-01]]

We can interpret this such as the least squares solution is

Solving a system of linear equations

Exercise 1

Solve the following system of linear equations:

Solution
import numpy as np 

A = np.array([[2, -4, 9],       # Right-hand side
            [4, -3, 8],
            [-2, 4, -2]])
b = np.array([-38, -26, 17])    # Left-hand side

solution = np.linalg.solve(A, b)
print(solution)


# Output:
[ 2.5  4.  -3. ]

This is interpreted asand

Exercise 2

Solve the following system of linear equations:

Solution
import numpy as np 

A = np.array([[1, 3, 6],       	# Right-hand side
            [2, 8, 16],
            [2, 6, 12]])
b = np.array([4, 8, 8])     	# Left-hand side

solution = np.linalg.solve(A, b)

print(solution)


# Output:
numpy.linalg.LinAlgError: Singular matrix

As we can see, using np.linalg.solve returns a LinAlgError telling us that the matrix (in this case A) is a singular matrix. A singular matrix is one that is not invertible. This means that the system of equations we are trying to solve does not have a unique solution (either none or multiple); np.linalg.solve can't handle this.

By using np.linalg.lstsq (least squares solution) instead, we will at least get one solution.

Solution using least squares method
import numpy as np 

A = np.array([[1, 3, 6],       	# Right-hand side
            [2, 8, 16],
            [2, 6, 12]])
b = np.array([4, 8, 8])     	# Left-hand side

x, residuals, rank, s = np.linalg.lstsq(A, b, rcond=None) 
# "rcond=None" sets new default for rcond, see function documentation

print(x)    # "x" is the solution


# Output:
[4. 0. 0.]

This is interpreted asand

Keep in mind that this is only one of multiple solutions!



  • No labels