Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Solving the Task Using SLE | Linear Algebra
Mathematics for Data Analysis and Modeling

book
Challenge: Solving the Task Using SLE

Uppgift

Swipe to start coding

We have already considered how to solve the SLE using inversed matrix. But we can also use np.linalg.solve(A, y) method that calculates the solution of the SLE:
A * x = y.
Your task is to solve the system using both these methods and compare the results:

  1. Use np.linalg.solve() method.
  2. Use np.inv() method to calculate inversed matrix and provide solution using: x = A_inv @ y.

Lösning

import numpy as np

# Define the coefficient matrix A and the constant vector B
A = np.array([[2, 3, -1], [4, -1, 5], [10, 2, 6]])
y = np.array([10, 4, 1])

# Solve the SLE using `np.linalg.solve()`
x = np.linalg.solve(A, y)
print(f'Solution using np.linalg.solve(): {x}')

# Calculate the inverse of matrix A
A_inv = np.linalg.inv(A)

# Solve the SLE using the inverse matrix method
x_inv = A_inv @ y
print(f'Solution using inverse matrix method: {x_inv}')

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 8
import numpy as np

# Define the coefficient matrix A and the constant vector B
A = np.array([[2, 3, -1], [4, -1, 5], [10, 2, 6]])
y = np.array([10, 4, 1])

# Solve the SLE using `np.linalg.solve()`
x = np.linalg.___(___, ___)
print(f'Solution using np.linalg.solve(): {x}')

# Calculate the inverse of matrix A
A_inv = np.linalg.___(___)

# Solve the SLE using the inverse matrix method
x_inv = A_inv ___ y
print(f'Solution using inverse matrix method: {x_inv}')
toggle bottom row
some-alt