Hot answers tagged

8

Having put in some time trying to see what's going, I've found a few things, but I don't have a perfectly clear picture. I believe the issue is with the large InterpolatingFunction in the integrand and not with NIntegrate per se. The time it takes for NIntegrate to set up the integration is much longer in V10, but the integration itself runs in about the ...


8

The trick is to use NDSolve instead of NIntegrate and thus in effect obtain a numerical antiderivative that can be evaluated fast at different points. NIntegrate will only do definite integration, so it needs to be run each time the integration bounds are changed. This is very slow, as you noticed. NDSolve will only need to be run once. Slow way (what ...


5

Since f[t] yields in effect {x[t], y[t]} and the area under a curve (with y[t] > 0) is given by the integral of y[t] x'[t] (i.e., $\int y\;dx$), then the following should work, assuming x[t] is increasing. dA[f_, t_?NumericQ] := Last[f[t]] First[f'[t]]; NIntegrate[dA[f, t], {t, 0, 1}] (* 150000. *) One can extend this to other forms, e.g. to closed ...


4

Sometimes a manual approach to the shooting method makes a BVP easier to solve. Set up with ParametricNDSolveValue: zmin = 0; zmax = 2; psol = ParametricNDSolveValue[{ D[ϕ[z], z, z] == 4*λ*ϕ[z]*(ϕ[z]*ϕ[z] - v^2) + 2*γ*χ[z]*χ[z]*ϕ[z], D[χ[z], z, z] == 2*γ*χ[z]*(ϕ[z]*ϕ[z] - μ^2) + 4*β*γ*χ[z]*χ[z]*χ[z], ϕ[zmin] == phi[zmin], ϕ'[zmin] == phip, ...


4

Here is one way (but I remain interested in other ways to solve the problem). The indefinite integral can still be reinterpreted as a differential equation, of the form $$ \frac{\partial F}{\partial t}(t,t')=f(t,t') \quad \text{under }F(0,t')=0 $$ and, though this isn't obvious, it can still be handled as a differential equation by NDSolve - except this ...


4

You should note that NIntegrate normally consumes standard functions or InterpolatingFunction. A BezierFunction is a parametric function and will not work right away with the integrator. You can do the following by the way. mesh = DiscretizeGraphics[ParametricPlot[f[t], {t, 0, 1}]]; nf = Interpolation[MeshCoordinates[mesh]]; NIntegrate[nf[x], ...


3

You're experiencing the typical and, in the simple example, expected limitations of searching for roots. The two FindRoot results are easily understood in terms of Newton's method. The best way to proceed, assuming given the example is typical, is to use WhenEvent. sol = NDSolve[{y''[t] == -y[t], y[0] == 1, y'[0] == 0, WhenEvent[y[t] == 0, firstzero = ...


3

For problems like these, I like to take small steps and check each one, so please bear with me. First, we write the two exponential functions, which I am calling f1 and f2. Then write the next two more complicated functions, called gSing for singlet and gTrip for triplet. Then write an expression for the denominator and an expression for the volume ...


3

Examine your integrand (which is suggested by the error, after all). PiecewiseExpand will collect all terms under one piecewise function. c*h[c, k1, t1]*(1 - H[c, k2, t2])*(1 - H[c, k3, t3]) // PiecewiseExpand (* Power::infy, Infinity::indet errors... *) You can see that the function does not have numeric values for c > 1. How to fix it is ...


2

Indeed, NDSolve cannot solve this equation as written. However, it is easy enough to eliminate y from the system. {x'[t] == y[t] + x[t] y[t] + z[t], z'[t] == 2*y[t]} /. y[t] -> 1 + z[t] - 2 x[t] and then solve and plot s1 = NDSolve[{Derivative[1][x][t] == 1 - 2 x[t] + 2 z[t] + x[t] (1 - 2 x[t] + z[t]), Derivative[1][z][t] == 2 (1 - 2 x[t] + ...


2

As @bbgodfrey commented, if the integrals in the equation in the OP's FindRoot command can be evaluated before passing the equation to FindRoot, one can save a lot of time. It seems there is still more to be done. I found FindRoot struggles to find an accurate root in some areas of the domain of the equation. It turns out one can use Solve to solve the ...


2

The most direct solution for the problem seems to be approximating the b.c. at infinity with a b.c. that's just far enough e.g. $$z'(10)\approx z'(\infty)=0$$ and it's indeed applicable: eq = z''[r]/(1 + z'[r]^2)^(3/2) + z'[r]/(r (Sqrt[1 + z'[r]^2])) == z[r]; {lb, rb} = {1, 10}; bcl = z'[lb] == -2; bcr := z'[rb] == 0; sol1 = NDSolveValue[{eq, bcl, bcr}, ...


1

Sometimes I do it this way: Block[{NIntegrate, x, y, z}, intfunc[z_] = NIntegrate[integrand, {x, -5, 5}, {y, -10, 1000}]; ]; intfunc[7.] (* 3.5009*10^7 *) It's not a great general programmatic way to go, but in the middle of solving a problem, in which I have constructed an expression integrand, this is to me an easy way. Blocking x, y, and z ...


1

integrand is a function and functions should be defined and called with explicit arguments. However, there is no need to use PatternTest with its arguments. integrand[x_, y_, z_] = z (Exp[-x^2] + y - Cos[y]); Since intfunc employs a numeric technique (NIntegrate) it should be defined with a PatternTest intfunc[z_?NumericQ] := NIntegrate[integrand[x, ...



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