Search code examples
rintegralcalculusnumerical-integration

Numerical integration in R : bad integrand behaviour error


This is my matrix

func=structure(c(-14.7690673280818, -14.5543581356252, -12.1406211639974, 
-10.7200919648493, -9.55507848352318, -9.20790894914246, -8.74647670464071, 
-8.26548763467919, -7.3962484443768, -6.94590909664862, -6.63903257406218, 
-6.40598087538929, -6.09621085755895, -5.88245015332806, -5.69615454359131, 
-5.4543516801245, -5.24485270997044, -5.05729667581875, -4.7660441793448, 
-4.39683466679171, -4.30668996362841, -4.0828498417172, -3.95528199743041, 
-3.81741171570944, -3.66175435590196, -3.56271256245504, -3.49609868983868, 
-3.37586125647396, -3.0034966287813, -2.90517151506904, -2.72993249556762, 
-2.66092057836039, -2.4408738658527, -2.36547969703899, -2.09546783234301, 
-1.95526398213212, -1.88470374007828, -1.72750243012653, -1.60575660667247, 
-1.43542615745848, -1.29362975715702, -1.18449733575892, -0.98947894198681, 
-0.853386354928181, -0.63296867063245, -0.527486151545778, -0.361277864998242, 
-0.257665212366851, -0.109270291541468, 0, 0.109270291541468, 
0.257665212366851, 0.361277864998242, 0.527486151545778, 0.63296867063245, 
0.853386354928181, 0.98947894198681, 1.18449733575892, 1.29362975715702, 
1.43542615745848, 1.60575660667247, 1.72750243012653, 1.88470374007828, 
1.95526398213212, 2.09546783234301, 2.36547969703899, 2.4408738658527, 
2.66092057836039, 2.72993249556762, 2.90517151506904, 3.0034966287813, 
3.37586125647396, 3.49609868983868, 3.56271256245504, 3.66175435590196, 
3.81741171570944, 3.95528199743041, 4.0828498417172, 4.30668996362841, 
4.39683466679171, 4.7660441793448, 5.05729667581875, 5.24485270997044, 
5.4543516801245, 5.69615454359131, 5.88245015332806, 6.09621085755895, 
6.40598087538929, 6.63903257406218, 6.94590909664862, 7.3962484443768, 
8.26548763467919, 8.74647670464071, 9.20790894914246, 9.55507848352318, 
10.7200919648493, 12.1406211639974, 14.5543581356252, 14.7690673280818, 
-13.7079821475458, -13.4498703618915, -11.67838231268, -10.4589486406842, 
-8.9432388382521, -8.58312548137122, -8.14102991846389, -7.71079209128989, 
-6.95240042017031, -6.66414427349685, -6.40033205538623, -6.13816822549321, 
-5.77208382991545, -5.49527630669887, -5.33154964830415, -5.12782641892739, 
-4.93206161073643, -4.72556232300793, -4.52080276980657, -4.1694078333758, 
-4.03956026669583, -3.90869860153294, -3.79104380757601, -3.69124767313376, 
-3.47159857371935, -3.3643163654408, -3.29467696235366, -3.16605701449292, 
-2.84435464789616, -2.71650210221017, -2.59690597248341, -2.52388797476942, 
-2.34094207608544, -2.20427426100811, -1.83879580218285, -1.72737794398069, 
-1.6454056454373, -1.5042411957216, -1.38609220687676, -1.2432788707001, 
-1.1225617148797, -1.01318771357089, -0.90038209504245, -0.773284479514442, 
-0.640368323748707, -0.555178375238413, -0.377358821158776, -0.230347860323025, 
-0.110436162251717, 0, 0.110436162251717, 0.230347860323025, 
0.377358821158776, 0.555178375238413, 0.640368323748707, 0.773284479514442, 
0.90038209504245, 1.01318771357089, 1.1225617148797, 1.2432788707001, 
1.38609220687676, 1.5042411957216, 1.6454056454373, 1.72737794398069, 
1.83879580218285, 2.20427426100811, 2.34094207608544, 2.52388797476942, 
2.59690597248341, 2.71650210221017, 2.84435464789616, 3.16605701449292, 
3.29467696235366, 3.3643163654408, 3.47159857371935, 3.69124767313376, 
3.79104380757601, 3.90869860153294, 4.03956026669583, 4.1694078333758, 
4.52080276980657, 4.72556232300793, 4.93206161073643, 5.12782641892739, 
5.33154964830415, 5.49527630669887, 5.77208382991545, 6.13816822549321, 
6.40033205538623, 6.66414427349685, 6.95240042017031, 7.71079209128989, 
8.14102991846389, 8.58312548137122, 8.9432388382521, 10.4589486406842, 
11.67838231268, 13.4498703618915, 13.7079821475458, -14.358609077878, 
-14.1271105147046, -11.9618138886078, -10.6190742201334, -9.31840132054895, 
-8.96622474353695, -8.51227248333579, -8.05091545299969, -7.22455526958365, 
-6.83691436333096, -6.54669635165921, -6.30238324376159, -5.97082920578328, 
-5.73268017960003, -5.55511488901834, -5.32804232266972, -5.12385612117062, 
-4.92897229198519, -4.67117775232928, -4.30885942992513, -4.20335651783943, 
-4.0154831357938, -3.89174994433905, -3.76860783727992, -3.58819663306074, 
-3.48596721027473, -3.41818297549918, -3.29470294456033, -2.94193593581688, 
-2.83218876260999, -2.67847401240583, -2.60791242809822, -2.40221737689215, 
-2.3031208003073, -1.99617971268181, -1.86711111167336, -1.79213635811389, 
-1.64113856670334, -1.52078410227216, -1.36109806339877, -1.22745572081485, 
-1.11822984936273, -0.955013720375685, -0.822400646878094, -0.635831069171161, 
-0.538198299721656, -0.367498441110635, -0.24709807532094, -0.109721283846771, 
0, 0.109721283846771, 0.24709807532094, 0.367498441110635, 0.538198299721656, 
0.635831069171161, 0.822400646878094, 0.955013720375685, 1.11822984936273, 
1.22745572081485, 1.36109806339877, 1.52078410227216, 1.64113856670334, 
1.79213635811389, 1.86711111167336, 1.99617971268181, 2.3031208003073, 
2.40221737689215, 2.60791242809822, 2.67847401240583, 2.83218876260999, 
2.94193593581688, 3.29470294456033, 3.41818297549918, 3.48596721027473, 
3.58819663306074, 3.76860783727992, 3.89174994433905, 4.0154831357938, 
4.20335651783943, 4.30885942992513, 4.67117775232928, 4.92897229198519, 
5.12385612117062, 5.32804232266972, 5.55511488901834, 5.73268017960003, 
5.97082920578328, 6.30238324376159, 6.54669635165921, 6.83691436333096, 
7.22455526958365, 8.05091545299969, 8.51227248333579, 8.96622474353695, 
9.31840132054895, 10.6190742201334, 11.9618138886078, 14.1271105147046, 
14.358609077878, -14.5672256639001, -14.3442603466748, -12.0526932005212, 
-10.6704167793823, -9.43869316924387, -9.08906143604798, -8.63130745231583, 
-8.15997238138419, -7.31181881941087, -6.89231124997616, -6.59362650222709, 
-6.3550370379524, -6.03455479085867, -5.80880119944129, -5.62679869904323, 
-5.39223941682433, -5.1853529853784, -4.99419352909143, -4.7193938866676, 
-4.35357309705323, -4.255876038818, -4.04972245780601, -3.92404029302206, 
-3.79341254883719, -3.62558255722496, -3.52497325621722, -3.4577838613747, 
-3.33595188997765, -2.97322433504632, -2.86928245642403, -2.70462793445213, 
-2.63485397290467, -2.42186464857093, -2.33481488889686, -2.04664318502277, 
-1.91191506158551, -1.8391839961936, -1.68503324864501, -1.56397162198931, 
-1.39887553095015, -1.26108886508264, -1.15191048988921, -0.972530768371285, 
-0.838149222100249, -0.634376246882453, -0.532753819606308, -0.364336815412981, 
-0.252468853112474, -0.109492065710681, 0, 0.109492065710681, 
0.252468853112474, 0.364336815412981, 0.532753819606308, 0.634376246882453, 
0.838149222100249, 0.972530768371285, 1.15191048988921, 1.26108886508264, 
1.39887553095015, 1.56397162198931, 1.68503324864501, 1.8391839961936, 
1.91191506158551, 2.04664318502277, 2.33481488889686, 2.42186464857093, 
2.63485397290467, 2.70462793445213, 2.86928245642403, 2.97322433504632, 
3.33595188997765, 3.4577838613747, 3.52497325621722, 3.62558255722496, 
3.79341254883719, 3.92404029302206, 4.04972245780601, 4.255876038818, 
4.35357309705323, 4.7193938866676, 4.99419352909143, 5.1853529853784, 
5.39223941682433, 5.62679869904323, 5.80880119944129, 6.03455479085867, 
6.3550370379524, 6.59362650222709, 6.89231124997616, 7.31181881941087, 
8.15997238138419, 8.63130745231583, 9.08906143604798, 9.43869316924387, 
10.6704167793823, 12.0526932005212, 14.3442603466748, 14.5672256639001, 
-13.6245844768687, -13.3630613956461, -11.6420519179073, -10.4384236674326, 
-8.89515033208928, -8.53401963497411, -8.0934438691888, -7.66719491633901, 
-6.91751548158816, -6.64199852086489, -6.38157100998465, -6.11711906602055, 
-5.74660855271358, -5.46484576488352, -5.30289294897202, -5.10216264876024, 
-4.90747729878566, -4.69948913603343, -4.50152763272595, -4.15153286112639, 
-4.01856478499323, -3.89501090892983, -3.77813524686729, -3.68133161034538, 
-3.45665297878437, -3.34872310204811, -3.27884590260074, -3.14956711773407, 
-2.83184663180451, -2.70167333082681, -2.5864505421683, -2.51311768021067, 
-2.33308777876224, -2.19160406439047, -1.81862225736669, -1.70946687980764, 
-1.6265976326061, -1.48669362532537, -1.36882733539455, -1.22817675030816, 
-1.10911635110033, -0.999723362435733, -0.89337938721453, -0.766988745723311, 
-0.640949911207503, -0.557354889428034, -0.37862272938278, -0.228200809764493, 
-0.110527795707098, 0, 0.110527795707098, 0.228200809764493, 
0.37862272938278, 0.557354889428034, 0.640949911207503, 0.766988745723311, 
0.89337938721453, 0.999723362435733, 1.10911635110033, 1.22817675030816, 
1.36882733539455, 1.48669362532537, 1.6265976326061, 1.70946687980764, 
1.81862225736669, 2.19160406439047, 2.33308777876224, 2.51311768021067, 
2.5864505421683, 2.70167333082681, 2.83184663180451, 3.14956711773407, 
3.27884590260074, 3.34872310204811, 3.45665297878437, 3.68133161034538, 
3.77813524686729, 3.89501090892983, 4.01856478499323, 4.15153286112639, 
4.50152763272595, 4.69948913603343, 4.90747729878566, 5.10216264876024, 
5.30289294897202, 5.46484576488352, 5.74660855271358, 6.11711906602055, 
6.38157100998465, 6.64199852086489, 6.91751548158816, 7.66719491633901, 
8.0934438691888, 8.53401963497411, 8.89515033208928, 10.4384236674326, 
11.6420519179073, 13.3630613956461, 13.6245844768687, -13.7513240158161, 
-13.4949850845108, -11.6972632618919, -10.4696154936035, -8.96823049188587, 
-8.60864584793023, -8.16576044465774, -7.73344959477095, -6.97053016408726, 
-6.67565344738059, -6.41008219291174, -6.14910749861269, -5.78532336166338, 
-5.51109109533802, -5.34644256890291, -5.14116391069274, -4.94483810678764, 
-4.73911258917317, -4.53082008138683, -4.17869747763421, -4.05047164341461, 
-3.91581211159757, -3.79775240187192, -3.6964010627146, -3.47936581711638, 
-3.37242020290272, -3.30290438286576, -3.17462683337767, -2.85085507867997, 
-2.72420863226898, -2.60233967200701, -2.52948530961352, -2.34502396371101, 
-2.21085897720345, -1.84928001732781, -1.73668634525457, -1.65518019199163, 
-1.51336068884896, -1.39506478104353, -1.25112746056258, -1.1295492863675, 
-1.02018515280999, -0.904021410598258, -0.776556379825624, -0.640066072057238, 
-0.554047238235075, -0.376701966557133, -0.231463685042374, -0.110388540236378, 
0, 0.110388540236378, 0.231463685042374, 0.376701966557133, 0.554047238235075, 
0.640066072057238, 0.776556379825624, 0.904021410598258, 1.02018515280999, 
1.1295492863675, 1.25112746056258, 1.39506478104353, 1.51336068884896, 
1.65518019199163, 1.73668634525457, 1.84928001732781, 2.21085897720345, 
2.34502396371101, 2.52948530961352, 2.60233967200701, 2.72420863226898, 
2.85085507867997, 3.17462683337767, 3.30290438286576, 3.37242020290272, 
3.47936581711638, 3.6964010627146, 3.79775240187192, 3.91581211159757, 
4.05047164341461, 4.17869747763421, 4.53082008138683, 4.73911258917317, 
4.94483810678764, 5.14116391069274, 5.34644256890291, 5.51109109533802, 
5.78532336166338, 6.14910749861269, 6.41008219291174, 6.67565344738059, 
6.97053016408726, 7.73344959477095, 8.16576044465774, 8.60864584793023, 
8.96823049188587, 10.4696154936035, 11.6972632618919, 13.4949850845108, 
13.7513240158161, -13.5038153563633, -13.2373523390222, -11.5894414578226, 
-10.4087012189772, -8.82551281645418, -8.46290889731311, -8.02453396711809, 
-7.60406134878737, -6.86699820314081, -6.6099290032563, -6.3544029256183, 
-6.08663753750477, -5.70971751250438, -5.42077894932731, -5.26139485499848, 
-5.06499864973269, -4.87187647554418, -4.66173225584577, -4.47361508632088, 
-4.12564790942336, -3.98816098787005, -3.87518960476143, -3.75944221250401, 
-3.66697204643782, -3.43501009186589, -3.32614231903036, -3.2559207639547, 
-3.12568790968636, -2.81373363068119, -2.68019961742924, -2.57130991397796, 
-2.49752109350884, -2.32171388099973, -2.17325620779303, -1.78940871644707, 
-1.68352966297731, -1.59936153416788, -1.46128278798956, -1.34382587758861, 
-1.20630719708568, -1.08964596607244, -0.980225481600834, -0.88323868593752, 
-0.757871821632761, -0.641792114656659, -0.560506724513046, -0.380453009333927, 
-0.225091641300559, -0.110660491161603, 0, 0.110660491161603, 
0.225091641300559, 0.380453009333927, 0.560506724513046, 0.641792114656659, 
0.757871821632761, 0.88323868593752, 0.980225481600834, 1.08964596607244, 
1.20630719708568, 1.34382587758861, 1.46128278798956, 1.59936153416788, 
1.68352966297731, 1.78940871644707, 2.17325620779303, 2.32171388099973, 
2.49752109350884, 2.57130991397796, 2.68019961742924, 2.81373363068119, 
3.12568790968636, 3.2559207639547, 3.32614231903036, 3.43501009186589, 
3.66697204643782, 3.75944221250401, 3.87518960476143, 3.98816098787005, 
4.12564790942336, 4.47361508632088, 4.66173225584577, 4.87187647554418, 
5.06499864973269, 5.26139485499848, 5.42077894932731, 5.70971751250438, 
6.08663753750477, 6.3544029256183, 6.6099290032563, 6.86699820314081, 
7.60406134878737, 8.02453396711809, 8.46290889731311, 8.82551281645418, 
10.4087012189772, 11.5894414578226, 13.2373523390222, 13.5038153563633, 
-12.8877947524831, -12.5961340486959, -11.3210853809037, -10.2570925911286, 
-8.47030492676585, -8.10018637873416, -7.67303749757961, -7.28202887686486, 
-6.60931905266524, -6.44634841642492, -6.21582362929168, -5.93115698481963, 
-5.5215432443443, -5.19600239869575, -5.04972087076752, -4.87543206990054, 
-4.69028336184037, -4.46914150055587, -4.33123842894087, -3.99361363184664, 
-3.833076928421, -3.77408485453713, -3.6640925546724, -3.59372660759739, 
-3.32461379011727, -3.21096198541142, -3.13898393694403, -3.00388455382712, 
-2.72134261347931, -2.57066623804085, -2.49408041291093, -2.41796583370138, 
-2.26369776447253, -2.07966723692233, -1.64375650300888, -1.54786821816088, 
-1.46043531083332, -1.33166687876378, -1.21629813646533, -1.09475471308712, 
-0.990331188483095, -0.880770453063485, -0.831512873094893, -0.711368103958197, 
-0.64608803628934, -0.576583643383317, -0.389788923647592, -0.219325408944189, 
-0.101244293663156, 0, 0.101244293663156, 0.219325408944189, 
0.389788923647592, 0.576583643383317, 0.64608803628934, 0.711368103958197, 
0.831512873094893, 0.880770453063485, 0.990331188483095, 1.09475471308712, 
1.21629813646533, 1.33166687876378, 1.46043531083332, 1.54786821816088, 
1.64375650300888, 2.07966723692233, 2.26369776447253, 2.41796583370138, 
2.49408041291093, 2.57066623804085, 2.72134261347931, 3.00388455382712, 
3.13898393694403, 3.21096198541142, 3.32461379011727, 3.59372660759739, 
3.6640925546724, 3.77408485453713, 3.833076928421, 3.99361363184664, 
4.33123842894087, 4.46914150055587, 4.69028336184037, 4.87543206990054, 
5.04972087076752, 5.19600239869575, 5.5215432443443, 5.93115698481963, 
6.21582362929168, 6.44634841642492, 6.60931905266524, 7.28202887686486, 
7.67303749757961, 8.10018637873416, 8.47030492676585, 10.2570925911286, 
11.3210853809037, 12.5961340486959, 12.8877947524831, -12.6000848203634, 
-12.2966556390103, -11.1957507602953, -10.1862843945321, -8.30440651819497, 
-7.93147284818737, -7.50817793772083, -7.13162491301438, -6.48897104737938, 
-6.36994876479492, -6.15110072929745, -5.85854042138457, -5.43365721329602, 
-5.09102141962306, -4.95085939235108, -4.78689576531572, -4.60547103236952, 
-4.37919276705198, -4.26474198682156, -3.93194755639431, -3.77843705689023, 
-3.70907277842741, -3.61955988253518, -3.55951762156004, -3.27305364378077, 
-3.15716747633446, -3.08436906473098, -2.94699678899349, -2.67819176248474, 
-2.51950911610342, -2.45801068672049, -2.38080987129559, -2.23660157184075, 
-2.03595688651075, -1.58836441712333, -1.47187389747016, -1.39555037998287, 
-1.27113029247092, -1.15673682064109, -1.0426545765059, -0.943946623196274, 
-0.835837575946843, -0.805837349555374, -0.689648730659341, -0.648094429117022, 
-0.584092303333525, -0.394149224677363, -0.222824659020402, -0.0906541511144734, 
0, 0.0906541511144734, 0.222824659020402, 0.394149224677363, 
0.584092303333525, 0.648094429117022, 0.689648730659341, 0.805837349555374, 
0.835837575946843, 0.943946623196274, 1.0426545765059, 1.15673682064109, 
1.27113029247092, 1.39555037998287, 1.47187389747016, 1.58836441712333, 
2.03595688651075, 2.23660157184075, 2.38080987129559, 2.45801068672049, 
2.51950911610342, 2.67819176248474, 2.94699678899349, 3.08436906473098, 
3.15716747633446, 3.27305364378077, 3.55951762156004, 3.61955988253518, 
3.70907277842741, 3.77843705689023, 3.93194755639431, 4.26474198682156, 
4.37919276705198, 4.60547103236952, 4.78689576531572, 4.95085939235108, 
5.09102141962306, 5.43365721329602, 5.85854042138457, 6.15110072929745, 
6.36994876479492, 6.48897104737938, 7.13162491301438, 7.50817793772083, 
7.93147284818737, 8.30440651819497, 10.1862843945321, 11.1957507602953, 
12.2966556390103, 12.6000848203634, -12.7233949898779, -12.425009683628, 
-11.2494681735225, -10.2166322197219, -8.3755092456272, -8.00338523998922, 
-7.57923229725886, -7.19608684570266, -6.5405512372713, -6.40269304276938, 
-6.17884044516668, -5.8896632980985, -5.47132446137006, -5.13601542532135, 
-4.9932306291456, -4.82484171541241, -4.64182091659811, -4.41774407285591, 
-4.29324182862204, -3.95837714178017, -3.80090594237893, -3.73788575810611, 
-3.6386462286676, -3.57417931862452, -3.29515190899568, -3.18022337150066, 
-3.10777656095811, -2.97137842876953, -2.69668587115743, -2.54143464829308, 
-2.47346988209172, -2.39673461879726, -2.24821478269518, -2.05469079214647, 
-1.61210502089736, -1.50444445385474, -1.42335954097761, -1.29707578786137, 
-1.18226432293022, -1.06498427720905, -0.963826675880699, -0.85422851115692, 
-0.817708608597364, -0.698957479868998, -0.647234505242058, -0.580874151897233, 
-0.392280434624987, -0.221324908501217, -0.0951930012794266, 
0, 0.0951930012794266, 0.221324908501217, 0.392280434624987, 
0.580874151897233, 0.647234505242058, 0.698957479868998, 0.817708608597364, 
0.85422851115692, 0.963826675880699, 1.06498427720905, 1.18226432293022, 
1.29707578786137, 1.42335954097761, 1.50444445385474, 1.61210502089736, 
2.05469079214647, 2.24821478269518, 2.39673461879726, 2.47346988209172, 
2.54143464829308, 2.69668587115743, 2.97137842876953, 3.10777656095811, 
3.18022337150066, 3.29515190899568, 3.57417931862452, 3.6386462286676, 
3.73788575810611, 3.80090594237893, 3.95837714178017, 4.29324182862204, 
4.41774407285591, 4.64182091659811, 4.82484171541241, 4.9932306291456, 
5.13601542532135, 5.47132446137006, 5.8896632980985, 6.17884044516668, 
6.40269304276938, 6.5405512372713, 7.19608684570266, 7.57923229725886, 
8.00338523998922, 8.3755092456272, 10.2166322197219, 11.2494681735225, 
12.425009683628, 12.7233949898779, -12.9701665831908, -12.6818752140478, 
-11.3569688906404, -10.2773650952434, -8.51780191626772, -8.14868819459235, 
-7.72003821070641, -7.32508978114711, -6.64377488616525, -6.46822176338875, 
-6.23435390289731, -5.95194722735932, -5.54670516078474, -5.22605862710606, 
-5.07802507599128, -4.90078016073291, -4.71456527242172, -4.49489397213016, 
-4.35027647059818, -4.01126873151645, -3.85381415345849, -3.78760418054142, 
-3.67684233255883, -3.6035206970325, -3.33937554552141, -3.22636344257365, 
-3.15462026543263, -3.02017161524238, -2.73369677368098, -2.58531260689116, 
-2.50440723533775, -2.42860364735808, -2.27145544935968, -2.09218158273497, 
-1.66032132882236, -1.56891953844307, -1.47901197414181, -1.34899860381407, 
-1.23335063996306, -1.10967106859385, -1.00361116644947, -0.894069184830091, 
-0.838429443534018, -0.717586396553344, -0.645513602695347, -0.574433901587114, 
-0.388540562231589, -0.218323567844169, -0.104276269037671, 0, 
0.104276269037671, 0.218323567844169, 0.388540562231589, 0.574433901587114, 
0.645513602695347, 0.717586396553344, 0.838429443534018, 0.894069184830091, 
1.00361116644947, 1.10967106859385, 1.23335063996306, 1.34899860381407, 
1.47901197414181, 1.56891953844307, 1.66032132882236, 2.09218158273497, 
2.27145544935968, 2.42860364735808, 2.50440723533775, 2.58531260689116, 
2.73369677368098, 3.02017161524238, 3.15462026543263, 3.22636344257365, 
3.33937554552141, 3.6035206970325, 3.67684233255883, 3.78760418054142, 
3.85381415345849, 4.01126873151645, 4.35027647059818, 4.49489397213016, 
4.71456527242172, 4.90078016073291, 5.07802507599128, 5.22605862710606, 
5.54670516078474, 5.95194722735932, 6.23435390289731, 6.46822176338875, 
6.64377488616525, 7.32508978114711, 7.72003821070641, 8.14868819459235, 
8.51780191626772, 10.2773650952434, 11.3569688906404, 12.6818752140478, 
12.9701665831908), .Dim = c(99L, 11L))

