# Tag Info

13

The correct syntax is NIntegrate[1, {x} ∈ ImplicitRegion[(x > 5 && x < 9) || (x > 11 && x < 13), x], Method -> "MonteCarlo"] The {x} has moved out in front. Alternatively you can do: NIntegrate[Boole[(x > 5 && x < 9) || (x > 11 && x < 13)], {x, 5, 13}, Method -> "MonteCarlo"] Also, if you ...

8

It has to do with the default behavior of GenerateConditions in multivariate integrals. Setting it explicitly to True will help in this case. Some explanation may be found here or here. The gist is that, for multiple integration, automatically checking conditions and issuing provisos for all but the final integration is typically both too costly (in speed) ...

7

The reason why you get a factor of 500 of I have explained in my comment. Let's replace your g with a better behaved function: mass = 100; width = 10^-2; g[x_] := mass/width HeavisideLambda[x/width] This is a triangular peak with area 100 and basewidth 0.01. Now let's impose the desired boundary conditions of $\partial u / \partial x =0$ at the ...

5

I wonder if this is a bug that appears when using regions and {"MonteCarlo"} as a method. It hangs my machine. This might be a possible workaround: NIntegrate[1, {x, y} \[Element] Triangle[{{0,0},{1,2},{2,1}}], Method-> "MonteCarlo", MaxPoints -> 10^5}] There isn't mention of the new arbitrary region functionality with the "MaxPoints" option or ...

5

