# Calculate a numerical integration series of rectangles

Posted 13 days ago
287 Views
|
9 Replies
|
2 Total Likes
|
 I have this problem:Let 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?Vladimir Answer
9 Replies
Sort By:
Posted 13 days ago
 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}] Answer
Posted 13 days ago
 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. Answer
Posted 13 days ago
 Interesting: this works as well f[x_] := If[Sin[x] < 0, 0, Sin[x]] Plot[NIntegrate[f[u], {u, 0, uu}], {uu, 0, 20}] Answer
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. Answer
Posted 13 days ago
 Hold doesn't work for me. The plot has no content, just the axes. This is a really strange problem. Answer
Posted 13 days ago
 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}] Answer
Posted 12 days ago
 Please try the attached. Again, it throws a warning and then it works. Answer
Posted 12 days ago
 Apparently one must avoid that in the context of NIntegrate the function QQ[] sees/evaluates any other argument than a numeric one. This works: ClearAll[QQ] 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? Answer
Posted 11 days ago
 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.. Answer