Hướng dẫn plot the function in plane and its derivative python - vẽ đồ thị của hàm trong mặt phẳng và python dẫn xuất của nó

Trong bài viết này, chúng tôi sẽ vẽ đồ dẫn đạo hàm của một hàm bằng cách sử dụng matplotlib và python. Đối với điều này, chúng tôi đang sử dụng một số mô -đun trong Python như sau:

  • Matplotlib: matplotlib là một trong những gói python phổ biến nhất được sử dụng để trực quan hóa dữ liệu. Đây là một thư viện đa nền tảng để tạo các lô 2D từ dữ liệu trong các mảng.Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays.
  • Numpy: Đây là một thư viện Python được sử dụng để làm việc với các mảng, nó cũng hỗ trợ các mảng và ma trận đa chiều lớn, nó cũng có một số chức năng toán học.It is a python library that is used for working with arrays, it also supports large multi-dimensional arrays and matrices, it also has several mathematical functions.
  • Scipy: Python có một thư viện có tên là SCIPY được sử dụng cho các tính toán toán học, khoa học và kỹ thuật. Thư viện này phụ thuộc vào Numpy, và cung cấp các hoạt động số khác nhau.Python has a library named as SciPy that is used for mathematical, scientific, and engineering calculations. This library depends on NumPy, and provides various numerical operations.

Để vẽ đồ dẫn của một hàm trước, chúng ta phải tính toán nó. Thư viện scipy.misc có hàm đạo hàm () chấp nhận một đối số là hàm và một hàm khác là biến w.r.t mà chúng ta sẽ phân biệt hàm. Vì vậy, chúng tôi sẽ tạo một phương thức có tên hàm () sẽ trả về hàm gốc và phương thức thứ hai có tên deriv () sẽ trả về đạo hàm của hàm đó.derivative() function which accepts one argument as a function and the other is the variable w.r.t which we will differentiate the function. So we will make a method named function() that will return the original function and a second method named deriv() that will return the derivative of that function.

Sau khi tính toán đạo hàm của hàm đầu vào này, chúng ta sẽ sử dụng hàm linspace () không gian numpy đặt ra phạm vi của trục x. Hàm lô () sẽ được sử dụng để vẽ hàm và cũng là đạo hàm của hàm đó.linspace() function which sets the range of the x-axis. The plot() function will be used to plot the function and also the derivative of that function.

Approach:

  • Nhập các mô -đun cần thiết.
  • Xác định các phương thức cho chức năng và đạo hàm của nó
  • Sử dụng chức năng không gian numpy để tạo khoảng cách trục x.
  • Vẽ đồ thị chức năng và đạo hàm của nó
  • Thay đổi giới hạn của trục bằng hàm gca ()
  • Vẽ đồ thị văn bản bằng hàm ()

Ví dụ 1: (đạo hàm của khối) & nbsp;

Trong ví dụ này, chúng tôi sẽ cung cấp hàm f (x) = 2x3+x+3 làm đầu vào, sau đó tính toán đạo hàm và vẽ cả hàm và đạo hàm của nó.

Python3

import matplotlib.pyplot as plt

from scipy.misc import derivative

import numpy as np

def function(x):

Các

def from4

matplotlib.pyplot as plt0____11 from7

from8from9 scipy.misc 0scipy.misc 1scipy.misc 2scipy.misc 3scipy.misc 2scipy.misc 5

scipy.misc 6from9scipy.misc 8scipy.misc 9from9import1scipy.misc 5

import3from9import5scipy.misc 9from9import8scipy.misc 5

derivative0from9derivative2scipy.misc 5

derivative4derivative5scipy.misc 5

Output:

Hướng dẫn plot the function in plane and its derivative python - vẽ đồ thị của hàm trong mặt phẳng và python dẫn xuất của nó

Ví dụ 2: (đạo hàm của đa thức độ poly) & nbsp;

Trong ví dụ này, chúng tôi sẽ cung cấp hàm f (x) = x4+x2+5 làm đầu vào, sau đó tính toán đạo hàm và vẽ cả hàm và đạo hàm của nó.

Python3

import matplotlib.pyplot as plt

from scipy.misc import derivative

import numpy as np

def function(x):

Các

def from4

