Tag Info

Hot answers tagged

14

The good news is that yes, there is an easy way to put your problem into NDSolve by using the new finite element functionality in v10. The bad news is that it seems the specific problem you're trying to solve is ill-posed. NDSolve can now handle internal boundaries; see e.g. the first figure under "Details" for DirichletCondition. Generating a mesh with ...


14

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


13

It turns out ListSurfacePlot3D does a terribly poor job of approximating the surface in the OP, otherwise one will just apply DiscretizeGraphics to the output obtained from ListSurfacePlot3D and be done with it. But since that's not applicable here, we present an approach that uses alpha shapes to approximate the shape of the given point set by tuning a ...


13

The memory leak in NIntegrate is a bug and has been fixed as of version 10.2.0. Earlier versions would lose around 720 bytes per evaluation for this example, which could not be recovered without restarting the kernel. ClearSystemCache[] should be used to make sure the memory is released. Using version 10.2: NI[z_?NumericQ, b0_?NumericQ] := ...


12

I'll change your proposed function because it's a constant and for such a function all sums will be equal.So: f[x_] := 1/4 x^2 di = (2 - 1)/50; intervals = Range[1, 2, di]; leftSum = Sum[f[i] di, {i, Most@intervals}]; rightSum = Sum[f[i] di, {i, Rest@intervals}]; middleSum = Sum[f[i + di/2] di, {i, Most@intervals}]; exact = Integrate[f@x, ...


12

NDSolve currently can't handle this kind of differential equation, LaplaceTransform is your friend. Since in this case inverse Laplace transform can't be done analytically by InverseLaplaceTransform, you need the help of numerical Laplace inversion package in addition: eq = {0.01 - 6.25 x[t] + (1.2 Integrate[x'[t - τ]/Sqrt[τ], {τ, 0, t}])/10^7 == 16 ...


12

$Version (* Out[228]= "8.0 for Microsoft Windows (64-bit) (October 7, 2011)" *) The result provided by Mathematica is correct: Integrate[a/(a^2 + Sin[t]^2), {t, 0, 2 π}] (* Out[213]= (2 π)/(Sqrt[1 + 1/a^2] a) *) Now the same procedure as "always" which "explains" the zero result. The indefinite integral is Integrate[a/(a^2 + Sin[t]^2), t] (* ...


12

If you're running Mathematica 10 or above, there's a dead simple method using the new Area function: newimg = img /. Ball[x___, 0.6] -> Sphere[x, 0.5] Total[Map[Area[RegionIntersection[#, Cuboid[{-1, -1, -1}, {1, 1, 1}]]] &, newimg]] (* 25.1327 *) Note a few things here: Mathematica considers Ball to be a 3D object; if we want its surface area ...


12

WhenEvent is working. Try WhenEvent[x[t] < 0, Print[t]; x[t] -> 0] to see that every crossing is detected. The problem is that it only detects crossings. So changing x[t] -> 0 does not reset the event. At the next step x[t] becomes negative and no event is detected. (This is how it is supposed to behave.) The way to deal with this is to use ...


11

Use NDSolve antiD = NDSolveValue[{f'[x] == Sqrt[1 + x^3], f[0] == 0}, f, {x, 0, 10}] Example usage: Plot[antiD[x], {x, 0, 10}] Alternatively... This works because this function can be antidifferentiated (by Mathematica). antiD = FunctionInterpolation[ Evaluate @ Integrate[Sqrt[1 + x^3], {x, 0, t}, Assumptions -> 0 < t < 10], {t, 0, ...


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


11

Working solution One can manually implement the shooting method with ParametricNDSolveValue and FindRoot: psol = ParametricNDSolveValue[{k f[z] + f'[z] == 0, f[-1] == Exp[2], WhenEvent[z == 0, f[z] -> r0 f[z]]}, f, {z, -1, 1}, {k, r0}]; k0 = k /. FindRoot[psol[k, 0.5][1] == 1, {k, 1}] (* 0.653426 *) Plot[psol[k0, 0.5][z], {z, -1, 1}] ...


10

For what it's worth, you can monitor the progress of Integrate and NIntegrate. I'm not sure how helpful the tools below are, but I feel it is probably worth mentioning them. Integrate Internal`Integrate`debugSwitch If you set Internal`Integrate`debugSwitch to the magic number 10, it will print its (major) steps in searching for the answer. For instance: ...


9

The interpolation step seems to be unnecessary because the integral into which it enters can be equally well approximated as a Riemann sum. So to get really fast results you could do the following: {r, h} = Transpose[hrdata]; d = Differences[r]; Clear[s]; s[q_] := (4 Pi 0.83 )/q Total[d Rest[r Sin[q r] (h - 1)]] ans2 = Table[{i, s[i]}, {i, 0.05, 11.4, ...


9

Besides trivial observations that one cannot evaluate numerically integrals involving symbolic constants there are more interesting aspects of the problem at hand. First one should realize that a standard numeric approach is not appropriate for this kind of problems, since the integrand involves singular points (zero in the denominator) thus it is not ...


9

Using approximate numbers (e.g. ones with decimal points) can lead to issues with exact solvers such as Integrate. One way around, if the function can be integrated with symbolic parameters, is to use Block to block the numeric values from being substituted until after the integration is complete: Block[{x0, a, b}, Assuming[a > 0 && b > 0 ...


9

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


8

the answer is in the links -- just to demonstrate or validate what it says: First@Last@ Reap@NIntegrate[Sin[x], {x, 0, 1}, EvaluationMonitor :> Sow[x], MaxRecursion -> 0, Method -> "GaussKronrodRule"] xi=First@Last@ Reap@NIntegrate[Sin[x], {x, 0, 1}, EvaluationMonitor :> Sow[x], MaxRecursion -> 0] ...


8

In V10 there has been added some symbolic processing of integrands containing an InterpolatingFunction. In particular if the interpolation grid divides the domain of integration into a number of subintervals, the number being at most the value of the option "MaxSubregions", the integrand will automatically be integrated over each subinterval. In V9, this is ...


8

Using the Method option with the following settings seems to work: NIntegrate[Sin[81 x]/((2^x + 1) (Sin[x])), {x, -Pi/2, Pi/2}, Method -> "LevinRule"] NIntegrate[Sin[81 x]/((2^x + 1) (Sin[x])), {x, -Pi/2, Pi/2}, Method -> "LocalAdaptive"] NIntegrate[Sin[81 x]/((2^x + 1) (Sin[x])), {x, -Pi/2, Pi/2}, Method -> ...


8

It looks like classic catastrophic round-off error. (Look at those exponents on $e$!). {sol} = DSolve[{x'[t] == y[t]/100, y'[t] == -100 x[t] - 100 y[t] + 2020, x[0] == 0, y[0] == 20}, {x, y}, t]; Now consider y[9.] vs. y[9] and y[9.`20]: y[9.] /. sol N::meprec: Internal precision limit $MaxExtraPrecision = 50.` reached while evaluating ...


8

You can make a change of variable to solve the problem. Here I'll use dChange for this task: r0 = 0.5; eqn = {k[z] f[z] + f'[z] == 0, k'[z] == 0, f[-1] == Exp[2], f[1] == 1}; c = Piecewise[{{r0, z > 0}}, 1]; neweqn = dChange[eqn, f[z] == c g[z]]; {solg, solk} = NDSolveValue[neweqn, {g, k}, {z, -1, 1}]; Plot[c solg[z], {z, -1, 1}]


8

I think there's a bug in the internal function NDSolve`SPRKDump`CheckSeparability that leads NDSolve to conclude that the system is not separable. I think you should report it and see if WRI can verify it (they would probably appreciate a link to this Q&A). It's a fair amount of work to track it down, and there is a lot of nearly unreadable stuff to ...


7

gauMix[x_, means_, vars_] := Total[(E^-(((x - means)^2)/(2*vars)))/Sqrt[2*Pi*vars]]/ Length[means]; means = {-7, 7}; vars = {6, 65/10}; f[x_] := gauMix[x, means, vars]; fxx = Integrate[f[x]*(x^2), {x, -Infinity, Infinity}] (* 221/4 *)


7

NIntegrate[f[t], {t, -1, x}] integrates the same thing over and over again, when a point is needed by Plot. Integrate what you need one time only: f[x_] = Which[-1 <= x <= 0, x, 0 <= x, x^2, True, 0]; ϕ[x_] = NDSolve[{Derivative[1][g][t] == f[t], g[-1] == 0}, g, {t, -1.5, 1}][[1, 1, 2]][x]; Plot[ϕ[x], {x, -1.5, 1}]


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


7

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


7

One can use Picard-type iteration to get the solution: Using an approximation to x'[t] (in the integral), we can integrate the ODE to obtain a new approximation. Remarkably, it converges in just two steps. My original thought was to step through the integration using the tools from tutorial/NDSolveStateData to build an interpolation of x'[t] at each step ...


7

Now fixed in version 10.2. In[1]:= Catch[NIntegrate[ 1, {x, y} \[Element] Triangle[{{0, 0}, {1, 2}, {2, 1}}], Method -> "MonteCarlo", EvaluationMonitor :> Throw[{x, y}]]] Out[1]= {0.615426, 0.793388} There is also RandomPoint: In[2]:= ...


7

Mathematica seems to split the integrand component, E^(-((-m + Log[x])^2/(2 s^2))) into E^(-((m^2 + Log[x]^2)/(2 s^2))) times the sort-of "coefficient" E^((m Log[x])/s^2) (* == x^(m/s^2) *) in order to calculate the integral in terms of Meijer $G$. For reasons that are obscure to me, it seems to want the coefficient of m in the exponent to be ...



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