Hướng dẫn writing equations in python - viết phương trình trong python

Giải phương trình

Hàm

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 của Sympy có thể được sử dụng để giải các phương trình và biểu thức có chứa các biến toán học tượng trưng.

Phương trình với một giải pháp

Một phương trình đơn giản chứa một biến như X-4-2 = 0 có thể được giải quyết bằng hàm

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 của Sympy. Khi chỉ có một giá trị là một phần của giải pháp, giải pháp ở dạng danh sách.x-4-2 = 0 can be solved using the SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function. When only one value is part of the solution, the solution is in the form of a list.x-4-2 = 0 can be solved using the SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function. When only one value is part of the solution, the solution is in the form of a list.

Phần mã bên dưới thể hiện hàm

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 của Sympy khi biểu thức được xác định với các biến toán học tượng trưng.

In [1]:

from sympy import symbols, solve

x = symbols('x') expr = x-4-2

sol = solve(expr)

sol

Để rút giá trị ra khỏi danh sách giải pháp
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

3, có thể sử dụng lập chỉ mục danh sách thông thường. Phần mã bên dưới thể hiện hàm giải quyết () của Sympy khi một phương trình được xác định với các biến toán học tượng trưng.

In [3]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

Phương trình với hai giải pháp

Phương trình bậc hai, như x^2 - 5x + 6 = 0, có hai giải pháp. Hàm

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 của Sympy có thể được sử dụng để giải một phương trình với hai giải pháp. Khi một phương trình có hai giải pháp, hàm
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 của Sympy sẽ xuất ra một danh sách. Các yếu tố trong danh sách là hai giải pháp.x^2 - 5x + 6 = 0, have two solutions. SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function can be used to solve an equation with two solutions. When an equation has two solutions, SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function outputs a list. The elements in the list are the two solutions.x^2 - 5x + 6 = 0, have two solutions. SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function can be used to solve an equation with two solutions. When an equation has two solutions, SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function outputs a list. The elements in the list are the two solutions.

Phần mã bên dưới cho thấy cách một phương trình với hai giải pháp được giải quyết với hàm

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 của Sympy.

In [4]:

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

Nếu bạn chỉ định đối số từ khóa
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

7 cho hàm
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 của Sympy, đầu ra vẫn là một danh sách, nhưng bên trong danh sách là một từ điển cho thấy biến nào đã được giải quyết.

In [5]:

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

5

Các phương trình Sympy được khởi tạo như một đối tượng của lớp

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

64. Sau khi các ký hiệu Sympy được tạo, các ký hiệu có thể được chuyển vào một đối tượng phương trình. Hãy tạo phương trình:
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

7

$$ 2x + y - 1 = 0 $$

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

6

Bây giờ chúng ta hãy tạo một phương trình thứ hai:

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

9

Hiện nay

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0

kết quả trong

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

51.
  1. Sympy
  2. Giải phương trình và viết biểu thức bằng Sympy và Python

