Tag Info

Hot answers tagged

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 ...


11

I've got a theory.... Let's try to get the antiderivative: Integrate[(1 + 16*Tan[2*x - y]^2)/(1 + 4*Tan[2*x - y]^2), x, Assumptions -> y \[Element] Reals] (*returns 5 x - (5 y)/2 - ArcTan[2 Tan[2 x - y]] *) Seems legit. You can test with D[5 x - (5 y)/2 - ArcTan[2 Tan[2 x - y]], x] and plot or rearrange. This antiderivative is technically correct, ...


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 ...


6

[Edit notice: I'll put the gist up front.] 10 π is not wrong With proper assumptions given, the integral evaluates as desired by the OP, to 6 π. Without them, it gives one of the correct values of the integral, 10 π, the one that in some sense is more likely, but without the correct conditions attached. (One may well argue that is a bug. However, ...


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 flesh out GuessWho's suggestion: sol[t_] = NDSolveValue[{Cos[t] E^(Cos[t] + 0.5 t) == f'[t], f[0] == 0}, f[t], {t, 0, 40}] Show[ Plot[E^(-0.5 x) NIntegrate[Cos[t] E^(Cos[t] + 0.5 t), {t, 0, x}], {x, 0, 40}] Plot[E^(-0.5 t) sol[t], {t, 0, 40}, PlotStyle -> {Red, Dashed}] ] The second plot is about 300 times faster than the first one.


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

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.01`20; (* 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


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. ...


2

Oddly, if you follow the hints given in the ConditionalExpressions you can get pointed to the right answer although constrained to an overly restrictive region. $Version "10.1.0 for Mac OS X x86 (64-bit) (March 24, 2015)" expr = (1 + 16 Tan[2 x - y]^2)/(1 + 4 Tan[2 x - y]^2); Integrate[expr, {x, 0, 2 Pi}] ConditionalExpression[9*Pi, -(Pi/2) ...


2

Just to illustrate versatility of Mathematica: Plot3D[2 x - y, {x, -2, 2}, {y, -2, 2}, RegionFunction -> Function[{x, y, z}, x^2 + y^2 < 4]] f = TransformedField["Cartesian" -> "Polar", 2 x - y, {x, y} -> {r, t}]; j = Simplify[Det[Outer[D[#1, #2] &, {r Cos[t], r Sin[t]}, {r, t}]]]; Integrate[f j, {r, 0, 2}, {t, 0, 2 Pi}] where the ...


2

While belisarius's comment answers the question, an arguably better way to achieve these is to use regions. For example, the plot is less choppy and there is less rounding error when integrating (for this example at least). (* without regions *) f[x_, y_] := 2*x - y (* choppy plot *) Plot3D[f[x, y], {y, -2, 2}, {x, -1*Sqrt[4 - y^2], Sqrt[4 - y^2]}] (* ...


1

You could sample by yourself dataSample= (E^(-0.5 #) NIntegrate[Cos[t] E^(Cos[t] + 0.5 t), {t, 0, #}] &) /@ Range[0, 40, .1] ListLinePlot[Thread@{Range[0, 40, .1], dataSample}]


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

Integrate and NIntegrate agree on this matter: Table[Integrate[(1+16 Tan[2 x-y]^2)/(1+4 Tan[2 x-y]^2),{x,0,2Pi}],{y,0,10,2}] (*==> {6π,6π,6π,6π,6π,6π}*) Table[NIntegrate[(1+16 Tan[2 x-y]^2)/(1+4 Tan[2 x-y]^2),{x,0,2Pi}],{y,0,10,2}] (*==> {18.8496,18.8496,18.8496,18.8496,18.8496,18.8496}*) N[6Pi] (*==> 18.8496*)


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

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

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 ...



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