Hướng dẫn how do you find the solution of a linear equation in python? - làm thế nào để bạn tìm thấy nghiệm của một phương trình tuyến tính trong python?
Hệ thống phương trình tuyến tính có thể được giải quyết với các mảng và numpy. Một hệ thống phương trình tuyến tính được hiển thị bên dưới: $$ 3x + 4Y - 12Z = 35 $$ Chức năng Các bước để giải hệ phương trình tuyến tính với Các mảng kết quả có ba mục. Một mục cho mỗi biến. In [1]: A = np.array([[8, 3, -2], [-4, 7, 5], [3, 4, -12]])
b = np.array([9, 15, 35])
x = np.linalg.solve(A, b)
x
Out[1]: array([-0.58226371, 3.22870478, -1.98599767])Chúng ta có thể cắm giá trị của X, Y và Z trở lại vào một trong các phương trình để kiểm tra câu trả lời.x, y and z back into one of the equations to check the answer. X là mục đầu tiên của mảng, y là mục thứ hai của mảng và Z là mục thứ ba của mảng. is the first entry of the array, y is the second entry of the array, and z is the third entry of the array. x = y = array([-0.58226371, 3.22870478, -1.98599767])0 z = array([-0.58226371, 3.22870478, -1.98599767])1 Khi các giá trị này được cắm vào phương trình từ trên: Câu trả lời phải là array([-0.58226371, 3.22870478, -1.98599767])2. Điều kiện tiên quyết: Sympy.Solve ()Sympy.solve() Trong bài viết này, chúng tôi sẽ thảo luận về cách giải một phương trình tuyến tính có nhiều hơn một biến. Ví dụ, giả sử chúng ta có hai biến trong các phương trình. Phương trình như sau:
Khi chúng ta giải phương trình này, chúng ta sẽ nhận được x = 1, y = 0 là một trong các giải pháp. Trong Python, chúng tôi sử dụng phương thức Eq () để tạo một phương trình từ biểu thức. & Nbsp; & nbsp;
Ví dụ: nếu chúng ta có biểu thức là x+y = 1. nó có thể được viết là eq (x+y, 1) Giải phương trình với hai biếnXây dựng các phương trình bằng phương pháp Eq (). Để giải các phương trình truyền chúng dưới dạng tham số cho hàm giải quyết ().solve() function. Ví dụ: & nbsp; Python3array([-0.58226371, 3.22870478, -1.98599767])3 array([-0.58226371, 3.22870478, -1.98599767])4 array([-0.58226371, 3.22870478, -1.98599767])5 array([-0.58226371, 3.22870478, -1.98599767])6 array([-0.58226371, 3.22870478, -1.98599767])7 array([-0.58226371, 3.22870478, -1.98599767])8 array([-0.58226371, 3.22870478, -1.98599767])9 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}0 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}2 array([-0.58226371, 3.22870478, -1.98599767])8 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}4 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}5 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}6 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}7 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}0 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}4 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}5 array([-0.58226371, 3.22870478, -1.98599767])8 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}4 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}8 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}6 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}7 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}0 np.linalg.solve() 4Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 np.linalg.solve() 7Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}0 np.linalg.solve() 0Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 np.linalg.solve() 3Output: Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0} Giải phương trình với ba biếnXây dựng các phương trình sau bằng phương trình Eq () và giải quyết sau đó để tìm các biến chưa biết.
Example: Python3array([-0.58226371, 3.22870478, -1.98599767])3 array([-0.58226371, 3.22870478, -1.98599767])4 array([-0.58226371, 3.22870478, -1.98599767])5 array([-0.58226371, 3.22870478, -1.98599767])6 array([-0.58226371, 3.22870478, -1.98599767])7 array([-0.58226371, 3.22870478, -1.98599767])8 array([-0.58226371, 3.22870478, -1.98599767])9 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}0 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}2 array([-0.58226371, 3.22870478, -1.98599767])8 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}4 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}5 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}6 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}7 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}0 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}4 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}5 array([-0.58226371, 3.22870478, -1.98599767])8 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}4 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}8 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}6 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}7 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}0 np.linalg.solve() 4Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 np.linalg.solve() 7Giải phương trình với ba biến Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}0 array([-0.58226371, 3.22870478, -1.98599767])11 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3}0 array([-0.58226371, 3.22870478, -1.98599767])15 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}1 Equation 1: Eq(x + y, 1) Equation 2 Eq(x - y, 1) Values of 2 unknown variable are as follows: {x: 1, y: 0}9 array([-0.58226371, 3.22870478, -1.98599767])18 Output: Equation 1: Eq(x + y + z, 1) Equation 2 Eq(x - y + 2*z, 1) Equation 3 Values of 3 unknown variable are as follows: {x: 0, y: 1/3, z: 2/3} Làm thế nào để bạn giải một phương trình tuyến tính trong Python?Theo hiểu biết của tôi, đây là phương pháp hiệu quả nhất để giải một phương trình tuyến tính trong Python ... def solve_linear (phương trình, var = 'x'): expression = phương trình.replace ("=", "-(")+")" .... Biểu thức = phương trình.replace ("=", " - (")+")". nhóm = eval (expression.replace (var, '1j'). trả về -grouped.real/nhóm.IMAG .. Giải quyết () làm gì trong Python?giải quyết () phương thức.Với sự trợ giúp của Sympy.Giải quyết phương pháp (biểu thức), chúng ta có thể giải quyết các phương trình toán học một cách dễ dàng và nó sẽ trả về rễ của phương trình được cung cấp làm tham số bằng cách sử dụng sympy.solve the mathematical equations easily and it will return the roots of the equation that is provided as parameter using sympy.
Làm thế nào để bạn tìm thấy giải pháp cho một phương trình tuyến tính?Làm thế nào để tôi giải các hệ phương trình tuyến tính bằng cách thay thế ?.. Cô lập một trong hai biến trong một trong các phương trình .. Thay thế biểu thức bằng biến bị cô lập từ bước 1 sang phương trình khác..... Giải phương trình tuyến tính cho biến còn lại .. Bạn có thể sử dụng Python để giải phương trình không?Sympy là một gói cho các giải pháp tượng trưng trong Python có thể được sử dụng để giải các hệ phương trình.Cách tiếp cận tương tự áp dụng cho các phương trình tuyến tính hoặc phi tuyến.. The same approach applies to linear or nonlinear equations. |