Sympy (http://www.sympy.org) là một thư viện Python cho toán học tượng trưng. (http://www.sympy.org) is a Python library for symbolic math. (http://www.sympy.org) is a Python library for symbolic math.

Trong toán học tượng trưng, ​​các biểu tượng được sử dụng để thể hiện các biểu thức toán học. Một ví dụ về biểu thức toán học tượng trưng là bên dưới:

Trong biểu thức ở trên, chúng tôi có các biến $ x $, $ y $ và $ z $.

Nếu chúng ta xác định biểu thức toán học biểu tượng thứ hai là:

Chúng ta có thể thay thế bằng $ a + b $ cho $ x $.

Biểu thức kết quả là:

$$ (a + b)^{2} + y^{2} = z $$$$ a^{2} + 2ab + b^{2} + y^{2} = z $$

Giải quyết $ y $ theo giá $ a $, $ b $ và $ z $, kết quả là:

$$ y = \ sqrt {z - a^{2} - 2ab - b^{2}} $$

Nếu chúng ta có các giá trị số cho $ Z $, $ A $ và $ B $, chúng ta có thể sử dụng Python để tính giá trị của $ y $.

Tuy nhiên, nếu chúng ta không có giá trị số cho $ z $, $ a $ và $ b $, Python cũng có thể được sử dụng để sắp xếp lại các thuật ngữ của biểu thức và giải quyết cho biến $ y $ theo các biến khác $, $ a $ và $ b $. Làm việc với các biểu tượng toán học theo cách lập trình, thay vì làm việc với các giá trị số theo cách lập trình, được gọi là toán học tượng trưng.

Sympy là một thư viện Python để làm việc với toán học tượng trưng. Trước khi Sympy có thể được sử dụng, nó cần được cài đặt. Việc cài đặt Sympy được thực hiện bằng cách sử dụng dấu nhắc Anaconda (hoặc thiết bị đầu cuối và PIP) với lệnh: is a Python library for working with symbolic math. Before SymPy can be used, it needs to be installed. The installation of Sympy is accomplished using the Anaconda Prompt (or a terminal and pip) with the command: is a Python library for working with symbolic math. Before SymPy can be used, it needs to be installed. The installation of Sympy is accomplished using the Anaconda Prompt (or a terminal and pip) with the command:

Sympy được bao gồm trong phân phối Anaconda của Python. Nếu bạn có phân phối Anaconda đầy đủ, bạn sẽ được thông báo rằng thư viện Sympy đã được cài đặt. is included in the Anaconda distribution of Python. If you have the full Anaconda distribution, you will be notified that the SymPy library is already installed. is included in the Anaconda distribution of Python. If you have the full Anaconda distribution, you will be notified that the SymPy library is already installed.

Xác định các biến trong Sympy¶

Để xác định các biến toán học tượng trưng với Sympy, trước tiên hãy nhập hàm

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 từ mô -đun Sympy:SymPy, first import the
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function from the SymPy module:SymPy, first import the
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function from the SymPy module:

In [1]:

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

4

Các biến toán học tượng trưng được khai báo bằng hàm

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 của Sympy. Lưu ý, các đối số được truyền cho hàm
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 (tên biểu tượng) được phân tách bằng một không gian, không có dấu phẩy và được bao quanh bởi các trích dẫn. Đầu ra của hàm
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 là các đối tượng ký hiệu Sympy. Các đối tượng đầu ra này được phân tách bằng dấu phẩy mà không có dấu ngoặc kép.SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function. Note, the arguments passed to the
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function (symbol names) are separated by a space, no comma, and surrounded by quotes. The output of the
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function are SymPy symbols objects. These output objects are separated by commas with no quotation marks.SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function. Note, the arguments passed to the
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function (symbol names) are separated by a space, no comma, and surrounded by quotes. The output of the
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

52 function are SymPy symbols objects. These output objects are separated by commas with no quotation marks.

Bây giờ các biểu tượng

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

56 và
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

57 đã được khởi tạo, một biểu thức toán học tượng trưng sử dụng
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

56 và
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

57 có thể được tạo ra.

Một biểu thức toán học tượng trưng là sự kết hợp của các biến toán học tượng trưng với số và toán tử toán học, chẳng hạn như ____ ____ 40, ________ 41, ________ 30 và

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

73. Các quy tắc Python tiêu chuẩn để làm việc với các số áp dụng trong các biểu thức toán học biểu tượng Sympy.SymPy symbolic math expressions.SymPy symbolic math expressions.

Sử dụng phương pháp

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

74 để chèn giá trị số vào biểu thức toán học tượng trưng. Đối số đầu tiên của phương thức
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

74 là đối tượng ký hiệu (biến) và đối số thứ hai là giá trị số. Trong biểu thức trên:

$$ 2x + y $$

Nếu chúng ta thay thế:

$$ x = 2 $$

Biểu thức kết quả là:

$$ (a + b)^{2} + y^{2} = z $$$$ a^{2} + 2ab + b^{2} + y^{2} = z $$

Giải quyết $ y $ theo giá $ a $, $ b $ và $ z $, kết quả là:

Để làm cho sự thay thế vĩnh viễn, một đối tượng biểu thức mới cần được gán cho đầu ra của phương thức

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

74.

In [6]:

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

9

Các biến Sympy cũng có thể được thay thế thành các biểu thức Sympy variables can also be substituted into SymPy expressions variables can also be substituted into SymPy expressions

In [7]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

0

Sự thay thế phức tạp hơn cũng có thể được hoàn thành. Xem xét những điều sau:

$$ 2x + y $$

Nếu chúng ta thay thế:

$$ x = 2 $$

Biểu thức kết quả là:

$$ (a + b)^{2} + y^{2} = z $$$$ a^{2} + 2ab + b^{2} + y^{2} = z $$

In [8]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

1

Giải quyết $ y $ theo giá $ a $, $ b $ và $ z $, kết quả là:

Để làm cho sự thay thế vĩnh viễn, một đối tượng biểu thức mới cần được gán cho đầu ra của phương thức

In [9]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

2

Các biến Sympy cũng có thể được thay thế thành các biểu thức Sympy variables can also be substituted into SymPy expressions

Sự thay thế phức tạp hơn cũng có thể được hoàn thành. Xem xét những điều sau:SymPy
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

62 methods can be chained together to substitue multiple variables in one line of code.

In [10]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

3

Out[10]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

4

Thay thế trong:

$$ y = 2x^2 + z^{-3} $$Sympy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

63 method.

In [11]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

5

Kết quả trong:

$$ 2x + 2x^2 + z^{-3} $$SymPy using symbolic math variables. Equations in SymPy are different than expressions in SymPy. An expression does not have equality. An equation has equality. An equation is equal to something.

In [1]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

6

Một ví dụ thực tế hơn có thể liên quan đến một biểu thức lớn và một số thay thế thay đổi.

$$ n_0E^{-q_v/rt} $$$$ n_0 = 3,48 \ lần 10^{-6} $$$$4. Sau khi các ký hiệu Sympy được tạo, các ký hiệu có thể được chuyển vào một đối tượng phương trình. Hãy tạo phương trình: equations are instantiated as an object of the

Nhiều phương thức Sympy SymPy symbols are created, the symbols can be passed into an equation object. Let's create the equation:

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

62 có thể được chuỗi với nhau để thay thế nhiều biến trong một dòng mã.SymPy
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

62 methods can be chained together to substitue multiple variables in one line of code.

Để đánh giá một biểu thức là số điểm nổi (lấy câu trả lời bằng số), hãy sử dụng phương thức

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

63 của Sympy.Sympy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

63 method.

Xác định các phương trình trong Sympy¶

Chúng ta có thể xác định các phương trình trong Sympy bằng các biến toán học tượng trưng. Các phương trình trong Sympy khác với các biểu thức trong Sympy. Một biểu thức không có sự bình đẳng. Một phương trình có sự bình đẳng. Một phương trình bằng với một cái gì đó.SymPy using symbolic math variables. Equations in SymPy are different than expressions in SymPy. An expression does not have equality. An equation has equality. An equation is equal to something.SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function. The
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function takes two arguments, a tuple of the equations
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

69 and a tuple of the variables to solve for
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

90.

In [5]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

7

Các phương trình Sympy được khởi tạo như một đối tượng của lớp SymPy solution object is a Python dictionary. The keys are the SymPy variable objects and the values are the numerical values these variables correspond to.

In [6]:

from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

8
from sympy import symbols, Eq, solve

y = symbols('y') eq1 = Eq(y + 3 + 8)

sol = solve(eq1) sol

9

from sympy import symbols, Eq, solve y = symbols('x') eq1 = Eq(x*2 -5x + 6) sol = solve(eq1) sol 14. Sau khi các ký hiệu Sympy được tạo, các ký hiệu có thể được chuyển vào một đối tượng phương trình. Hãy tạo phương trình: equations are instantiated as an object of the

from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

64 class. After SymPy symbols are created, the symbols can be passed into an equation object. Let's create the equation:

$$ 2x + y - 1 = 0 $$SymPy. Using symbolic math, we can define expressions and equations exactly in terms of symbolic variables. We reviewed how to create a SymPy expression and substitue values and variables into the expression. Then we created to SymPy equation objects and solved two equations for two unknowns using SymPy's
from sympy import symbols, Eq, solve

y = symbols('x') eq1 = Eq(x*2 -5x + 6)

sol = solve(eq1) sol

0 function.