Online Scipy Compiler

Solving linear equations with scipy

Python
# Problem Statement
# Let's say we have the following system of equations:

# (2x + 3y = 5)
# (4x - y = 3)
# We want to find the values of (x) and (y) that satisfy both equations.


import numpy as np
from scipy.linalg import solve

# Define the coefficients of the equations in the form of matrix A
A = np.array([[2, 3], [4, -1]])

# Define the constants of the equations in vector B
B = np.array([5, 3])

# Solve for x and y
solution = solve(A, B)

print(f"The solution is x = {solution[0]} and y = {solution[1]}")
Click Run or press shift + ENTER to run code.