Python tích hợp đa biến

Giải tích là một nhánh của toán học tập trung vào giới hạn, hàm số, đạo hàm, tích phân và chuỗi vô hạn. Chúng tôi sẽ sử dụng thư viện SymPy để tính toán với python. SymPy là một thư viện Python cho toán học tượng trưng. Nó nhằm mục đích trở thành một hệ thống đại số máy tính (CAS) đầy đủ tính năng trong khi vẫn giữ mã đơn giản nhất có thể để dễ hiểu và dễ mở rộng. SymPy được viết hoàn toàn bằng Python

Cài đặt

pip install sympy

Nếu chúng ta muốn viết bất kỳ biểu thức sympy nào, trước tiên chúng ta phải khai báo các biến biểu tượng của nó. Để làm điều này, chúng ta có thể sử dụng hai chức năng sau

  • sympy. Biểu tượng(). Nó được sử dụng để khai báo một biến duy nhất bằng cách chuyển biến đó dưới dạng một chuỗi vào tham số của nó
  • sympy. biểu tượng(). Nó được sử dụng để khai báo đa biến bằng cách chuyển các biến dưới dạng chuỗi vào tham số của nó. Tất cả các biến phải được phân tách bằng dấu cách tạo thành một chuỗi

khác biệt hóa

Chúng ta có thể phân biệt bất kỳ biểu thức sympy nào bằng cách sử dụng phương thức diff(func, var). Tham số func biểu thị biểu thức sympy được phân biệt và var biểu thị biến mà chúng ta phải phân biệt

ví dụ 1

Python3




# Importing library

import sympy as sym

 

# Declaring variables

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
0
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
2
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
3
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
4

 

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
5

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
6
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
8
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
9
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
9
second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y
1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
9
second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y
3
second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y
4
second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y
3
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
9
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
9
second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y
1
second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y
4
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
0

 

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
1

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
2_______1_______1
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
4

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
6
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
7
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
9
oo
0
1
0

 

oo
0
1
1

oo
0
1
2_______1_______1
oo
0
1
4

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
6
oo
0
1
7
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
9
1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
0

đầu ra.  

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2

Chúng ta cũng có thể tìm các dẫn xuất cao hơn bằng cách sử dụng phương pháp diff(func, var, n). Ở đây, tham số n biểu thị đạo hàm thứ n được tìm thấy

ví dụ 2.  

Python3




1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
1

1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
2

1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
3
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
1
1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
5
1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
6_______1_______4

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
6# Importing library0
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
9# Importing library3

 

1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
1

# Importing library5

# Importing library6_______1_______1 # Importing library8

1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
6
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
4

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
6import3
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
9import6

đầu ra.  

second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y

Hội nhập

Bạn có thể thực hiện tích hợp xác định và vô thời hạn của các hàm cơ bản và hàm đặc biệt siêu việt thông qua hàm integration().  

Cú pháp tích phân vô thời hạn. sympy. tích hợp (func, var)

Cú pháp tích phân xác định. sympy. tích hợp(func, (var,  lower_limit, upper_limit))

Tham số func biểu thị biểu thức sympy cần lấy vi phân, var biểu thị biến mà chúng ta phải lấy vi phân, Lower_limit biểu thị giới hạn dưới của tích phân xác định và upper_limit biểu thị giới hạn trên của tích phân xác định

Ghi chú. ∞ trong SymPy là oo

ví dụ 1.  

Python3




import7

import8______1_______1 sympy as sym0

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
6sympy as sym3
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
9sympy as sym6

 

sympy as sym7

sympy as sym8

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
1 # Declaring variables0# Declaring variables1# Declaring variables2
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8# Declaring variables2# Declaring variables5

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
6# Declaring variables8
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
9_______1_______01

 

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
02

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
03_______1_______1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
05# Declaring variables1_______1_______07
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
08
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
09

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
6
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
12
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
9_______1_______15

đầu ra.  

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1

Hạn mức

Bạn có thể tính giới hạn của hàm bằng cách sử dụng giới hạn(hàm, biến, điểm). Vì vậy, nếu bạn muốn tính giới hạn của f(x) là  x->0, bạn sẽ đưa ra giới hạn (f, x, 0)

Thí dụ.  

Python3




derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
16

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
17_______1_______1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
19

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
21

 

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
22

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
23
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
25# Declaring variables2_______1_______27
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
28

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
30

 

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
31

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
32_______1_______1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
34
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
27_______1_______36
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
08
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
4

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
40

đầu ra.  

oo
0
1

Mở rộng sê-ri

Chúng ta cũng có thể tính toán khai triển chuỗi Taylor của các hàm xung quanh một điểm. Để tính toán khai triển của f(x) xung quanh điểm x=x0 theo thứ tự xn, hãy sử dụng sympy. chuỗi(f, x, x0, n). x0 và n có thể được bỏ qua, trong trường hợp đó, giá trị mặc định x0=0 và n=6 sẽ được sử dụng

Thí dụ.  

Python3




derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
41

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
42______1_______1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
44

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
46

 

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
41

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
48
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
1
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
50# Declaring variables2_______1_______27
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
53
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
08
indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
8
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
56
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
4

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1
5
derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2
59

đầu ra.  

1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)

Số hạng O(x4) hoặc O(x6) ở cuối có nghĩa là tất cả các số hạng x có lũy thừa lớn hơn hoặc bằng x4 hoặc x6 đều bị bỏ qua

Trăn Cumtrapz() là gì?

cumtrapz(), chúng ta có thể lấy giá trị tích phân tích lũy của y(x) bằng cách sử dụng quy tắc hình thang tổng hợp bằng cách sử dụng scipy. tích hợp. phương thức cumtrapz(). cú pháp. scipy. tích hợp. cumtrapz() Trả về. Trả về giá trị tích phân tích lũy của y(x).

Tích hợp quad hoạt động như thế nào?

Hàm quad trả về hai giá trị, trong đó số đầu tiên là giá trị của tích phân và giá trị thứ hai là ước tính của sai số tuyệt đối trong giá trị của tích phân . Lưu ý - Vì quad yêu cầu hàm làm đối số đầu tiên nên chúng ta không thể chuyển trực tiếp exp làm đối số.

Sự khác biệt giữa quad và quadrature trong Scipy là gì?

quad nhận một hàm có đối số vô hướng trong khi bậc hai nhận một hàm nhận đối số vectơ (nhiều đánh giá khác nhau của hàm đồng thời). Thích ứng chỉ có nghĩa là nó lấy nhiều mẫu hơn (hiệu quả) cho đến khi sai số cận biên giảm xuống dưới một số dung sai.

Tích hợp quad trở lại là gì?

quad(), chúng ta có thể lấy tích phân của một hàm đã cho từ giới hạn a đến b bằng cách sử dụng scipy. tích hợp. phương thức quad(). Trở lại. Trả về tích phân của một đa thức .