I am trying to integrate a numpy array which looks something like this:
I tried doing np.trapz(y=f, x=x)
but end up getting a negative value of -0.000802483, which was unexpected as f is never negative over the given x interval. I expect it to be a positive value. Am I doing something wrong here and does using np.trapz make sense here? I am quite inexperienced with numerical methods in python, any help would be greatly appreciated, thanks!
f array:
[8.50448832e-06, 1.05913683e-05, 1.11672674e-05, 1.03133123e-05,
8.55576374e-06, 6.51874371e-06, 4.66984459e-06, 3.23400327e-06,
2.23952788e-06, 1.61397514e-06, 1.26551646e-06, 1.11602722e-06,
1.09337264e-06, 1.16013046e-06, 1.29750464e-06, 1.48914243e-06,
1.71015364e-06, 1.92463843e-06, 2.11557374e-06, 2.27914844e-06,
2.41413389e-06, 2.52152682e-06, 2.60410241e-06, 2.66593017e-06,
2.71192544e-06, 2.74748305e-06, 2.77821789e-06, 2.80982042e-06,
2.84802547e-06, 2.89868017e-06, 2.96550880e-06, 3.04685482e-06,
3.14027584e-06, 3.24321794e-06, 3.35292462e-06, 3.46637434e-06,
3.58025198e-06, 3.69202940e-06, 3.80158930e-06, 3.90921139e-06,
4.01521443e-06, 4.11995070e-06, 4.22380119e-06, 4.32717165e-06,
4.43048947e-06, 4.53420149e-06, 4.63876310e-06, 4.74441156e-06,
4.85115094e-06, 4.95898043e-06, 5.06790572e-06, 5.17793927e-06,
5.28910054e-06, 5.40141637e-06, 5.51492115e-06, 5.62965722e-06,
5.74566570e-06, 5.86294288e-06, 5.98146531e-06, 6.10120449e-06,
6.22212681e-06, 6.34419345e-06, 6.46736032e-06, 6.59157794e-06,
6.71679139e-06, 6.84294018e-06, 6.96995819e-06, 7.09777352e-06,
7.22630845e-06, 7.35548870e-06, 7.48530158e-06, 7.61576302e-06,
7.74689094e-06, 7.87870523e-06, 8.01122775e-06, 8.14448236e-06,
8.27849500e-06, 8.41329367e-06, 8.54890851e-06, 8.68537178e-06,
8.82271792e-06, 8.96098359e-06, 9.10020767e-06, 9.24043128e-06,
9.38169220e-06, 9.52399166e-06, 9.66731438e-06, 9.81164377e-06,
9.95696193e-06, 1.01032497e-05, 1.02504864e-05, 1.03986503e-05,
1.05477182e-05, 1.06976654e-05, 1.08484660e-05, 1.10000927e-05,
1.11525166e-05, 1.13057075e-05, 1.14596338e-05, 1.16142624e-05,
1.17695588e-05, 1.19254869e-05, 1.20820259e-05, 1.22391888e-05,
1.23969940e-05, 1.25554608e-05, 1.27146099e-05, 1.28744630e-05,
1.30350432e-05, 1.31963744e-05, 1.33584820e-05, 1.35213926e-05,
1.36851339e-05, 1.38497349e-05, 1.40152258e-05, 1.41816381e-05,
1.43490045e-05, 1.45173591e-05, 1.46867371e-05, 1.48571751e-05,
1.50287105e-05, 1.52013618e-05, 1.53751190e-05, 1.55499688e-05,
1.57258974e-05, 1.59028901e-05, 1.60809314e-05, 1.62600049e-05,
1.64400935e-05, 1.66211790e-05, 1.68032427e-05, 1.69862648e-05,
1.71702248e-05, 1.73551011e-05, 1.75408716e-05, 1.77275129e-05,
1.79150010e-05, 1.81033110e-05, 1.82924169e-05, 1.84822921e-05,
1.86729088e-05, 1.88642383e-05, 1.90562544e-05, 1.92489505e-05,
1.94423280e-05, 1.96363887e-05, 1.98311346e-05, 2.00265678e-05,
2.02226907e-05, 2.04195058e-05, 2.06170156e-05, 2.08152233e-05,
2.10141318e-05, 2.12137445e-05, 2.14140649e-05, 2.16150966e-05,
2.18168436e-05, 2.20193100e-05, 2.22225000e-05, 2.24264183e-05,
2.26310696e-05, 2.28364587e-05, 2.30425910e-05, 2.32494716e-05,
2.34571064e-05, 2.36655022e-05, 2.38746665e-05, 2.40846075e-05,
2.42953332e-05, 2.45068523e-05, 2.47191735e-05, 2.49323059e-05,
2.51462588e-05, 2.53610420e-05, 2.55766653e-05, 2.57931389e-05,
2.60104733e-05, 2.62286795e-05, 2.64477684e-05, 2.66677514e-05,
2.68886403e-05, 2.71104471e-05, 2.73331840e-05, 2.75568637e-05,
2.77814991e-05, 2.80071034e-05, 2.82336902e-05, 2.84612731e-05,
2.86898648e-05, 2.89194782e-05, 2.91501262e-05, 2.93818220e-05,
2.96145791e-05, 2.98484114e-05, 3.00833329e-05, 3.03193579e-05,
3.05565011e-05, 3.07947774e-05, 3.10342019e-05, 3.12747902e-05,
3.15165580e-05, 3.17595214e-05, 3.20036966e-05, 3.22491003e-05,
3.24957494e-05, 3.27436611e-05, 3.29928530e-05, 3.32433427e-05,
3.34951484e-05, 3.37482885e-05, 3.40027817e-05, 3.42586461e-05,
3.45158800e-05, 3.47744606e-05, 3.50343633e-05, 3.52955627e-05,
3.55580326e-05, 3.58217460e-05, 3.60866750e-05, 3.63527911e-05,
3.66200649e-05, 3.68884659e-05, 3.71579632e-05, 3.74285249e-05,
3.77001182e-05, 3.79727094e-05, 3.82462641e-05, 3.85207471e-05,
3.87961220e-05, 3.90723520e-05, 3.93493990e-05, 3.96272243e-05,
3.99057882e-05, 4.01850501e-05, 4.04649684e-05, 4.07455009e-05,
4.10266042e-05, 4.13082348e-05, 4.15903804e-05, 4.18730703e-05,
4.21563372e-05, 4.24402148e-05, 4.27247376e-05, 4.30099411e-05,
4.32958616e-05, 4.35825364e-05, 4.38700036e-05, 4.41583023e-05,
4.44474726e-05, 4.47375553e-05, 4.50285924e-05, 4.53206267e-05,
4.56137020e-05, 4.59078630e-05, 4.62031555e-05, 4.64996261e-05,
4.67973225e-05, 4.70962935e-05, 4.73965885e-05, 4.76982583e-05,
4.80013546e-05, 4.83059299e-05, 4.86120380e-05, 4.89197251e-05,
4.92289986e-05, 4.95398538e-05, 4.98522856e-05, 5.01662890e-05,
5.04818583e-05, 5.07989878e-05, 5.11176714e-05, 5.14379028e-05,
5.17596752e-05, 5.20829817e-05, 5.24078151e-05, 5.27341678e-05,
5.30620321e-05, 5.33913998e-05, 5.37222624e-05, 5.40546112e-05,
5.43884373e-05, 5.47237312e-05, 5.50604834e-05, 5.53986839e-05,
5.57383224e-05, 5.60793884e-05, 5.64218710e-05, 5.67657590e-05,
5.71110410e-05, 5.74577047e-05, 5.78057281e-05, 5.81550770e-05,
5.85057158e-05, 5.88576079e-05, 5.92107163e-05, 5.95650029e-05,
5.99204289e-05, 6.02769547e-05, 6.06345400e-05, 6.09931435e-05,
6.13527232e-05, 6.17132363e-05, 6.20746392e-05, 6.24368873e-05,
6.27999353e-05, 6.31637371e-05, 6.35282455e-05, 6.38934127e-05,
6.42591899e-05, 6.46255276e-05, 6.49923751e-05, 6.53596810e-05,
6.57273931e-05, 6.60954580e-05, 6.64638218e-05, 6.68324292e-05,
6.72012496e-05, 6.75703534e-05, 6.79398379e-05, 6.83098025e-05,
6.86803487e-05, 6.90515798e-05, 6.94236014e-05, 6.97965210e-05,
7.01704482e-05, 7.05454949e-05, 7.09217748e-05, 7.12994040e-05,
7.16785006e-05, 7.20591849e-05, 7.24415794e-05, 7.28258086e-05,
7.32119995e-05, 7.36002812e-05, 7.39907848e-05, 7.43836438e-05,
7.47789940e-05, 7.51769734e-05, 7.55777221e-05, 7.59813826e-05,
7.63880996e-05, 7.67980127e-05, 7.72111298e-05, 7.76273428e-05,
7.80465374e-05, 7.84685970e-05, 7.88934026e-05, 7.93208325e-05,
7.97507629e-05, 8.01830673e-05, 8.06176169e-05, 8.10542803e-05,
8.14929237e-05, 8.19334107e-05, 8.23756026e-05, 8.28193580e-05,
8.32645329e-05, 8.37109809e-05, 8.41585530e-05, 8.46070975e-05,
8.50564601e-05, 8.55064839e-05, 8.59570094e-05, 8.64078743e-05,
8.68589136e-05, 8.73099596e-05, 8.77608418e-05, 8.82113882e-05,
8.86615329e-05, 8.91113973e-05, 8.95611241e-05, 9.00108582e-05,
9.04607475e-05, 9.09109426e-05, 9.13615966e-05, 9.18128656e-05,
9.22649084e-05, 9.27178865e-05, 9.31719644e-05, 9.36273094e-05,
9.40840916e-05, 9.45424841e-05, 9.50026628e-05, 9.54648067e-05,
9.59290976e-05, 9.63957202e-05, 9.68648626e-05, 9.73367153e-05,
9.78114722e-05, 9.82893303e-05, 9.87704892e-05, 9.92551521e-05,
9.97435237e-05, 1.00235716e-04, 1.00731671e-04, 1.01231313e-04,
1.01734564e-04, 1.02241344e-04, 1.02751571e-04, 1.03265163e-04,
1.03782036e-04, 1.04302101e-04, 1.04825273e-04, 1.05351460e-04,
1.05880572e-04, 1.06412515e-04, 1.06947194e-04, 1.07484514e-04,
1.08024375e-04, 1.08566678e-04, 1.09111320e-04, 1.09658199e-04,
1.10207209e-04, 1.10758243e-04, 1.11311192e-04, 1.11865946e-04,
1.12422391e-04, 1.12980414e-04, 1.13539922e-04, 1.14100904e-04,
1.14663365e-04, 1.15227312e-04, 1.15792750e-04, 1.16359685e-04,
1.16928123e-04, 1.17498070e-04, 1.18069533e-04, 1.18642517e-04,
1.19217029e-04, 1.19793076e-04, 1.20370663e-04, 1.20949798e-04,
1.21530488e-04, 1.22112738e-04, 1.22696556e-04, 1.23281949e-04,
1.23868924e-04, 1.24457488e-04, 1.25047648e-04, 1.25639411e-04,
1.26232785e-04, 1.26827778e-04, 1.27424399e-04, 1.28022682e-04,
1.28622678e-04, 1.29224438e-04, 1.29828015e-04, 1.30433462e-04,
1.31040833e-04, 1.31650183e-04, 1.32261567e-04, 1.32875041e-04,
1.33490664e-04, 1.34108492e-04, 1.34728584e-04, 1.35351001e-04,
1.35975801e-04, 1.36603048e-04, 1.37232802e-04, 1.37865127e-04,
1.38500087e-04, 1.39137746e-04, 1.39778169e-04, 1.40421423e-04,
1.41067575e-04, 1.41716694e-04, 1.42368825e-04, 1.43023960e-04,
1.43682077e-04, 1.44343158e-04, 1.45007183e-04, 1.45674130e-04,
1.46343979e-04, 1.47016707e-04, 1.47692293e-04, 1.48370715e-04,
1.49051948e-04, 1.49735970e-04, 1.50422756e-04, 1.51112282e-04,
1.51804523e-04, 1.52499454e-04, 1.53197049e-04, 1.53897280e-04,
1.54600122e-04, 1.55305547e-04, 1.56013526e-04, 1.56724032e-04,
1.57437035e-04, 1.58152529e-04, 1.58870541e-04, 1.59591101e-04,
1.60314238e-04, 1.61039982e-04, 1.61768365e-04, 1.62499418e-04,
1.63233172e-04, 1.63969659e-04, 1.64708911e-04, 1.65450960e-04,
1.66195841e-04, 1.66943585e-04, 1.67694227e-04, 1.68447801e-04,
1.69204341e-04, 1.69963883e-04, 1.70726461e-04, 1.71492111e-04,
1.72260869e-04, 1.73032773e-04, 1.73807858e-04, 1.74586144e-04,
1.75367629e-04, 1.76152305e-04, 1.76940166e-04, 1.77731206e-04,
1.78525418e-04, 1.79322794e-04, 1.80123328e-04, 1.80927011e-04,
1.81733837e-04, 1.82543796e-04, 1.83356882e-04, 1.84173084e-04,
1.84992395e-04, 1.85814805e-04, 1.86640305e-04, 1.87468886e-04,
1.88300539e-04, 1.89135252e-04, 1.89973016e-04, 1.90813821e-04,
1.91657686e-04, 1.92504708e-04, 1.93354998e-04, 1.94208665e-04,
1.95065822e-04, 1.95926585e-04, 1.96791067e-04, 1.97659386e-04,
1.98531660e-04, 1.99408011e-04, 2.00288559e-04, 2.01173427e-04,
2.02062741e-04, 2.02956628e-04, 2.03855214e-04, 2.04758630e-04,
2.05667007e-04, 2.06580478e-04, 2.07499178e-04, 2.08423242e-04,
2.09352789e-04, 2.10287784e-04, 2.11228127e-04, 2.12173718e-04,
2.13124451e-04, 2.14080222e-04, 2.15040923e-04, 2.16006445e-04,
2.16976678e-04, 2.17951508e-04, 2.18930822e-04, 2.19914503e-04,
2.20902434e-04, 2.21894493e-04, 2.22890560e-04, 2.23890511e-04,
2.24894220e-04]
x array:
[192.68860048, 192.18172021, 191.72558157, 191.31429202,
190.94296221, 190.60745222, 190.30419615, 190.03007711,
189.78233599, 189.55850331, 189.35634746, 189.17383451,
189.00909674, 188.86040749, 188.72616092, 188.60485555,
188.49508064, 188.39550495, 188.30486735, 188.22196886,
188.14566599, 188.07486501, 188.00851713, 187.94561432,
187.88518575, 187.82629479, 187.7680363 , 187.70953449,
187.64994097, 187.58843501, 187.52471988, 187.4596708 ,
187.39430761, 187.3296264 , 187.26660038, 187.20618083,
187.14929649, 187.09642682, 187.04703778, 187.00046645,
186.95606702, 186.91320987, 186.87128073, 186.82967999,
186.78782204, 186.74513475, 186.70107768, 186.6555782 ,
186.6090346 , 186.56185881, 186.51445461, 186.46721785,
186.4205366 , 186.37479146, 186.33035568, 186.28759548,
186.2468023 , 186.20790552, 186.17071973, 186.13506353,
186.10075942, 186.06763367, 186.03551617, 186.00424032,
185.97364285, 185.94356377, 185.91384622, 185.8843364 ,
185.85488347, 185.82535666, 185.79574633, 185.7660932 ,
185.73643756, 185.70681915, 185.67727713, 185.64785007,
185.61857602, 185.58949246, 185.56063631, 185.53204398,
185.50375134, 185.47579373, 185.44820599, 185.42102245,
185.3942708 , 185.36793712, 185.34199086, 185.316402 ,
185.29114105, 185.26617909, 185.24148772, 185.21703906,
185.19280573, 185.16876086, 185.14487802, 185.12113129,
185.09749519, 185.0739447 , 185.05045522, 185.02700259,
185.00356308, 184.98011344, 184.95664239, 184.93316263,
184.90968964, 184.88623867, 184.86282477, 184.83946279,
184.81616738, 184.79295299, 184.76983385, 184.74682402,
184.72393734, 184.7011875 , 184.67858795, 184.65615197,
184.63389268, 184.61182296, 184.58995557, 184.56830304,
184.54687752, 184.52568079, 184.50470008, 184.48392178,
184.46333244, 184.44291885, 184.422668 , 184.40256707,
184.38260346, 184.36276475, 184.3430387 , 184.32341329,
184.30387666, 184.28441713, 184.26502323, 184.24568362,
184.22638719, 184.20712296, 184.18788013, 184.16864807,
184.14941633, 184.13017459, 184.11091392, 184.09163324,
184.07233455, 184.05301986, 184.03369114, 184.01435034,
183.99499939, 183.97564022, 183.95627473, 183.9369048 ,
183.91753228, 183.89815902, 183.87878683, 183.85941754,
183.84005291, 183.82069471, 183.80134469, 183.78200458,
183.76267609, 183.74336089, 183.72406066, 183.70477706,
183.68551173, 183.66626656, 183.64704358, 183.6278448 ,
183.60867219, 183.58952772, 183.57041331, 183.55133087,
183.53228228, 183.5132694 , 183.49429406, 183.47535805,
183.45646317, 183.43761116, 183.41880377, 183.40004269,
183.38132962, 183.3626662 , 183.34405408, 183.32549485,
183.30699012, 183.28854144, 183.27015034, 183.25181831,
183.23354652, 183.21533608, 183.19718805, 183.1791035 ,
183.16108346, 183.14312895, 183.12524098, 183.10742052,
183.08966855, 183.07198601, 183.05437384, 183.03683294,
183.01936422, 183.00196856, 182.98464682, 182.96739984,
182.95022844, 182.93313345, 182.91611565, 182.89917582,
182.88231471, 182.86553307, 182.84883163, 182.83221094,
182.81566809, 182.79919663, 182.78279 , 182.76644177,
182.75014557, 182.73389514, 182.71768429, 182.70150693,
182.68535706, 182.66922874, 182.65311615, 182.63701353,
182.62091521, 182.60481561, 182.58870922, 182.57259062,
182.55645448, 182.54029553, 182.52410861, 182.50788861,
182.49163052, 182.47532939, 182.45898038, 182.4425787 ,
182.42611965, 182.40959872, 182.39301574, 182.3763761 ,
182.35968543, 182.34294934, 182.32617332, 182.3093628 ,
182.29252312, 182.27565956, 182.25877731, 182.24188148,
182.22497712, 182.20806919, 182.19116258, 182.1742621 ,
182.15737249, 182.14049841, 182.12364445, 182.10681513,
182.09001489, 182.0732481 , 182.05651905, 182.03983197,
182.02319101, 182.00660025, 181.99006371, 181.97358443,
181.95716127, 181.94079185, 181.92447382, 181.90820486,
181.8919827 , 181.87580506, 181.85966973, 181.84357449,
181.82751719, 181.81149566, 181.7955078 , 181.77955152,
181.76362476, 181.74772547, 181.73185167, 181.71600135,
181.70017259, 181.68436344, 181.66857201, 181.65279642,
181.63703484, 181.62128543, 181.60554641, 181.589816 ,
181.57409247, 181.55837406, 181.54265828, 181.52694172,
181.51122096, 181.49549266, 181.47975351, 181.46400028,
181.44822976, 181.43243883, 181.4166244 , 181.40078344,
181.38491298, 181.36901011, 181.35307194, 181.33709567,
181.32107853, 181.30501782, 181.28891088, 181.2727551 ,
181.25654793, 181.24028688, 181.22396949, 181.20759338,
181.19115619, 181.17465564, 181.15808949, 181.14145555,
181.12475331, 181.10798876, 181.09116943, 181.07430268,
181.05739579, 181.04045588, 181.02348997, 181.00650495,
180.98950756, 180.97250445, 180.95550213, 180.93850699,
180.92152529, 180.90456319, 180.8876267 , 180.87072172,
180.85385404, 180.8370293 , 180.82025306, 180.80353074,
180.78686763, 180.77026891, 180.75373966, 180.73728483,
180.72090924, 180.70461725, 180.68840633, 180.67226823,
180.65619464, 180.6401774 , 180.62420847, 180.60827997,
180.59238417, 180.57651346, 180.56066036, 180.54481757,
180.52897788, 180.51313424, 180.49727975, 180.48140762,
180.4655112 , 180.449584 , 180.43361963, 180.41761187,
180.4015546 , 180.38544186, 180.36926781, 180.35302676,
180.33671313, 180.32032151, 180.30384658, 180.28728325,
180.27063081, 180.25389595, 180.23708594, 180.22020794,
180.20326895, 180.18627582, 180.16923529, 180.15215395,
180.13503824, 180.11789447, 180.10072882, 180.08354732,
180.06635587, 180.04916023, 180.03196601, 180.01477872,
179.99760368, 179.98044613, 179.96331113, 179.94620363,
179.92912843, 179.91209022, 179.89509352, 179.87814276,
179.86124217, 179.84439292, 179.82759098, 179.81083186,
179.79411112, 179.77742445, 179.76076761, 179.74413641,
179.7275268 , 179.71093475, 179.69435636, 179.67778779,
179.66122528, 179.64466516, 179.62810383, 179.61153778,
179.59496356, 179.57837783, 179.5617773 , 179.54515878,
179.52851914, 179.51185535, 179.49516445, 179.47844355,
179.46168984, 179.44490059, 179.42807373, 179.41120912,
179.39430705, 179.3773678 , 179.36039166, 179.34337889,
179.32632976, 179.30924454, 179.2921235 , 179.27496689,
179.25777497, 179.24054799, 179.2232862 , 179.20598984,
179.18865916, 179.17129439, 179.15389577, 179.13646353,
179.11899788, 179.10149907, 179.08396729, 179.06640278,
179.04880574, 179.03117638, 179.01351494, 178.99582209,
178.97809874, 178.96034578, 178.9425641 , 178.92475451,
178.90691783, 178.88905484, 178.87116628, 178.85325289,
178.83531534, 178.81735431, 178.79937043, 178.78136431,
178.76333651, 178.7452876 , 178.72721808, 178.70912845,
178.69101917, 178.67289068, 178.65474338, 178.63657764,
178.61839382, 178.60019223, 178.58197287, 178.56373501,
178.5454778 , 178.52720042, 178.50890207, 178.49058198,
178.47223937, 178.4538735 , 178.43548366, 178.41706912,
178.39862922, 178.38016327, 178.36167064, 178.34315068,
178.3246028 , 178.30602639, 178.28742088, 178.26878573,
178.25012039, 178.23142435, 178.21269711, 178.19393819,
178.17514713, 178.15632369, 178.13746795, 178.11857998,
178.09965986, 178.08070765, 178.06172339, 178.04270713,
178.0236589 , 178.00457871, 177.98546659, 177.96632251,
177.94714649, 177.92793849, 177.90869848, 177.88942642,
177.87012227, 177.85078595, 177.83141739, 177.81201652,
177.79258324, 177.77311744, 177.75361901, 177.73408774,
177.71452325, 177.69492518, 177.67529317, 177.65562687,
177.63592595, 177.61619007, 177.59641892, 177.57661218,
177.55676955, 177.53689073, 177.51697545, 177.49702343,
177.4770344 , 177.45700811, 177.4369443 , 177.41684275,
177.39670321, 177.37652548, 177.35630934, 177.33605459,
177.31576113, 177.2954291 , 177.27505864, 177.25464985,
177.23420277, 177.21371743, 177.19319379, 177.1726318 ,
177.15203134, 177.13139227, 177.1107144 , 177.08999751,
177.06924133, 177.04844555, 177.02760982, 177.00673375,
176.98581692, 176.96485885, 176.94385905, 176.92281695,
176.90173195, 176.88060326, 176.85943008, 176.83821163,
176.81694721, 176.79563615, 176.77427785, 176.75287176,
176.73141736, 176.70991421, 176.68836189, 176.66676006,
176.64510842, 176.62340672, 176.60165476, 176.57985239,
176.55799952]
CodePudding user response:
The trapezoidal method sums over
0.5*(y[k] y[k 1])*(x[k 1]-x[k])
As for your given arrays the middle factor is always positive and the last factor always negative, the sum over it will also be negative.
This is also mathematically correct, an integral with a falling integration variable has the negative value of the same integral with the interval ends switched