用数据评估未知函数的积分 table
Evaluate integral of unknown function with data table
我想找到 2 个有界区域。但是,我不知道蓝色曲线的功能,但我确实有它的数据 table。我查找并发现了使用 scipy.integrate.simps
的可能性,但我不知道如何指定该区域以红线(上或下)为界而不是 x 轴为界。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
from sklearn.linear_model import LinearRegression
from scipy import interpolate
from scipy import integrate
%matplotlib inline
file = pd.read_excel("T8.xlsx","Phthalansäureanhydrid",usecols=[2,3])
X = file['Zeit(s)']
Y = file['Temperatur(Celcius Grad)']
fig, ax = plt.subplots()
ax.plot(X,Y,'-',color='#10A5F3', label="Phthalansäureanhydrid")
ax.grid(True, which='major', axis='both', color='#F19211', linestyle='-')
#ax.grid(True, which='minor', axis='both', color='#F19211', linestyle='--')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
#ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
#ax.legend(loc='upper center', frameon=True)
#major & minor ticks
ax.xaxis.set_major_locator(MultipleLocator(100))
ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
ax.xaxis.set_minor_locator(MultipleLocator(10))
#extrapolation - first line
temp1 = []
time1 = []
xnew1 = []
for i in file.index:
if i > 630:
temp1.append(file['Temperatur(Celcius Grad)'][i])
time1.append(file['Zeit(s)'][i])
else:
xnew1.append(file['Zeit(s)'][i])
extrapo1 = InterpolatedUnivariateSpline(time1, temp1, k=1)
ynew1 = extrapo1(xnew1)
#extrapolation - second line
temp2 = []
time2 = []
xnew2 = []
for i in file.index:
if 100<i<400:
temp2.append(file['Temperatur(Celcius Grad)'][i])
time2.append(file['Zeit(s)'][i])
if i>400:
xnew2.append(file['Zeit(s)'][i])
ynew2 = np.polyval(z,xnew2)
z = np.polyfit(time2,temp2,1)
#horizontal line
def hori(x):
a = xnew1.index(x)
b = xnew2.index(x)
return np.linspace(ynew2[b],ynew1[a])
#integral
plt.plot(xnew1,ynew1,'-',color='black')
plt.plot(xnew2,ynew2,'-',color='black')
plt.plot([470]*len(hori(470)),hori(470),'--',color='red')
plt.savefig('phth.pdf')
Link 到数据:https://docs.google.com/spreadsheets/d/1xznXj-aA-Szq2s4KWb-qPWYxZbQNrA5FgUCQT6i7oVo/edit?usp=sharing
我将编写一个基本的说明性示例,以便您了解如何处理此类问题。这不是针对您的数据,但应该对您有所帮助:
import matplotlib.pyplot as plt
import numpy as np
# Here are my curves
x = np.linspace(0, np.pi/2, num=100)
blue = np.sin(x)
orange = np.linspace(0.5, 0.8, num=100)
green = np.linspace(0.1, 0.2, num=100)
# Here is a plot to illustrate my example
plt.plot(x, blue, x, orange, x, green)
plt.show()
这个地块已经标注了我想要计算的区域;蓝色曲线下方但以绿色和橙色曲线为界的区域:
因此,为了解决这个问题,我们将解决以下问题:
当它高于绿色且低于橙色时在蓝色曲线下方
并将其添加到区域
低于蓝色时在橙色曲线下方
然后减去面积
低于蓝色时在绿色曲线下方
这有点复杂,但请尝试描绘这些区域,以便您可以弄清楚我们为什么要添加和减去这些区域。我们可以使用 np.trapz
函数计算出这些区域,并只屏蔽我们想要的区域:
mask = (blue > green) * (blue < orange)
first_bullet_area = np.trapz(blue[mask], x[mask])
mask = (orange < blue)
second_bullet_area = np.trapz(orange[mask], x[mask])
mask = (green < blue)
third_bullet_area = np.trapz(green[mask], x[mask])
然后我们只需要做加减法:
>>> first_bullet_area + second_bullet_area - third_bullet_area
0.6190953349008973
我想找到 2 个有界区域。但是,我不知道蓝色曲线的功能,但我确实有它的数据 table。我查找并发现了使用 scipy.integrate.simps
的可能性,但我不知道如何指定该区域以红线(上或下)为界而不是 x 轴为界。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
from sklearn.linear_model import LinearRegression
from scipy import interpolate
from scipy import integrate
%matplotlib inline
file = pd.read_excel("T8.xlsx","Phthalansäureanhydrid",usecols=[2,3])
X = file['Zeit(s)']
Y = file['Temperatur(Celcius Grad)']
fig, ax = plt.subplots()
ax.plot(X,Y,'-',color='#10A5F3', label="Phthalansäureanhydrid")
ax.grid(True, which='major', axis='both', color='#F19211', linestyle='-')
#ax.grid(True, which='minor', axis='both', color='#F19211', linestyle='--')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
#ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
#ax.legend(loc='upper center', frameon=True)
#major & minor ticks
ax.xaxis.set_major_locator(MultipleLocator(100))
ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
ax.xaxis.set_minor_locator(MultipleLocator(10))
#extrapolation - first line
temp1 = []
time1 = []
xnew1 = []
for i in file.index:
if i > 630:
temp1.append(file['Temperatur(Celcius Grad)'][i])
time1.append(file['Zeit(s)'][i])
else:
xnew1.append(file['Zeit(s)'][i])
extrapo1 = InterpolatedUnivariateSpline(time1, temp1, k=1)
ynew1 = extrapo1(xnew1)
#extrapolation - second line
temp2 = []
time2 = []
xnew2 = []
for i in file.index:
if 100<i<400:
temp2.append(file['Temperatur(Celcius Grad)'][i])
time2.append(file['Zeit(s)'][i])
if i>400:
xnew2.append(file['Zeit(s)'][i])
ynew2 = np.polyval(z,xnew2)
z = np.polyfit(time2,temp2,1)
#horizontal line
def hori(x):
a = xnew1.index(x)
b = xnew2.index(x)
return np.linspace(ynew2[b],ynew1[a])
#integral
plt.plot(xnew1,ynew1,'-',color='black')
plt.plot(xnew2,ynew2,'-',color='black')
plt.plot([470]*len(hori(470)),hori(470),'--',color='red')
plt.savefig('phth.pdf')
Link 到数据:https://docs.google.com/spreadsheets/d/1xznXj-aA-Szq2s4KWb-qPWYxZbQNrA5FgUCQT6i7oVo/edit?usp=sharing
我将编写一个基本的说明性示例,以便您了解如何处理此类问题。这不是针对您的数据,但应该对您有所帮助:
import matplotlib.pyplot as plt
import numpy as np
# Here are my curves
x = np.linspace(0, np.pi/2, num=100)
blue = np.sin(x)
orange = np.linspace(0.5, 0.8, num=100)
green = np.linspace(0.1, 0.2, num=100)
# Here is a plot to illustrate my example
plt.plot(x, blue, x, orange, x, green)
plt.show()
这个地块已经标注了我想要计算的区域;蓝色曲线下方但以绿色和橙色曲线为界的区域:
因此,为了解决这个问题,我们将解决以下问题:
当它高于绿色且低于橙色时在蓝色曲线下方
并将其添加到区域
低于蓝色时在橙色曲线下方
然后减去面积
低于蓝色时在绿色曲线下方
这有点复杂,但请尝试描绘这些区域,以便您可以弄清楚我们为什么要添加和减去这些区域。我们可以使用 np.trapz
函数计算出这些区域,并只屏蔽我们想要的区域:
mask = (blue > green) * (blue < orange)
first_bullet_area = np.trapz(blue[mask], x[mask])
mask = (orange < blue)
second_bullet_area = np.trapz(orange[mask], x[mask])
mask = (green < blue)
third_bullet_area = np.trapz(green[mask], x[mask])
然后我们只需要做加减法:
>>> first_bullet_area + second_bullet_area - third_bullet_area
0.6190953349008973