How to solve the Liner Equation using Python

 

Code Example


# How to solve the Liner Equation using Python


Example 1: [with Multiple variable]

import numpy as np

'''
    8x+3y−2z=9
    −4x+7y+5z=15
    3x+4y−12z=35
'''

A = np.array([[8, 3, -2], [-4, 7, 5], [3, 4, -12]])
b = np.array([9, 15, 35])
answer = np.linalg.solve(A, b)
print(answer)

 


Example 2: [only for single variable]


def solve_linear(equation,var='x'):
    expression = equation.replace("=","-(") + ")"
    nn = expression.replace(var,'1j')
    grouped = eval(nn)
    print("grouped : ", grouped)
    return -grouped.real/grouped.imag


res = solve_linear("x-2x-46=x-7x")
print(round(res*10, 2))


Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python