1. Import Libraries
import numpy as np
from scipy.linalg import solve
numpy (np) → A library for handling arrays, matrices, and numerical operations.
scipy.linalg.solve → A function from SciPy’s linear algebra module that solves systems of linear equations of the form:
A⋅x=b
where:
A = coefficient matrix
b = constant terms (right-hand side vector)
x = unknown variables
2. Define the Coefficient Matrix
A = np.array([[3, 2], [1, 2]])
This creates a 2×2 matrix:
This matrix represents the coefficients of the variables in the system of equations.
3. Define the Constants (Right-Hand Side)
b = np.array([12, 8])
This creates a column vector:
It represents the values on the right-hand side of the equations.
4. Solve the System
print(solve(A, b))
solve(A, b) finds the solution
Here it means:
This corresponds to the system of equations:
3x+2y=12
x+2y=8
5. The Output
The program prints:
[4. 2.]
That means:
x=4,y=2
Final Answer (Solution of the system):
[4. 2.]
Download Book - 500 Days Python Coding Challenges with Explanation


0 Comments:
Post a Comment