Difference between Integral and trapz

7 views (last 30 days)
Kushal Kumar
Kushal Kumar on 3 Jun 2019
Commented: Laura B. on 12 May 2020
Hi, Thank you for viewing the question.
I have tried to do a comparison between Integral and trapz for a simple function of x.^2. with upper and lower limits as 9 and 0 its a simple sanity check I am trying to do
Intergal value for this function is 243.
To check trapz accuracy I ended up giving x and y values taken from the above function, like x = [0:1:9] and y = [0,1,4,9,16,25,36,49,64,81] and trapz is showing a value of 244.50.
May I know why there is such a difference and what other functions are available for me to get the integral of the above discrete data correct.

Answers (1)

Geoff Hayes
Geoff Hayes on 3 Jun 2019
Kushal - from trapz, this function computes the approximate integral of Y via the trapezoidal method with unit spacing. So it is an approximation only. If you increase the number of points, then you will achieve a better result...but it will still be an approximation.
>> x = linspace(0,9,1000);
>> y = x.^2;
>> trapz(x,y)
ans =
243.0001
  9 Comments
Walter Roberson
Walter Roberson on 12 May 2020
For example I have here 46 different solutions all with residue < 1e-35 and negative t0; they are pretty different
-47.95089469474497 -44.16157387672567 -4.371775917311408e-17 -6.079276847115207 4.943729409719964 1.525296255775328
-44.66652754395707 -40.99039637120786 3.219072511060688e-16 -35.07524122407148 -1.485631988687895 -37.3267490861478
-42.49369284069891 -33.36824153438488 -5.876084484599276e-16 -20.7220970568693 -6.631130369829794 -22.5200415144427
-39.60246938430808 -29.00547040120762 -9.008783210618455e-18 -44.37017392200255 37.51773697209195 20.64900789748739
-38.76733218357975 -27.53736287179044 1.95633494448283e-16 -27.04420347526322 38.3427893611626 29.50731063091068
-32.35030236700637 -44.22342446981496 7.696124218853795e-15 22.75562555373097 -28.81354322878946 38.47368113696579
-30.79096259697437 -39.31240264917558 -3.802437871397855e-17 30.09061096642649 33.14764502396599 -6.400996022989665
-28.6835626333566 -37.79637250183026 -7.863324162590477e-17 -8.325892219584183 23.39211157786728 -9.366610674357929
-27.18398939298427 -41.47949785723394 -7.307779354428176e-15 10.92562159387612 49.32891305205408 -34.28578267885976
-26.2630570676281 -41.32702114061822 2.899910469426467e-18 11.97516454846699 -0.1204558920673215 -15.44979170734793
-25.56376551258081 -41.20297330998476 -3.879057950853999e-16 34.31111433937419 -6.948801788373169 13.05008982760977
-21.09104489223759 -45.55670987515589 1.337713117534706e-15 46.07473728167103 30.10215154930555 -27.4941347444438
-20.66314154632227 -40.81783439430432 8.360680221621128e-17 7.115833060872347 -15.72781568903134 -37.48636675718868
-18.88399419417758 -21.01165722499711 7.878012455897127e-17 10.52232330824396 10.11113999687696 41.99995196407257
-18.85146902477141 -43.86053189465629 -3.733897662776094e-16 41.4169990417417 19.20895301694181 10.81056419661596
-17.58704378417622 -44.54512414505932 9.901140003394243e-18 -8.240610079726025 -8.606502456372805 29.94939480735483
-14.62213860603608 -39.07607120988833 6.061475975241885e-19 -19.13992953599639 7.311584594684017 48.32465144217963
-14.116587000359 -31.70843071280881 5.458746378811086e-17 -39.02076642249535 -4.209470305827821 -32.78342364806393
-13.94906263395042 -34.14047786354747 1.814247092464174e-20 -19.13734706566127 -0.6575426312010795 -34.07058063923574
-11.33832518581703 -47.61628978204907 -4.087467080660635e-16 7.377947886194208 -22.80215379729776 -34.17090547585008
-7.141132296424756 -42.59101911843941 -1.974276536125174e-17 -36.92908790787328 19.35219211044549 -33.74913795982584
-6.615278770910967 -11.67117383782504 1.71713275908378e-18 44.86253616579738 36.93601528487618 -42.85221771017089
-4.784741763825965 -31.74533170975311 -2.547280931346719e-19 1.99415587813591 -3.535305579321786 -1.525212988475015
-2.033138146692584 -49.29328043736207 -1.699556749705114e-17 -39.52741899514895 -0.7354480896091764 -41.31830628989966
-1.72572743839612 -49.81630755186946 -7.721634246451934e-15 -6.073369146843959 32.3210046466435 -41.16002879357249
1.088568230546769 -46.16264791890128 -2.731544944591147e-18 -7.144532923379474 4.93555211005385 -21.92310385920268
2.135102195658865 -47.11213549457229 7.798874518070057e-16 -46.6690414048627 -8.833919384687489 36.70807052907853
9.140373076117212 -35.96652732219512 -1.584900825845564e-16 2.586247179487001 -6.091027852139672 23.05532149934864
11.63814502658233 -44.00326128264724 -1.110180417589272e-16 -14.90637324973984 -8.406055711676384 -32.2512228552188
13.27488353827509 -33.70957936198759 3.511506537473809e-17 -28.54061172143811 17.62530237819364 -6.91878920002956
14.06921199707027 -27.2805934249703 2.999351415451131e-18 19.46689055536654 21.32776315293494 12.95742161358538
15.25929389392785 -26.54127555037479 2.889740046490772e-17 -12.85582751642021 -25.55018982923392 7.745501376908562
21.12087578244763 -32.8827316184443 2.245295062740123e-16 -45.80200926204462 -3.629914615216504 -31.76401510335888
21.15477829965959 -40.29292321223875 -3.025337565359654e-17 -2.264513604872063 -12.24001351897262 -7.123354087992601
22.04404343686652 -49.02299250830714 2.056397134640879e-16 -41.16718115133767 13.00497428557341 -7.038357782753925
23.22819701570493 -39.64721006849439 -2.868355240527429e-16 -33.05198553053757 44.2947341073166 -35.78347486499941
24.33482480185546 -47.36089989888285 -1.650901582034252e-16 40.50810939904423 39.15977121162014 -17.6588008327631
27.51208693277401 -35.92979712636892 1.441930129744566e-16 22.56754202729954 30.95710983410491 14.48949043902964
31.65526804343262 -40.48589155541801 -2.167506016113711e-16 13.68188229315157 45.45685585537677 -41.6738308551631
35.16997203781847 -39.14254710437893 1.026874825141096e-15 -3.809446159716262 47.6458077616564 -42.73097582180128
35.44205060306072 -24.8487185871428 -2.513978647861074e-16 -41.50254282593841 -4.831122920315394 32.41990274589189
39.51773491161424 -46.07703456319031 1.096582375254929e-16 -31.80977027629155 4.2894386737609 -18.24318607233912
41.53091425697731 -30.6618715438338 2.761911410748264e-16 -41.95851345397433 15.73575587891305 37.85200983109226
43.28175010459749 -45.80110014206731 -7.97962522378623e-17 41.27390133247491 -9.615874436055448 11.58684299485161
43.6499302816635 -34.19046136891242 -2.003832586568789e-17 31.75055731961091 24.49637898981904 19.95624478802585
44.28547380793928 -26.7597154467868 -2.637435267348304e-18 -9.563851502544852 -5.365026941691676 -28.26884625771725
Laura B.
Laura B. on 12 May 2020
Hello Walter,
First of all, Thank you !
If I understand correctly, the fit is not perfect, then the curves (theoretical and experimental) are different and therefore the area (and the two values of the integral) is different.
Unfortunately, there is no "optimal" predefined theoretical function for these experimental data and what I am looking for is the better solution.
There are no constraints on the parameters, except, maybe for D0 and t0 : the solutions should be as close as possible to the original ones.
Would you have any idea how to improve this fit?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!