python微元法计算函数曲线长度的方法

yipeiwu_com5年前Python基础

计算曲线长度,根据线积分公式:

python微元法计算函数曲线长度,令积分函数 f(x,y,z) 为1,即计算曲线的长度,将其微元化:

python微元法计算函数曲线长度

其中

python微元法计算函数曲线长度

根据此时便可在python编程实现,给出4个例子,代码中已有详细注释,不再赘述

'''
计算曲线长度,根据线积分公式:
\int_A^Bf(x,y,z)dl,令积分函数为1,即计算曲线的长度
'''
import numpy as np
from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt

## 求二维圆周长,半径为1,采用参数形式
def circle_2d(dt=0.001,plot=True):
 dt = dt # 变化率
 t = np.arange(0,2*np.pi, dt)
 x = np.cos(t)
 y = np.sin(t)

 # print(len(t))
 area_list = [] # 存储每一微小步长的曲线长度

 for i in range(1,len(t)):
  # 计算每一微小步长的曲线长度,dx = x_{i}-x{i-1},索引从1开始
  dl_i = np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 ) 
  # 将计算结果存储起来
  area_list.append(dl_i)

 area = sum(area_list)# 求和计算曲线在t:[0,2*pi]的长度

 print("二维圆周长:{:.4f}".format(area))
 if plot:
  fig = plt.figure()
  ax = fig.add_subplot(111)
  ax.plot(x,y)
  plt.title("circle")
  plt.show()


## 二维空间曲线,采用参数形式
def curve_param_2d(dt=0.0001,plot=True):
 dt = dt # 变化率
 t = np.arange(0,2*np.pi, dt)
 x = t*np.cos(t)
 y = t*np.sin(t)

 # print(len(t))
 area_list = [] # 存储每一微小步长的曲线长度

 # 下面的方式是循环实现
 # for i in range(1,len(t)):
 #  # 计算每一微小步长的曲线长度,dx = x_{i}-x{i-1},索引从1开始
 #  dl_i = np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 ) 
 #  # 将计算结果存储起来
 #  area_list.append(dl_i)

 # 更加pythonic的写法
 area_list = [np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 ) for i in range(1,len(t))]

 area = sum(area_list)# 求和计算曲线在t:[0,2*pi]的长度

 print("二维参数曲线长度:{:.4f}".format(area))

 if plot:

  fig = plt.figure()
  ax = fig.add_subplot(111)
  ax.plot(x,y)
  plt.title("2-D Parameter Curve")
  plt.show()

## 二维空间曲线
def curve_2d(dt=0.0001,plot=True):
 dt = dt # 变化率
 t = np.arange(-6,10, dt)
 x = t
 y = x**3/8 - 4*x + np.sin(3*x)

 # print(len(t))
 area_list = [] # 存储每一微小步长的曲线长度

 # for i in range(1,len(t)):
 #  # 计算每一微小步长的曲线长度,dx = x_{i}-x{i-1},索引从1开始
 #  dl_i = np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 ) 
 #  # 将计算结果存储起来
 #  area_list.append(dl_i)

 area_list = [np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 ) for i in range(1,len(t))]

 area = sum(area_list)# 求和计算曲线在t:[0,2*pi]的长度

 print("二维曲线长度:{:.4f}".format(area))

 if plot:
  fig = plt.figure()
  ax = fig.add_subplot(111)
  ax.plot(x,y)
  plt.title("2-D Curve")
  plt.show()

## 三维空间曲线,采用参数形式
def curve_3d(dt=0.001,plot=True):
 dt = dt # 变化率
 t = np.arange(0,2*np.pi, dt)
 x = t*np.cos(t)
 y = t*np.sin(t)
 z = 2*t

 # print(len(t))
 area_list = [] # 存储每一微小步长的曲线长度

 for i in range(1,len(t)):
  # 计算每一微小步长的曲线长度,dx = x_{i}-x{i-1},索引从1开始
  dl_i = np.sqrt( (x[i]-x[i-1])**2 + (y[i]-y[i-1])**2 + (z[i]-z[i-1])**2 ) 
  # 将计算结果存储起来
  area_list.append(dl_i)

 area = sum(area_list)# 求和计算曲线在t:[0,2*pi]的长度

 print("三维空间曲线长度:{:.4f}".format(area))

 if plot:
  fig = plt.figure()
  ax = fig.add_subplot(111,projection='3d')
  ax.plot(x,y,z)
  plt.title("3-D Curve")
  plt.show()

if __name__ == '__main__':

 circle_2d(plot=True)
 curve_param_2d(plot=True)
 curve_2d(plot=True)
 curve_3d(plot=True)

得到结果:

二维圆周长:6.2830
二维参数曲线长度:21.2558
二维曲线长度:128.2037
三维空间曲线长度:25.3421

python微元法计算函数曲线长度

python微元法计算函数曲线长度

python微元法计算函数曲线长度

python微元法计算函数曲线长度

以上这篇python微元法计算函数曲线长度的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 通过可变参数计算n个数的乘积方法

python 通过可变参数计算n个数的乘积方法

通过可变参数计算n个数的乘积: 代码如下: list = [] def the_input(count=eval(input("输入乘数的总个数:"))): for i in...

python numpy 部分排序 寻找最大的前几个数的方法

如下所示: import numpy as np K=4 a = np.array([0, 8, 0, 4, 5, 8, 8, 0, 4, 2]) a[np.argpartition...

Python+Socket实现基于TCP协议的客户与服务端中文自动回复聊天功能示例

Python+Socket实现基于TCP协议的客户与服务端中文自动回复聊天功能示例

本文实例讲述了Python+Socket实现基于TCP协议的客户与服务端中文自动回复聊天功能。分享给大家供大家参考,具体如下: 【吐槽】 网上的代码害死人,看着都写的言之凿凿,可运行就是...

python实现批量修改图片格式和尺寸

本文实例为大家分享了python批量处理图片的具体代码,供大家参考,具体内容如下 公司的一个项目要求把所有4096x4096的图片全部转化成2048x2048的图片,这种批量转换图片大...

Django接收post前端返回的json格式数据代码实现

post接收字符串 def subscription(request): msg = request.POST.get('msg') # tel_no = request....