matplotlib.pyplot as plt0____11 from7

from8from9 scipy.misc 0scipy.misc 1scipy.misc 2scipy.misc 3scipy.misc 2scipy.misc 5

scipy.misc 6from9function(x):7scipy.misc 9from9import1scipy.misc 5

import3from9import5scipy.misc 9from9import8scipy.misc 5

Ví dụ 2: (đạo hàm của đa thức độ poly) & nbsp;

derivative4derivative5scipy.misc 5

Output:

Trong ví dụ này, chúng tôi sẽ cung cấp hàm f (x) = x4+x2+5 làm đầu vào, sau đó tính toán đạo hàm và vẽ cả hàm và đạo hàm của nó.

Cácgca() function that will change the limits of the axis so that both x, y axes intersect at the origin. The text() function which comes under matplotlib library plots the text on the graph and takes an argument as (x, y) coordinates. We will also do some formatting.

Python3

import matplotlib.pyplot as plt

from scipy.misc import derivative

import numpy as np

def function(x):

Các

def from4

matplotlib.pyplot as plt0____11 from7

from8from9 scipy.misc 0scipy.misc 1scipy.misc 2scipy.misc 3scipy.misc 2scipy.misc 5

scipy.misc 6from9matplotlib.pyplot as plt53scipy.misc 9from9import1scipy.misc 5

import3from9matplotlib.pyplot as plt60scipy.misc 9from9import8scipy.misc 5

matplotlib.pyplot as plt65matplotlib.pyplot as plt66matplotlib.pyplot as plt67matplotlib.pyplot as plt68matplotlib.pyplot as plt69

Ví dụ 2: (đạo hàm của đa thức độ poly) & nbsp;

derivative0from9derivative2scipy.misc 5

matplotlib.pyplot as plt79matplotlib.pyplot as plt80scipy.misc 3matplotlib.pyplot as plt82matplotlib.pyplot as plt83matplotlib.pyplot as plt84matplotlib.pyplot as plt85from9matplotlib.pyplot as plt87

matplotlib.pyplot as plt88matplotlib.pyplot as plt89from9matplotlib.pyplot as plt91matplotlib.pyplot as plt92from9matplotlib.pyplot as plt60scipy.misc 5

matplotlib.pyplot as plt79scipy.misc 1matplotlib.pyplot as plt98scipy.misc 3from00matplotlib.pyplot as plt83from02matplotlib.pyplot as plt85from9from05from06

matplotlib.pyplot as plt88matplotlib.pyplot as plt89from9matplotlib.pyplot as plt91matplotlib.pyplot as plt92from9matplotlib.pyplot as plt53scipy.misc 5

derivative4derivative5scipy.misc 5

Output:


Làm thế nào để bạn vẽ một hàm và phái sinh trong Python?

Approach:..
Nhập các mô -đun cần thiết ..
Xác định các phương thức cho chức năng và đạo hàm của nó ..
Sử dụng chức năng không gian Linspace để tạo khoảng cách trục x ..
Vẽ đồ thị chức năng và đạo hàm của nó ..
Thay đổi giới hạn của trục bằng hàm gca () ..
Vẽ đồ thị văn bản bằng hàm text () ..

Làm thế nào để bạn viết đạo hàm của một hàm trong Python?

Với sự trợ giúp của phương thức sympy.derivative (), chúng ta có thể tạo ra một dẫn xuất không được đánh giá của biểu thức sympy.Nó có cùng một cú pháp với phương thức Diff ().Để đánh giá một đạo hàm không được đánh giá, hãy sử dụng phương thức doit ().sympy. Derivative() method, we can create an unevaluated derivative of a SymPy expression. It has the same syntax as diff() method. To evaluate an unevaluated derivative, use the doit() method.

Làm cách nào để vẽ một hàm trong Python?

Matplotlib với Python..
Đặt kích thước hình và điều chỉnh phần đệm giữa và xung quanh các ô phụ ..
Tạo chức năng do người dùng xác định bằng cách sử dụng, def, tức là, f (x) ..
Tạo các điểm dữ liệu X bằng cách sử dụng Numpy ..
Sơ đồ x và f (x) bằng phương thức cốt truyện () ..
Để hiển thị hình, sử dụng phương thức show () ..