Message Boards Message Boards


Calculate a numerical integration series of rectangles

Posted 13 days ago
9 Replies
2 Total Likes

I have this problem:


QQ[t_] := If[EvenQ[Floor[t]], 0, 1]

which is a series of rectangle pulses.

Now we integrate QQ[t] from zero to some X.

qq[X_] := Module[{u}, NIntegrate[QQ[u], {u, 0, X}]]

Result is a straight line with slope equal to 1, and this is not true.

Where is my mistake?


9 Replies

I tried some Options, but with no result. Seems indeed to be a bug. A very brute and simple method yields a reasonable result:

qq1[X_, n_] := Sum[X/n QQ[j X/n], {j, 0, n}]
Plot[qq1[x, 50 x], {x, 0, 10}]

Please do not ask me why this code works and your version did not.

QQ[x_] := 1 - SquareWave[{0, 1}, .5 x]
qq[X_] := NIntegrate[QQ[v], {v, 0, X}]

When I plot the graph, I get what one would expect, horizontal pieces on the unit intervals beginning with an even number and line segments with slope one on the unit intervals beginning with odd numbers for a function that is nondecreasing.

Interesting: this works as well

f[x_] := If[Sin[x] < 0, 0, Sin[x]]
Plot[NIntegrate[f[u], {u, 0, uu}], {uu, 0, 20}]
Posted 13 days ago

This seems to work. It throws a warning when I plotted it, but then it plots the right function.

QQ[t_] := Hold[If[EvenQ[Floor[t]], 0, 1]]

The Hold seems to be necessary. I wish I knew why.

Hold doesn't work for me. The plot has no content, just the axes. This is a really strange problem.

This worked correctly:

f[x_] := Piecewise[{{0, 0 <= x < 1}, {1, 1 <= x < 2}, {0, 
    2 <= x < 3}, {1, 3 <= x < 4}, {0, 4 <= x < 5}}]

g[z_] := NIntegrate[f[t], {t, 0, z}]

Please try the attached. Again, it throws a warning and then it works.

Apparently one must avoid that in the context of NIntegrate the function QQ[] sees/evaluates any other argument than a numeric one. This works:

QQ[t_?NumericQ] := If[EvenQ[Floor[t]], 0, 1]
qq[X_] := NIntegrate[QQ[v], {v, 0, X}, AccuracyGoal -> 10]
Plot[qq[u], {u, 0, 5}]

Hold is probably doing something similar by inhibiting the evaluation of something nonnumeric (?).. (With AccuracyGoal -> 10 I am just suppressing the error message.) Does that make sense?

I would like to greet all of you. Your comments and suggestions are very valuable, because my "mother milk" was FORTRAN IV on IBM7040. Now, after retirement, I have Mathematica Home Edition and this is a different kettle of fish.

My experiments:

NIntegrate[Hold[If[EvenQ[Floor[t]], 0, 1]], {t, 0, A}]          works

NIntegrate[If[EvenQ[Floor[t]], 0, 1], {t, 0, A}]                    does not work

NIntegrate[Hold[If[Mod[Floor[t], 2] == 0, 0, 1]], {t, 0, A}]   works

NIntegrate[If[Mod[Floor[t], 2] == 0, 0, 1], {t, 0, A}]             works too

It seems to me that problem is connected with function EvenQ as can be deduced from yours experiments too..

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract