Message Boards Message Boards

0
|
3491 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Perform a numerical integration using trapezoid method?

I wrote a code that uses the trapezoid method to calculate integrals numerically.

I wanted to see how accurate the formula that gives the upper bound of the fault made.

This formula is:

I-T{2n}\approx (1/3)*(T{2n}-T_n) With I the actual value of the integral, and T_k the value that the trapezoid method gives for k partitions.

I plotted the difference between the actual fault (aka 'the fault on the fault formula') and what the formula tells us against the number of partitions for \int\limits_2^5 x^2 dx = 39.

3 weird things happened: 1) The fault seems REALLY, maybe too small. It could be true, but it seems weird. Some values even pop up at (seemingly?) zero.

2) The freaky things are the lines that are starting to form. They appear for every upper number of partitions, the bigger, the clearer.

3) When running the code for a more complicated integral, it behaved like you'd expect: the y-values get smaller as n gets bigger.

What on Earth could be the cause of this, and why!?

POSTED BY: Nio Totté
4 Replies

It looks like numerical noise due to rounding errors in the last digits. The plot amplifies it out of proportion.

POSTED BY: Gianluca Gorni

I figured that when I decrease the amount of partitions, I'd get a less accurate result and therefore less/no chance on rounding errors. No idea if that's correct though. I tried it with n going from 1 to 10, and it still shows the same type of stuff. If it's rounding, how could I fix it? I think I recall my prof saying Mathematica remembers everything exactly?

POSTED BY: Nio Totté
Posted 4 years ago

Hi Nio,

Use exact values

intsom[f_, n_, a_, b_] := 
 Module[{t = 0, fen = {}},
  While[t < n + 1, c = f[a + ((b - a)/n)*t]; fen = Append[fen, c]; t++];
  T = ((b - a)/n)*(Total[fen] - 1/2*(f[a] + f[b]))]

list1 = {};
list2 = {};

Do[list1 = 
  Append[list1, 
   Abs[39 - intsom[g, 20*n, 2, 5] - (intsom[g, 20*n, 2, 5] - intsom[g, 10*n, 2, 5])/3]], {n, 10, 100}]
ListPlot[list1]

enter image description here

POSTED BY: Rohit Namjoshi

Hi!! Thanks for the input. Very good point; I didn't think of that. Unfortunately, it doesn't work. That graph is meaningless and when I let my computer show list1, it just shows zeroes. I tried trimming down the range of y, but it never shows anything, except maybe a vague line I can't even be sure of if it indeed is there at y=0. I went through a bunch of intervals from [-10,10] to [-10^(-16),10^(-16)]. Any ideas of what it might be, or am I just misunderstanding this?

POSTED BY: Nio Totté
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract