import matplotlib.pyplot as pltimport numpy as npfrom scipy.integrate import quadfrom math import pi
例 1:函数f(x)和g(x)如图1,求这两个曲线合围的区域的面积
例1求解步骤如下:
def f(x): return 0.5*x**2def g(x): return 1/(1+x**2)x = np.linspace(-2,2,100)
plt.plot(x,f(x))plt.plot(x,g(x))plt.show()
要想求出这两个函数的曲线合围区域的面积,就要找到两个曲线的交点,即f(x)和g(x)联立方程组求得两个根:-1 和 1,然后用scipy中的求积分函数quad(),代码如下:
# 求函数f(x)和g(x)图形合围区域的面积,a1 = quad(f,-1,1)[0]a2 = quad(g,-1,1)[0]abs(a1 - a2)
def enclosed_area(x): return g(x) - f(x)
quad(enclosed_area, -1, 1)
计算结果:(1.2374629934615633, 1.3738599074625972e-14),包括两个数值,前面一个为面积的值,后面一个是误差。
例 2 :函数f(x)和g(x)如图3,求这两个曲线合围的闭合区域的面积之和
例2求解步骤如下:
def f(x): return 1 + x + np.e**(x**2 - 2 * x)def g(x): return x**4 - 6.5*x**2 + 6*x + 2x = np.linspace(0,2,100)
plt.plot(x,f(x))plt.plot(x,g(x))plt.fill_between(x,f(x),g(x), color = 'pink', alpha = 0.5)plt.text(0.5, 2.5,'Left')plt.text(1.5, 2.0,'Right')plt.show()
plt.plot(x,f(x)- g(x))plt.axhline(color = 'black')plt.title('f(x) - g(x) = 0 ');
from scipy.optimize import root_scalar
def difference(x): return f(x) - g(x)
root_scalar(difference, bracket = [0.75, 1.25], method = 'bisect')roots = root_scalar(difference, bracket = [0.75, 1.25], method = 'bisect')roots.root
结果为1.032831888363944。
def Left(x): return g(x) - f(x)def Right(x): return f(x) - g(x)area_Left = quad(Left,0,roots.root)[0]area_Right = quad(Right,roots.root, 2)[0]area_Right + area_Left
两个闭合区域的面积之和为2.0043456631323346。
例 3 : 求f(x)和g(x)的函数曲线所形成的非闭合区域的面积
已知 f(x)=sin(x), g(x)=cos(x), (0<x<π),求这两个函数曲线所形成的非闭合区域的面积
例3求解步骤如下:
def f(x): return np.sin(x)def g(x): return np.cos(x)x = np.linspace(0,pi,100)
plt.plot(x,f(x))plt.plot(x,g(x))plt.fill_between(x,f(x),g(x), color = 'pink', alpha = 0.5)plt.text(0.25, 0.60,'Left')plt.text(1.8, 0.25,'Right')plt.show()
plt.plot(x,f(x)- g(x))plt.axhline(color = 'black')plt.title('f(x) - g(x) = 0 ');
from scipy.optimize import root_scalar
def difference(x): return f(x) - g(x)
root_scalar(difference, bracket = [0, pi], method = 'bisect')roots = root_scalar(difference, bracket = [0, pi], method = 'bisect')roots.root
结果为0.7853981633974483。
plt.plot(x,f(x) - g(x))plt.axhline(color = 'black')plt.title('f(x) - g(x) = 0');plt.plot(roots.root, difference(roots.root),'ro');
plt.plot(x,f(x))plt.plot(x,g(x))plt.plot(roots.root,f(roots.root),'ro')
def Left(x): return g(x) - f(x)def Right(x): return f(x) - g(x)area_Left = quad(Left,0,roots.root)[0]area_Right = quad(Right,roots.root, 2)[0]area_Right + area_Left
两个非闭合区域的面积之和为1.3352765344676507。
例 4 :如图12,求f(x)和g(x)的曲线合围而成的多个区域的面积之和,
例4中f(x)和g(x)两条曲线形成的的合围区域较多,但是求区域面积的方法与例2和例3类似,求解步骤如下:
def blue_curve(x): return x**4 + 2*x**3 - 12*x**2 - 15*x +22def yellow_curve(x): return 0.5*np.sin(x)x = np.linspace(-4.5,4,100)
绘制f(x)和g(x)的曲线图(图13),代码如下:
plt.plot(x,blue_curve(x))plt.plot(x,yellow_curve(x))plt.fill_between(x,blue_curve(x),yellow_curve(x), color = 'pink', alpha = 0.5)plt.text(-4.3, 5,'1')plt.text(-3.2, -8,'2')plt.text(-1, 5,'3')plt.text(2, -10,'4')plt.text(3.5, 5,'5')plt.show()
plt.plot(x,blue_curve(x) - yellow_curve(x))plt.axhline(color = 'black')plt.title('f(x) - g(x) = 0');plt.plot(roots_1.root, difference(roots_1.root),'ro')plt.plot(roots_2.root, difference(roots_2.root),'ro')plt.plot(roots_3.root, difference(roots_3.root),'ro')plt.plot(roots_4.root, difference(roots_4.root),'ro');
def area_One(x): return blue_curve(x) - yellow_curve(x) def area_Two(x): return yellow_curve(x) - blue_curve(x) def area_Three(x): return blue_curve(x) - yellow_curve(x)def area_Four(x): return yellow_curve(x) - blue_curve(x) def area_Five(x): return blue_curve(x) - yellow_curve(x)
area_first = quad(area_One,-4,roots_1.root)[0]# (合围区域1的面积为2.269226788589267) area_second = quad(area_Two,roots_1.root,roots_2.root)[0]# (合围区域2的面积为14.145183556035736) area_third = quad(area_Three,roots_2.root,roots_3.root)[0]# (合围区域3的面积为52.57306136862575)area_fourth = quad(area_Four,roots_3.root,roots_4.root)[0]# (合围区域4的面积为32.5705234694899)area_fifth = quad(area_Five,roots_4.root,4)[0]# (合围区域5的面积为65.4734188683106)Total_area = area_first + area_second + area_third + area_fourth + area_fifth# (5个合围区域的面积之和为167.03141405105126)
(注:图1、图2和图12中,上一行为表达式的显示状态,下一行为表达式的输入状态。)
附:
例题4中 f(x)和g(x)的值差别太大,g(x)在前面的图上看起来是一条直线,当g(x) = 30*sin(x) ,效果如下图。因平台对文章修改有字数限制,前面例4的内容就不修改了,各位看官将就看吧!
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!