This s not an answer but an extended comment about results with v10.1 $Version "10.1.0 for Mac OS X x86 (64-bit) (March 24, 2015)" Integrate[1/(x y z), {x, 0, 1}, {y, 0, 1}, {z, 0, 1}] 0 Integrate[1./(x y z), {x, 0, 1}, {y, 0, 1}, {z, 0, 1}] 0 However,NIntegrate gives the a large result in either case with convergence warnings ... 5 To illustrate whats going on, your function is 0 at x=0, rises to a max and becomes essentially zero very quickly. With[{a = .9}, Plot[x Exp[-(a^2+.001^2) x^2], {x, 0, 3}]] Now look at the values NIntegrate computes: res = Reap[With[{a = .9}, NIntegrate[y = x Exp[-(a^2 + 0.001^2) x^2], {x, 0, 8000}, EvaluationMonitor :> Sow[{x, ... 4 First note that, $$\oint_{E(\vec{x})=\epsilon}\frac{dS}{|\nabla E(\vec{x})|} =\oint_{E(\vec{x})=\epsilon}\frac{\nabla E(\vec{x})\cdot d\vec{S}}{|\nabla E(\vec{x})|^2} = \int_{E(\vec{x})\leq\epsilon} \nabla\cdot\left( \frac{\nabla E(\vec{x})}{|\nabla E(\vec{x})|^2}\right) dV$$ By now, the volume integral can be evaluated by Integrate[f ... 3 Elvira, I am going to put here what I had put together for an answer to your question. There is still something puzzling about your question though, and that is the fact that you are essentially recalculating the same integral twice, it seems to me. Here is what I mean: $$\text{firstIntegral}=\int_{x1}^{x2} \! f(x) \, \mathrm{d}x$$ Then you are looking ... 3 Sometimes it is just a matter of adjusting the options. Here is a modification of your code that gives an answer fairly quickly and without any messages, using NIntegrate and some of its options: P = 0; l = 0; x = 4; κ = 0.0120; (* note increased precision *) n = 5; q = (κ*n)/2; Do[Q = Exp[((-I)*(MathieuCharacteristicA[ν, q] - x^2/4)*τ/2)]* ... 3 The problem actually seems to be caused by the very first pair of integers - (2,3) This is because there is actually a singularity in the function here: Plot[Log[n Log[n Log[n]]]-1/Log[n Log[n Log[n]]],{n,2,3}] If we ask Integrate to symbolically evaluate the integral between these bounds, it tells us that is does not converge: Integrate[Log[n Log[n ... 3 Here are some reasons or surmises: I believe some functions are special-cased in NIntegrate; I'm pretty sure this is true for low-degree polynomials. To get the advantage of compiling, use f = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, Evaluate@g[x, y, z]], but it will still be slower than just using g. Without the Evaluate, the compiled function makes ... 3 NIntegrate does each integral separately This has been observed before: NIntegrate piecewise vector function, Nested NIntegrate of vector function. It is also clear from the following BenchmarkPlot: int[n_] := Block[{shaxis}, shaxis = Table[1.0*i, {i, 1, n}]; NIntegrate[shaxis/(x^3 + 10), {x, 0, Infinity}, Method -> {"GlobalAdaptive", Method ... 3 The reason for the error message is that C is a reserved system symbol. http://reference.wolfram.com/language/ref/C.html Since C has the attribute Protected, no further definitions can be made for it. http://reference.wolfram.com/language/ref/Protected.html 3 Why not {sol} = NDSolve[{f'[x] == g[x], g'[x] == f[x], f[0] == 1, g[0] == 0}, {f, g}, {x, 0, 1}] omitting the definition of f? Check: {Inactivate@NIntegrate[g[x0], {x0, 0, x}], f[x]} /. sol /. x -> 0.5 // Activate (* {0.521095, 0.521095} *) 3 The differential equation, its initial condition, and its boundary conditions are translationally invariant in space. Consequently, the solution must be independent of x and y. Indeed, solving the equations as given in the Question does give a spatially constant solution that oscillates in time. For instance, DensityPlot[Evaluate[Re[A[x, y, 10000]] /. ... 3 Treat the maximum machine number as a singularity: ListPlot[Table[{a, NIntegrate[x Exp[-(a^2 + 0.001^2) x^2], {x, 0, 0.5 Log[$MaxMachineNumber/(a^2 + 0.001^2)], 8000}]}, {a, 0.001, 1, 1/100}], Joined -> True Update [Forgive me, I actually have a job, and, while I could solve the problem quickly over breakfast, I could not compose a complete ...

2

By using the output of ListContourPlot3D and the new Mathematica 10.0 feature DiscretizeGraphics, one can nicely generate a meshed contour region which is suitable for NIntegrate. We can show this for the above example for energy contours from 1.0 to 2.0 with a step width of 0.1: Monitor[Table[ e = ListInterpolation[data, {{-1, 1}, {-1, 1}, {-1, 1}}]; f ...

2

The code in the Question runs slowly, because it evaluates n^2 integrals. However, all the integrands are of the form Dc Exp[-Ax (x - a1)^2] Exp[-Ay (y - b1)^2] Exp[-Ax (x - a2)^2] Exp[-Ay (y - b2)^2]; This generic term can be integrated symbolically in several seconds Integrate[Laplacian[%, {x, y}], {x, -a, a}, {y, -b, b}] (* Dc (E^((-2*a^2 - a1^2 - ...

2

No fair, you let NIntegrate see the symbolic form of the native expression. If you do the same trick: f3[x_?NumericQ, y_?NumericQ, z_?NumericQ] := g[x, y, z]; NIntegrate[g[x, y, z], {x, 0, 100}, {y, 0, 10}, {z, 0, 9}] // Timing NIntegrate[f2[x, y, z], {x, 0, 100}, {y, 0, 10}, {z, 0, 9}] // Timing NIntegrate[f3[x, y, z], {x, 0, 100}, {y, 0, 10}, ...

2

One way to improve on the error estimates is to incrementally integrate over each successive pair of integers. Then sum the partial results. Quiet[vals = NIntegrate[ PowerExpand[Log[n Log[n Log[n]]] - 1/(Log[n Log[n Log[n]]]), Assumptions -> n > 2], {n, #, # + 1}] & /@ Range[2, 100];] sums = Accumulate[vals] Now use sums in that plot. ...

1

This is a stab at cleaning it up. I put in table form so you can see how to loop over a. Needs["NumericalCalculus"] Table[ g[t_] = {-(a + 2*Cos[2*t])*Sin[3*t], (a + 2*Cos[2*t])*Cos[3*t], 2*Sin[2*t]}; dg[t_] = If[t - 2*Pi <= 0, g'[t], g'[2*Pi]]; tfn = Module[{s}, NDSolveValue[{t'[s] == 1/Norm[dg[t[s]]], t[0] == 0, ...

1

Here is my second stab at the problem. The first time I tried this, I was using Interpolation to find the expression of the parabolas, but the Manipulate wrapper was quite sluggish. @GuessWhoItIs. pointed out that InterpolatingPolynomials might be a snappier choice in this case. As I understand it, this function constructs a Newton divided difference ...

1

As Fred Simons comments NIntegrate has the HoldAll attribute but alone that does not explain this behavior. With the literal assignment z = 1 no NIntegrate::nlim message prints: z = 1; NIntegrate[f[x], {x, 0, z}] Table normally works by the same mechanism as Block, and indeed we see the same behavior from Block: ClearAll[f, x, z] Block[{z = 1}, ...

1

With RuntimeOptions -> "EvaluateSymbolically" -> False and Evaluate you don't need an intermediate function and get 3x speedup: f = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, g[x, y, z]]; f2[x_Real, y_Real, z_Real] := f[x, y, z]; f3 = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, Evaluate@g[x, y, z], RuntimeOptions -> ...

1

You are almost there. You only missed to create the list that you want to plot. Here are your definitions: (*parameters*) \[CapitalOmega]m = 1.0; \[CapitalOmega]\[CapitalLambda] = 0.0; \[CapitalOmega]k = 1 - \[CapitalOmega]m - \[CapitalOmega]\[CapitalLambda]; (*Integral with variable limits*) A[a_?NumericQ] := (5 \[CapitalOmega]m)/ 2 ...

1

y1 = 0; y2 = 1; n = 50; y = Range[y1,y2, (y2-y1)/(n-1.)]; f[x_, y_] := y Sin[x] If your function is Listable you can do this points = {y, NIntegrate[f[x, y], {x, 0, Pi}]} // Transpose; ListLinePlot[points] If your function is not Listable: points = Map[{#, NIntegrate[f[x, #], {x, 0, Pi}]} &, y] ListLinePlot[points]

1

I think this shows what the problem is. The ODE is inherently numerically unstable as any deviation from the exact solution goes to ±∞. asol = DSolve[{y''[x] == (x^2 - 1) y[x], y[0] == 1, y'[0] == p}, y[x], x]; Manipulate[ Plot[y[x] /. asol /. {{p -> 0}, {p -> 10^(-error)}, {p -> -10^(-error)}} // Evaluate, {x, 0, 7}, PlotRange -> 2], ...

Only top voted, non community-wiki answers of a minimum length are eligible