How can I speed up NIntegrate computation? - plot

I want to plot a definite integral
myIntegral[x_] := NIntegrate[Sqrt[(a - b)^2 + (c - d)^2]/ (a - b), {a, 0, x}, {b, x, 1}, {c, 0, 1}, {d, 0, 1}]
Plot[myIntegral[x], {x, 0, 1}]
I am using Mathematica 11.3. When I evaluate the code, Mathematica immediately gives some warnings about numerical integration converging too slowly, then it seems stuck in computation.
I have tried some options of NIntegrate, for example as suggested here, but have had no success.
Is there a way to obtain the result of Plot in an acceptable time (e.g. some minutes)?

It is within reach of Integrate.
Assuming[0<x<1,
Simplify[
Integrate[Sqrt[(a-b)^2 + (c-d)^2]/(a-b), {a,0,x}, {b,x,1}, {c,0,1}, {d,0,1}]]]
which gives you
(-11*Sqrt[2] + 11*Sqrt[2+(-2+x)*x] + x*(-8-7*Sqrt[2+(-2+x)*x] + 13*Sqrt[1+x^2] +
2*x*(6-3*Sqrt[2+(-2+x)*x] + x*(-4+2*x + Sqrt[2+(-2+x)*x] - Sqrt[1+x^2]))) +
18*x^2*ArcCoth[Sqrt[2+(-2+x)*x]] + 9*ArcSinh[1] - 3*ArcSinh[1-x] - 3*ArcSinh[x] +
6*Log[1-x] + 9*x*Log[-1+Sqrt[2+(-2+x)*x]] + 3*((-2+x)*Log[1+Sqrt[2+(-2+x)*x]] -
4*x*Log[-(((-1+x)*(1 + Sqrt[1+x^2]))/x)] + x^3*(Log[2+x^2+2*Sqrt[1+x^2]] +
2*Log[(1-x)/(x+x*Sqrt[2-2*x+x^2])])))/36
And that should be much faster to plot than doing thousands on individual NIntegrate

Related

No output on ParametricPlot

