簡體   English   中英

是否有用於查找“曲線下面積”的每個部分的 Python function?

[英]Is there a Python function for finding each section of "Area Under curve"?

我正在嘗試使用 Python 包(simps,trapz)計算曲線下的面積(“A1”,“A2”,“A3”,由紅線切片,第一行圖)。

問題是“A2”(第二行圖,“Area2”)的結果與我預期的不同。 如何切出“A2” (不是“Area2”)來計算截面的面積?

最后,有誰知道如何計算忽略負 y 軸值(A3)的絕對面積


a1y = [-731.11059984, -728.47093717, -713.86716911, -680.2975881 ,
       -618.76047483, -524.25410195, -396.7767379 , -240.32665095,
        -63.9021133 ,  122.49859465,  306.87718006,  478.23533331,
        630.57472375,  757.89699553,  849.20376345,  912.49660913,
        966.77707721, 1008.04667182, 1037.30685332, 1055.55903521,
       1065.80458132]
a1x = np.arange(len(a1y))

a2y=[1069.04480327, 1068.2809582 , 1064.51424672, 1057.74581119,
       1048.97673424, 1038.20803754, 1023.44068087, 1004.67556141,
        983.91351339,  959.15530785,  928.40165277,  890.65319339,
        844.9105128 ,  791.17413276,  730.4445147 ,  664.72206101,
        596.00711644,  526.29996976,  465.60085552,  409.90995604,
        354.22740345,  302.5532819 ,  256.88762985]
a2x= np.arange(len(a2y))

a3y=[ 216.23044249,  181.58167415,  149.94124091,  121.30902312,
         94.68486807,   69.06859264,   42.45998605,   14.85881254,
        -10.73518588,  -35.32228663,  -63.90278387,  -94.47698566,
       -127.0452113 , -160.60778851, -194.16505079, -227.71733472,
       -261.26497733, -294.8083135 , -327.34767345, -358.88338026,
       -385.41574752, -407.94507695, -433.47165627, -459.99575703,
       -486.51763258, -513.03751622, -537.55561935, -561.07212984,
       -582.58721047, -603.10099754, -622.61359958, -640.12509628,
       -657.63553747, -670.14494237, -681.6532989 , -696.16056319,
       -711.66665933, -727.17147916, -742.67488235, -758.17669657,
       -773.67671795, -789.17471161, -804.67041247, -821.16352618,
       -837.65373036, -849.1406759 , -855.62398858]
a3x=np.arange(len(a3y))

wave = [-731.11059984, -728.47093717, -713.86716911, -680.2975881 ,
       -618.76047483, -524.25410195, -396.7767379 , -240.32665095,
        -63.9021133 ,  122.49859465,  306.87718006,  478.23533331,
        630.57472375,  757.89699553,  849.20376345,  912.49660913,
        966.77707721, 1008.04667182, 1037.30685332, 1055.55903521,
       1065.80458132, 1069.04480327, 1068.2809582 , 1064.51424672,
       1057.74581119, 1048.97673424, 1038.20803754, 1023.44068087,
       1004.67556141,  983.91351339,  959.15530785,  928.40165277,
        890.65319339,  844.9105128 ,  791.17413276,  730.4445147 ,
        664.72206101,  596.00711644,  526.29996976,  465.60085552,
        409.90995604,  354.22740345,  302.5532819 ,  256.88762985,
        216.23044249,  181.58167415,  149.94124091,  121.30902312,
         94.68486807,   69.06859264,   42.45998605,   14.85881254,
        -10.73518588,  -35.32228663,  -63.90278387,  -94.47698566,
       -127.0452113 , -160.60778851, -194.16505079, -227.71733472,
       -261.26497733, -294.8083135 , -327.34767345, -358.88338026,
       -385.41574752, -407.94507695, -433.47165627, -459.99575703,
       -486.51763258, -513.03751622, -537.55561935, -561.07212984,
       -582.58721047, -603.10099754, -622.61359958, -640.12509628,
       -657.63553747, -670.14494237, -681.6532989 , -696.16056319,
       -711.66665933, -727.17147916, -742.67488235, -758.17669657,
       -773.67671795, -789.17471161, -804.67041247, -821.16352618,
       -837.65373036, -849.1406759 , -855.62398858]


%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import trapz, simps       


fig=plt.figure()
plt.subplot(2, 1, 1)
plt.plot(wave)
plt.title('Area under curve')

plt.subplot(2, 3, 4)
plt.plot(a1x,a1y)
plt.title('Area 1')

plt.subplot(2, 3, 5)
plt.plot(a2x,a2y)
plt.title('Area 2')

plt.subplot(2, 3, 6)
plt.plot(a3x,a3y)
plt.title('Area 3')

print ("Area under curve", simps(wave, np.arange(len(wave)))
print("area under graph A1", simps(a1y, a1x))
print("area under graph A2", simps(a2y, a2x))
print("area under graph A3", simps(a3y, a3x))

在此處輸入圖像描述

有誰知道如何計算“A2”(不是“Area2”)?

在此處輸入圖像描述

IIUC,您想計算由 wave 的最小值定義的水平線與每個塊的曲線之間的曲線面積嗎?

只需減去最小值:

M = min(wave)
print("Area under curve", simps(np.array(wave)-M, np.arange(len(wave))))
print("area under graph A1", simps(np.array(a1y)-M, a1x))
print("area under graph A2", simps(np.array(a2y)-M, a2x))
print("area under graph A3", simps(np.array(a3y)-M, a3x))

output:

Area under curve 81497.49533938
area under graph A1 21437.511782499998
area under graph A2 36243.834408466675
area under graph A3 20800.25109029666

注意。 這三個區域的總和略小於總區域,因為您在三個塊之間跳過了兩條細帶。 為避免這種情況,請確保一個塊的最后一個點是下一個塊的第一個點。

連續間隔:

Area under curve 81497.49533938
area under graph A1 23361.64246073833
area under graph A2 36243.834408466675
area under graph A3 21891.767255634997

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM