# Tag Info

12

The main problem is that your pos is not seen as a 3D vector. The cross product is therefore interpreted as a scalar: q*Cross[D[pos[t], t], b] when adding this to the vector q.e this 'scalar' term is added to each of the vector components: q*e + q*Cross[D[pos[t], t], b] This won't work, instead do: b = {1, 0, 0}; e = {0, 0, 1}; q = 1; m = 1; ...

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

8

Alternative method: b = {1, 0, 0}; e = {0, 0, 1}; q = 1; m = 1; sol = NDSolve[{e + Cross[pos'[t], b] == m/q pos''[t], pos[0] == {0, 0, 0}, pos'[0] == {0, 0, 0}}, pos, {t, 0, 10}, Method -> {"EquationSimplification" -> "Residual"}]; ParametricPlot3D[pos[t] /. sol, {t, 0, 10}, PlotRange -> All]

6

Another alternative is to package your constant vector parameters as DiscreteVariables. In the OP's case, it necessary only to chnage e since b occurs inside Cross, which will not evaluate until all its arguments are vectors. Note that in the equation we changed e to e[t] and set its value with e[0] == {0, 0, 1}. b = {1, 0, 0}; (*e={0,0,1};*) q = 1; m = 1; ...

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

5

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

5

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

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

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

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

4

Your F wrapper function is doing nothing for you in your code, so I removed it and replaced it with direct calls to F1. NIntegrate in the argument to FindMinValue should not be evaluated unless it is passed explicitly numerical arguments, so it is best to wrap it in a function protected by NumericQ (functiontominimize below). Since all the other functions ...

4

This is a fairly common problem to encounter and in this case was a bit subtle due to having an outer and inner function both in need of being defined only for explicitly numeric input. So I'll repost my comment as an answer and make it a Community wiki. The FindMinumum objective itself needs to be defined only for numeric input. Which can be done as ...

3

From the description of the question it seems to me that using the (undocumented) option IntegrationMonitor to obtain integration intervals and estimates might be very useful. Here is an example: t = Reap[NIntegrate[Sin[x]/Sqrt[x], {x, 0, 100}, PrecisionGoal -> 6, Method -> "MonteCarlo", IntegrationMonitor -> (Sow[ ...

3

You want to hunt down the error? Here is the best piece of advice: don't plot a function until you know it works. Okay, that's out of the way, now let's go through the process of finding out why your code gives an error. First we can look at just one integral, Λ = 10^-6; Δ = 10^-3; θ = 1/2 ArcTan[Δ/δ]; h = 10^-1; t = 10^3; s = -h Sqrt[Δ^2 + δ^2] ...

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

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

2

What about: StoppingTest -> (Apply[ Or, Table[EuclideanDistance[Coordinates[s], Source[i]] < 1, {i,1,NumSources}], {0}])

2

I only address your question about the integration. I tried your code for the integral for t=1. Indeed there was such a message: NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small I think a good idea would be ...

1

This is an interesting question with some promising discussion in comments; I'll summarize what was said for future reference, so your question shows up as answered. m_goldberg reported that unfortunately there are no documented options for StateResponse yet. However, Suba Thomas suggested that you can try to pass options to the underlying NDSolve by ...

1

I computed the PDE system solutions without the MaxStep specification. (As suggested by user21.) The numerical integration seems to be fast enough with the default NIntegrate options. If I use AccuracyGoal -> 6 the integration becomes 3 times faster. If I remove the integration at $t=0$ I do not get messages. As Alexei Boulbitch suggested we can try ...

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