龙空技术网

python计算导数、不定积分和定积分

冬瓜视频1号 77

前言:

现在大家对“怎么用python求积分”大体比较着重,姐妹们都需要知道一些“怎么用python求积分”的相关知识。那么小编也在网摘上搜集了一些有关“怎么用python求积分””的相关内容,希望各位老铁们能喜欢,小伙伴们快快来学习一下吧!

import numpy as np

import matplotlib.pyplot as plt

# 定义函数 f(x) = x^2

def f(x):

return x**2

# 定义导数函数 f'(x) = 2x

def f_prime(x):

return 2*x

# 定义不定积分函数 F(x) = (1/3) * x^3 + C(C为积分常数,这里不考虑C)

def F(x):

return (1/3) * x**3

# 定义定积分区间 [a, b]

a = 2

b = 3

# 计算定积分结果

definite_integral = F(b) - F(a)

# 生成 x 值范围

x = np.linspace(0, 3, 400)

# 计算对应的 y 值

y = f(x)

y_prime = f_prime(x)

y_integral = F(x)

# 绘图

plt.figure(figsize=(10, 6))

plt.plot(x, y, label='f(x) = x^2', color='blue')

plt.plot(x, y_prime, label="f'(x) = 2x", linestyle='--', color='red')

plt.plot(x, y_integral, label="∫(x^2) dx = (1/3) * x^3", linestyle=':', color='green')

# 高亮定积分区域

plt.fill_between(x, 0, y, where=(x >= a) & (x <= b), color='yellow', alpha=0.3, label='Dingjifen Quyu')

plt.legend()

plt.grid(True)

plt.title('Function, Derivative, Indefinite Integral, and Definite Integral')

plt.xlabel('x')

plt.ylabel('y')

plt.xlim(0, 3)

plt.ylim(0, 10)

plt.axhline(0, color='black',linewidth=0.5)

plt.axvline(0, color='black',linewidth=0.5)

plt.legend()

plt.show()

标签: #怎么用python求积分