I'm solving and plotting the equations of motion for the double pendulum using Mathematica's NDSolve.
I've successfully plotted the Angular position using a standard plot. But when I come to use the parametric plot for the position of each mass. I get no errors but simply no plot.
eqn1 = 2 th''[t] + Sin[th[t] - ph[t]] (ph'[t])^2 + Cos[th[t] - ph[t]] (ph''[t]) + (2 g/l) Sin[th[t]]
eqn2 = ph''[t] + Sin[th[t] - ph[t]] (th'[t])^2 + Cos[th[t] - ph[t]] (th''[t]) + (g/l) Sin[th[t]]
eqnA = eqn1 /. {g -> 10, l -> 1}
eqnB = eqn2 /. {g -> 10, l -> 1}
sol = NDSolve[{eqnA == 0, eqnB == 0, th[0] == 0.859, th'[0] == 0, ph[0] == 0.437, ph'[0] == 0}, {th, ph}, {t, 0, 10}]
Plot[{th[t], ph[t]} /. sol, {t, 0, 10}]
r1 = {lSin[th[t]] + lSin[ph[t]], -lCos[th[t]] - lCos[ph[t]]} /. {l -> 1, g -> 10}
ParametricPlot[r1 /. sol, {t, 0, 10}]
Replace
r1 = {lSin[th[t]] + lSin[ph[t]], -lCos[th[t]] - lCos[ph[t]]} /. {l->1, g->10}
with
r1 = {l*Sin[th[t]] + l*Sin[ph[t]], -l*Cos[th[t]] - l*Cos[ph[t]]} /. {l->1, g->10}
and your ParametricPlot should appear.
One useful trick you might remember, when any plot doesn't appear you can try replacing the plot with Table and see what it shows. Often the table of data provides the needed hint about why the plot isn't appearing.

how to improve the performance of this recursion code in mathematica

Clear[r, re, p, pmax, delta, imagesize, delta]
ClearSystemCache[]
re[0, r_] := Sqrt[8/Pi]*((1 - r)/r)^(1/4)*1;
re[1, r_] := Sqrt[8/Pi]*((1 - r)/r)^(1/4)*-1*2*(1 - 2*r);
re[p_, r_] := re[p, r] = Sqrt[8/Pi]*((1 - r)/r)^(1/4)*(-1)^p*(re[1, r]*re[p - 1, r] - re[p - 2, r]);
imagesize = 32;
pmax = 10;
delta = 2/imagesize;
Table[r = Sqrt[x^2 + y^2]; re[pmax, r], {x, -1 + delta/2, 1 - delta/2, delta}, {y, 1 - delta/2, -1 + delta/2, -delta}];
this code is to calculate the distance r from each pixel to point(0,0), then evaluate the radial polynomial as below:
for accuracy, I will use the recursion version:
.
When the imagesize and pmax increase, the time will become unacceptable. So, I would ask if we can use compile of other methods to speed up, like for imagesize is 256 and pmax is 120, the time will be about 10 seconds.
In my code, I also use the memoization to store the value during the evaluation which I will use in the future.

Solving of a linear system with parameters

I have a linear system of four equations with four variables $a,b,c,d$ and two parameters $i,h$ where equations are roughly of the form
$$a h^3 i^3 + b h^2 i^2 +c h i +d=0$$
I want to get $a,b,c,d$ in terms of $i,h$.
Is this possible in SymPy? If not, can someone recomend how to do it on some other way?
For completeness, the answer is yes, solve in Sympy solves systems of equations with parameters. An example using the equation you stated:
from sympy import *
var('a b c d i h')
eqns = [a*h**3*i**3 + b*h**2*i**2 + c*h*i + d, a+b+c+d, a-b*h*i**2 -c - d, a+b+c-h**2 - i**2]
solve(eqns, [a,b,c,d])
The first argument of solve is a list of equations, the second the list of variables to solve for. The output is a solution, presented as a dictionary:
{c: (h**2 + i**2)*(-h**4*i**5 + h**3*i**3 - 2*h**2*i**2 + h*i**2 + 1)/(h*i*(-h**3*i**4 + h**2*i**2 + h*i**2 - 2*h*i + 1)),
b: -(2*h**2 + 2*i**2)/(h*i*(h**2*i**3 + h*i**2 - h*i + 1)),
a: (-h**3*i**2 + h**2 - h*i**4 + i**2)/(h*i*(h**2*i**3 + h*i**2 - h*i + 1)),
d: -h**2 - i**2}

Controlling measure zero sets of solutions with Manipulate. A case study

To address the question we start with the following toy model problem being here just a case study:
Given two circles on a plane (its centers (c1 and c2) and radii (r1 and r2)) as well as a positive number r3, find all circles with radii = r3 (i.e all points c3 being centers of circles with radii = r3) tangent (externally and internally) to given two circles.
In general, depending on Circle[c1,r1], Circle[c2,r2] and r3 there are 0,1,2,...8 possible solutions. A typical case with 8 solutions :
I slightly modified a neat Mathematica implementation by Jaime Rangel-Mondragon on Wolfram Demonstration Project, but its core is similar:
Manipulate[{c1, a, c2, b} = pts;
{r1, r2} = Map[Norm, {a - c1, b - c2}];
w = Table[
Solve[{radius[{x, y} - c1]^2 == (r + k r1)^2,
radius[{x, y} - c2]^2 == (r + l r2)^2}
] // Quiet,
{k, -1, 1, 2}, {l, -1, 1, 2}
];
w = Select[
Cases[Flatten[{{x, y}, r} /. w, 2],
{{_Real, _Real}, _Real}
],
Last[#] > 0 &
];
Graphics[
{{Opacity[0.35], EdgeForm[Thin], Gray,
Disk[c1, r1], Disk[c2, r2]},
{EdgeForm[Thick], Darker[Blue,.5],
Circle[First[#], Last[#]]& /# w}
},
PlotRange -> 8, ImageSize -> {915, 915}
],
"None" -> {{pts, {{-3, 0}, {1, 0}, {3, 0}, {7, 0}}},
{-8, -8}, {8, 8}, Locator},
{{r, 0.3, "r3"}, 0, 8},
TrackedSymbols -> True,
Initialization :> (radius[z_] := Sqrt[z.z])
]
We can easily conclude that in a generic case we have an even number of solutions 0,2,4,6,8 while cases with an odd number of solutions 1,3,5,7 are exceptional - they are of zero measure in terms of control ranges. Thus changing in Manipulate c1, r1, c2, r2, r3 one can observe that it is much more difficult to track cases with an odd number of circles.
One could modify on a basic level the above approach : solving purely symbolically equations for c3 as well as redesignig Manipulate structure with an emphasis on changing number of solutions. If I'm not wrong Solve can work only numerically with Locator in Manipulate, however here Locator seems to be crucial for simplicity of controlling c1, r1, c2, r2 as well as for the whole implementation.
Let's state the questions, :
1. How can we force Manipulate to track seamlessly cases with an odd number of solutions (circles) ?
2. Is there any way to make Solve to find exact solutions of the underlying equations?
( I find the answer by Daniel Lichtblau to be the best approach to the question 2, but it seems in this instance there is still an essential need for sketching of a general technique of emphasizing measure zero sets of solutions while working with Manipulate )
These considerations are of less importance while dealing with exact solutions
For example Solve[x^2 - 3 == 0, x] yields {{x -> -Sqrt[3]}, {x -> Sqrt[3]}}
while in case from the above of slightly more difficult equations extracted from Manipulate setting the following arguments :
c1 = {-Sqrt[3], 0}; a = {1, 0}; c2 = {6 - Sqrt[3], 0}; b = {7, 0};
{r1, r2} = Map[ Norm, {a - c1, b - c2 }];
r = 2.0 - Sqrt[3];
to :
w = Table[Solve[{radius[{x, y} - {x1, y1}]^2 == (r + k r1)^2,
radius[{x, y} - {x2, y2}]^2 == (r + l r2)^2}],
{k, -1, 1, 2}, {l, -1, 1, 2}];
w = Select[ Cases[ Flatten[ {{x, y}, r} /. w, 2], {{_Real, _Real}, _Real}],
Last[#] > 0 &]
we get two solutions :
{{{1.26795, -3.38871*10^-8}, 0.267949}, {{1.26795, 3.38871*10^-8}, 0.267949}}
similarly under the same arguments and equations, putting :
r = 2 - Sqrt[3];
we get no solutions : {}
but in fact there is exactly one solution which we would like to emphasize:
{ {3 - Sqrt[3], 0 }, 2 - Sqrt[3] }
In fact, passing to Graphics such a small difference between two different solutions and the uniqe one is indistinguishable, however working with Manipulate we cannot track carefully with a desired accuracy merging of two circles and usually the last observed configuration when lowering r3 before vanishing all solutions (reminding so-called structural instability) looks like this :
Manipulate is rather a powerful tool, not only a toy, and its mastering could be very useful. The considered issues when appearing in a serious research are frequently critical, for example: in studying solutions of nonlinear differential equations, occurence of singularities in its solutions, qualitative behavior of dynamical systems, bifurcations, phenomena in Catastrophe theory and so on.
As this is a measure zero set, tools that require some granularity will generally have trouble with the concept. Perhaps better is to look for the singularity locus explicitly, where solutions have multiplicity or in other ways depart from the nearby solution behavior(s). It will be a part of the discriminant variety. In particular, you can grab the relevant part by setting your defining polynomials to zero and simultaneously making the Jacobian determinant zero.
Here is your example. I will eventually (wlog) put one center at the origin and the other at (1,0).
centers = Array[c, {2, 2}];
radii = Array[r, 3];
circ[cen_, rad_, x_, y_] := ({x, y} - cen).({x, y} - cen) - rad^2
I'll use your 'k' for both polynomials. Your formulation has pairs (k,l) where each is +-1. We can just use k, arrange by squaring to get a polynomial in k^2, and replace that with 1.
polys =
Table[Expand[
circ[centers[[j]], radii[[3]] + k*radii[[j]], x, y]], {j, 2}]
Out[18]= {x^2 + y^2 - 2 x c[1, 1] + c[1, 1]^2 - 2 y c[1, 2] +
c[1, 2]^2 - k^2 r[1]^2 - 2 k r[1] r[3] - r[3]^2,
x^2 + y^2 - 2 x c[2, 1] + c[2, 1]^2 - 2 y c[2, 2] + c[2, 2]^2 -
k^2 r[2]^2 - 2 k r[2] r[3] - r[3]^2}
We'll remove the part that is linear in k, square the rest, square that removed part, and equate the two. We also then replace k with unity.
p2 = polys - k*Coefficient[polys, k];
polys2 = Expand[p2^2 - (k*Coefficient[polys, k])^2] /. k -> 1;
We now get the determinant of the Jacobian and add that to the brew.
discrim = Det[D[polys2, #] & /# {x, y}];
allrelations = Join[polys2, {discrim}];
Now set the centers as noted earlier (could have done this from the beginning, one would suppose).
ar2 =
allrelations /. {c[1, 1] -> 0, c[1, 2] -> 0, c[2, 1] -> 0,
c[2, 2] -> 0}
Out[38]= {x^4 + 2 x^2 y^2 + y^4 - 2 x^2 r[1]^2 - 2 y^2 r[1]^2 +
r[1]^4 - 2 x^2 r[3]^2 - 2 y^2 r[3]^2 - 2 r[1]^2 r[3]^2 + r[3]^4,
x^4 + 2 x^2 y^2 + y^4 - 2 x^2 r[2]^2 - 2 y^2 r[2]^2 + r[2]^4 -
2 x^2 r[3]^2 - 2 y^2 r[3]^2 - 2 r[2]^2 r[3]^2 + r[3]^4, 0}
We now eliminate x and y to get the locus in r[1],r[2],r[3] parameter space that determines where we'll have multiplicity in our solutions.
gb = GroebnerBasis[ar2, radii, {x, y},
MonomialOrder -> EliminationOrder]
{r[1]^6 - 3 r[1]^4 r[2]^2 + 3 r[1]^2 r[2]^4 - r[2]^6 -
8 r[1]^4 r[3]^2 + 8 r[2]^4 r[3]^2 + 16 r[1]^2 r[3]^4 -
16 r[2]^2 r[3]^4}
If I did this all correctly, then we now have the polynomial defining the locus in parameter space where solution sets can get silly. Off this set they should never have multiplicity, and real counts should always be even. The intersection of this set with real space will be a 2d surface in the 3d space of the radii parameters. It will separate regions that have 0, 2, 4, 6, or 8 real solutions from one another.
Last, I'll point out that in this example the variety in question reduces nicely into a product of planes. I guess from a geometric view this is not too surprising.
Factor[gb[[1]]]
Out[43]= (r[1] - r[2]) (r[1] + r[2]) (r[1] - r[2] - 2 r[3]) (r[1] +
r[2] - 2 r[3]) (r[1] - r[2] + 2 r[3]) (r[1] + r[2] + 2 r[3])

i need to find the upper bound of this: or the tight bound:

lets say i have an expression:
(n)+((n-1)*2)+((n-2)*3)+((n-3)*4)+...+(3*(n-2))+(2*(n-1))+(1*(n))
what is the tight bound of this? or the upper bound? is this n^3? is this n^4? the maximum amount of number i can get out of this? thanks
EDIT: so: for i=1 then: the ans is 1.
i=2: (1*2 + 2*1)
1=3: (1*3 + 2*2 + 3*1)
i=4: (1*4 + 2*3 + 3*2 + 4*1 )
and so on
Try Wolfram Alpha ...
Sum[(i + 1) (n - i), {i, 0, n - 1}]

Resources