Each column is a function for me that I want to integrate, and put the values in my integr matrix:

 integr=matrix(0,11)

 for (t in 1:11){
     integrating = approxfun(thau,func[,t],rule=2)
     integr[t,1]=integrate(integrating, lower = 0.01, upper = 0.5,subdivisions = 1000)$value
}

I have this error message:

Error in integrate(integrating, lower = 0.01, upper = 0.5, subdivisions = 1000) : 
  extremely bad integrand behaviour

How do I get around this problem and continue to use the function integrate in R. Will I need to do some non-linear approach? If so, how can I do it?

Many thanks.


Solution

  • First, it is very interesting to see quite a few numerical integration question regarding quantile function in the last 10 days or so. For example:

    Note how this question is similar to the first one. Although you did not mention what thau is, I believe it is thau <- seq(0.01, 0.99, 0.01). Let's have sketch your func matrix against thau:

    matplot(thau, func, type = "l")
    

    enter image description here

    Also, let's verify that all columns of func are monotonically increasing:

    all(diff(func) > 0)
    # [1] TRUE
    

    Basically, your question is using the answer I provided in the 1st linked question (the justification of rule = 2 is given in the 3rd linked question, though). But thanks to your question; I now realize there are some potential numerical flaw behind.


    It is sophisticated for me to understand the mathematics behind Adaptive Quadrature in a limited time as I am not an expert in the field. But it is rather surprising to me that it would fail sometime on such a trivial task. As I mentioned in the 2nd linked question, we can even use trapezoidal rule.

    When I test integrate, it is the 1st column of func rather than the 5th as you reported that fails.

    ## get interpolation function for all columns in a list
    flst <- lapply(1:ncol(func), function (i) approxfun(thau, func[,i], rule = 2))
    
    ## all OK excluding the 1st column
    sapply(flst[-1], function (fun) integrate(fun, 0.01, 0.5)$value)
    # [1] -2.010421 -2.088981 -2.114083 -2.000653 -2.015932 -1.986130 -1.912076
    # [8] -1.877459 -1.892291 -1.921983
    
    ## the 1st one fails
    integrate(flst[[1]], 0.01, 0.5)
    #  extremely bad integrand behaviour
    

    As said earlier, I believe this failure artificial due to the problem's simple nature. In fact, let's consider

    integrate(flst[[1]], 0.01 + .Machine$double.eps ^ 0.25, 0.5)
    # -2.13653 with absolute error < 8.7e-05
    
    integrate(flst[[1]], 0, 0.5)
    # -2.286034 with absolute error < 0.00027
    

    They all work.


    As far as I can explore, the integrate function is using two Fortran subroutines:

    • dqags for definite integral, capable to deal with end-points singularity;
    • dqagi for indefinite integral.

    R documentation for integrate does not explain much on the error handling of those routines, but the Fortran page does a little. Unfortunately, it is still not extremely clear what the "bad behaviour" is. But it is clear enough to see that regardless what error code is, those Fortran subroutines will always return integration result.

    A look at the source code of integrate verifies this. Integration result is stored in variable wk, then a swtich statement is used to interpret the integer error code stored in wk$ierr:

    res$message <- switch(wk$ierr + 1L, "OK", "maximum number of subdivisions reached", 
        "roundoff error was detected", "extremely bad integrand behaviour", 
        "roundoff error is detected in the extrapolation table", 
        "the integral is probably divergent", "the input is invalid")
    if (wk$ierr == 6L || (wk$ierr > 0L && stop.on.error)) 
        stop(res$message)
    

    The if statement following this switch decides whether we want to ignore any error. Note there is an stop.on.error argument in integrate; if we set it FALSE instead of the default TRUE, integrate will always work. Therefore, let's do

    z <- integrate(flst[[1]], 0.01, 0.5, stop.on.error = FALSE)
    
    str(z)
    # $ value       : num -2.14
    # $ abs.error   : num 0.000446
    # $ subdivisions: int 69
    # $ message     : chr "extremely bad integrand behaviour"
    # $ call        : language integrate(f = flst[[1]], lower = 0.01, upper = 0.5, stop.on.error = FALSE)
    # - attr(*, "class")= chr "integrate"
    
    z$value
    # [1] -2.138348
    

    This is all I can do at the moment. I believe there will be an opportunity for me to read around Adaptive Quadrature in the near future.