Faster alternative to NSolve - wolfram-mathematica

I have an equation, which I am trying to solve using NSolve in Mathematica. Here is how I do it:
T == (0.000242895 E^(-(2.09472*10^11/(5.70068*10^8 +
7.76206*10^12 T))))/(1 +
0.969073 E^(-(4.18945*10^11/(5.70068*10^8 + 7.76206*10^12 T))) -
1.96883 Cos[8.77331*10^6/(2.28027*10^9 + 7.76206*10^12 T)])
NSolve[T == (0.000242895 E^(-(2.09472*10^11/(5.70068*10^8 +
7.76206*10^12 T))))/(1 +
0.969073 E^(-(4.18945*10^11/(5.70068*10^8 + 7.76206*10^12 T))) -
1.96883 Cos[8.77331*10^6/(2.28027*10^9 + 7.76206*10^12 T)])
, {T}, Reals]
The problem is that it takes a long time (~1min) to find a solution. Is there a faster way to obtain a solution other than using NSolve?

The denominator of your expression goes through zero at approximately T=215. The other solution is T=0. If you want the non-zero solution simply use
FindRoot[denom,{T,T0}]
where T0 is most any positive number. This takes about a millisecond.

Related

Is there a general term formula for 3 queens problem?

The specific question description is:
Put 3 queens on a chessboard of M columns and N rows, how to determine the number of ways that no two of them are in attacking positions?
Note that M is not equals to N, and M/N are larger than a Integer in C language so that there is no way to solve this question using classical computer algorithm like DFS/BFS(for time and memory complexity considerations).
I guess this problem can be calculated by the mathematical method of permutation or combination, but I am not good at math, so, please help me.
Yes.
Searching for keyword "3 queens" in OEIS gives us A047659, and in the Formula section, Vaclav Kotesovec wrote that:
In general, for m <= n, n >= 3, the number of ways to place 3 nonattacking queens on an m X n board is n^3/6*(m^3 - 3m^2 + 2m) - n^2/2*(3m^3 - 9m^2 + 6m) + n/6(2m^4 + 20m^3 - 77m^2 + 58m) - 1/24*(39m^4 - 82m^3 - 36m^2 + 88m) + 1/16*(2m - 4n + 1)(1 + (-1)^(m+1)) + 1/2(1 + abs(n - 2m + 3) - abs(n - 2m + 4))(1/24((n - 2m + 11)^4 - 42(n - 2m + 11)^3 + 656(n - 2m + 11)^2 - 4518(n - 2m + 11) + 11583) - 1/16(4m - 2n - 1)*(1 + (-1)^(n+1))) [Panos Louridas, idee & form 93/2007, pp. 2936-2938].
This formula can be manually confirmed on small Ns and Ms. A simple Python script for this purpose is shown below:
import fractions # to avoid floating error
m = fractions.Fraction(4)
n = fractions.Fraction(4)
assert m<=n
one = fractions.Fraction(1)
ans = n**3/6*(m**3 - 3*m**2 + 2*m) - n**2/2*(3*m**3 - 9*m**2 + 6*m) + n/6*(2*m**4 + 20*m**3 - 77*m**2 + 58*m) - one/24*(39*m**4 - 82*m**3 - 36*m**2 + 88*m) + one/16*(2*m - 4*n + 1)*(1 + (-1)**(m+1)) + one/2*(1 + abs(n - 2*m + 3) - abs(n - 2*m + 4))*(one/24*((n - 2*m + 11)**4 - 42*(n - 2*m + 11)**3 + 656*(n - 2*m + 11)**2 - 4518*(n - 2*m + 11) + 11583) - one/16*(4*m - 2*n - 1)*(1 + (-1)**(n+1)))
print(ans)
Unfortunately, I failed to find the proof of this formula ([Panos Louridas, idee & form 93/2007, pp. 2936-2938], as Vaclav Kotesovec cited). The journal idee & form does not seem to be freely accessible. But due to the reputation of Vaclav Kotesovec (the author of Non-attacking chess pieces), I think we should trust this result.
The simple answer is inclusion/exclusion.
We start by counting the number of ways to place 3 queens in order. Which is just (n*m) * (n*m - 1) * (n*m - 2).
Now we have overcounted, because we don't want the count of the number of ways to place 3 queens with queens 1,2 attacking. Or queens 1,3. Or queens 2,3. But that is just the sum over rows, columns and diagonals of length l of l * (l-1) * (m*n-2).
But now we have undercounted, because if all 3 queens attack each other then we added them in the first step, subtracted 3x in the second step, and now we need to add them back 2x to get to counting those 0 times. Which is the sum over rows, columns and diagonals of length l of l * (l-1) * (l-2).
But this is the count of ways to place all of the queens in order. But given 3 queens on the board, there are 6 orders we could place them. So divide the whole answer by 6.
This can be turned into a program that is O(n+m) to run. Which should be fast enough for your purposes. If you were willing to do a bunch more analysis, we could actually produce a O(1) formula.
The field with coordinates (i, j) is vulnerable for the queen locаted at (qi , qj) if
i == qi || j == qj || abs(i - j) == abs(qi - qj)
This boolean expression should be false for feasible coordinates of each queen. Finding three such fields should not be hard. One cаn try Monte Carlo method which has complexity o(M * N) in worst case.

How to replace a Exponential function with a series in a complex expression using sympy or MMA?

I have a function like this :
The Funtion in MMA
the express for MMA code:
1/(-0.00001 + accT)^11 3.03226 (3.7499 - 8.57119 accT +
4.99983 accT^2 + 0.600048 accT^3 - 1.50004 accT^4 +
1. accT^5) (0.285697 + 0.0000119995 accT - 0.599976 accT^2 -
2.*10^-14 accT^3 + 1.5*10^-9 accT^4 - 0.00006 accT^5 +
1. accT^6) (-1. + E^(
3/2 (1 - (1.56078 (-0.00001 + accT))/phVel)^2)) phVel^2
I want to integrate accT in (0,1).
I have try lots of ways to integrate the function. Fox example,set the Lower limit of integration in 0.0000001 or split integral interval to every 0.01 then Multiply by the express,i.e,Rectangular method, loop i in [0.01,100]:
express1+=0.01*express(i)
but all fail.
so I try to using series to expand E^(3/2 (1 - (1.56078 ( -0.00001 + accT))/phVel)^2),and use the result replace Exponential function.
import sympy as sy
#like picture
express = .....
expressionSeries = sy.exp( 3/2 (1 - (1.56078 (-0.00001 +accT))/phVel)**2),
expressionSeries = sy.series(expressionSeries,accT,0,5)
expression = expression.subs(sy.exp( 3/2 (1 - (1.56078 (-0.00001 +accT))/phVel)**2),expressionSeries)
the code dosen't work.
The integrate result should integrate(express)~phVel^7
You have a factor of 1/(-0.00001 + accT)^11 and so when accT==0.00001 it looks like you will have infinity.
You might try to find
Simplify[
Limit[Integrate[yourfunction,{accT,0,a}],a->0.00001,Direction->"FromBelow"]+
Limit[Integrate[yourfunction,{accT,a,1}],a->0.00001,Direction->"FromAbove"]
]
but that looks like it might be adding -infinity and infinity and the result of that can be anything.
Lots of things about this question and this response really worry me.

Getting the equation for the line of intersection using Mathematica

I have a nasty expression that I am playing around with on Mathematica.
(-X + (2 X - X^2)/(
2 (-1 + X)^2 ((1 + 2 (-1 + p) X - (-1 + p) X^2)/(-1 + X)^2)^(3/2)))/X
I graphed it along with the plane z = 0 where X and p are both restricted from 0 to 1:
Plot3D[{nasty equation is here, 0}, {p , 0, 1}, {X, 0, 1}]
I decided it would be interesting to obtain the equation for the intersection of the plane generated from the nasty equation and z = 0. So I used solve:
Solve[{that nasty equation == 0}, {p, X}, Reals]
and the output was even nastier with some results having the # symbol in it ( I have no idea what it is, and I am new to Mathematica). Is there a way to get an equation for the nice line of intersection between the nasty equation and z = 0 where p and X are restricted from 0 to 1? In the graph generated from Plot3D I see that the line of intersection appears to be some nice looking half parabola looking thing. I would like the equation for that if possible. Thank you!
For complicated nasty equations Reduce is often more powerful and less likely to give you something that you will later find has hidden assumptions inside the result. Notice I include your constraint about the range of p and X to give Reduce the maximum amount of
information that I can to help it produce the simplest possible solution for you.
In[1]:= Reduce[(-X + (2 X-X^2)/(2 (-1 + X)^2 ((1 + 2 (-1 + p) X - (-1 + p) X^2)/
(-1 + X)^2)^(3/2)))/X == 0 && 0 < X < 1 && 0 < p < 1, {X, p}]
Out[1]= 0<X<1 && p == Root[12 - 47*X + 74*X^2 - 59*X^3 + 24*X^4 - 4*X^5 + (-24 +
108*X - 192*X^2 + 168*X^3 - 72*X^4 + 12*X^5)*#1 + (-48*X + 144*X^2 - 156*X^3 +
72*X^4 - 12*X^5)*#1^2 + (-32*X^2 + 48*X^3 - 24*X^4 + 4*X^5)*#1^3 & , 1]
Root is a Mathematica function representing a root of a usually complicated polynomial
that would often be much larger if the actual root were written out in algebra, but we
can see whether the result is understandable enough to be useful by using ToRadicals.
Often Reduce will return several different alternatives using && (and) and || (or) to
let you see the details you must understand to correctly use the result. See how I
copy the entire Root[...] and put that inside ToRadicals. Notice how Reduce returns
answers that include information about the ranges of variables. And see how I give Simplify the domain information about X to allow it to provide the greatest possible simplification.
In[2]:= Simplify[ToRadicals[Root[12 - 47 X + 74 X^2 - 59 X^3 + 24 X^4 - 4 X^5 +
(-24 + 108 X - 192 X^2 + 168 X^3 - 72 X^4 + 12 X^5) #1 + (-48 X + 144 X^2 -
156 X^3 + 72 X^4 - 12 X^5) #1^2 + (-32 X^2 + 48 X^3 - 24 X^4+ 4 X^5)#1^3&,1]],
0 < X < 1]
Out[2]= (8*X - 24*X^2 + 26*X^3 - 12*X^4 + 2*X^5 + 2^(1/3)*(-((-2 + X)^8*(-1 +
X)^2*X^3))^(1/3))/(2*(-2 + X)^3*X^2)
So your desired answer of where z= 0 will be where X is not zero, to avoid 0/0 in
your original equation, and where 0 < X < 1, 0 < p < 1 and where p is a root of that
final complicated expression in X. That result is a fraction and to be a root you
might take a look at where the numerator is zero to see if you can get any more
information about what you are looking for.
Sometimes you can learn something by plotting an expression. If you try to plot that final result you may end up with axes, but no plot. Perhaps the denominator is causing problems. You can try plotting just the numerator. You may again get an empty plot. Perhaps it is your cube root giving complex values. So you can put your numerator inside Re[] and plot that, then repeat that but using Im[]. Those will let you plot just the real and imaginary parts. You are doing this to try to understand where the roots might be. You should be cautious with plots because sometimes, particularly for complicated nasty expressions, the plot can make mistakes or hide desired information from you, but when used with care you can often learn something from this.
And, as always, test this and everything else very carefully to try to make sure that no mistakes have been made. It is too easy to "type some stuff into Mathematica, get some stuff out", think you have the answer and have no idea that there are significant errors hidden.

mathematica simplify denominator

In this algebraic expression, how to force Mathematica to calculate first a common
denominator and then to simplify:
(32768 Sqrt[\[Pi]] Va)/((7/b^2 + 2/mua^2)^(3/2) mua^3) +
(32 Sqrt[\[Pi]] Va)/((b^2 + mua^2)^(3/2) (2/b^2 + 1/(b^2 + mua^2))^(3/2))
+ (6144 Sqrt[\[Pi]] Va)/((b^2 + mua^2)^( 3/2) (3/b^2 + 1/(b^2 + mua^2))^(3/2))
the expression is too long, this is only a part.
Use
Together[ (your expression here) ]
For this expression it works with Together[] and then Apart[]. But for a larger expression with 50 terms like these I used Expand[expression]//Factor, and then again Expand[expression] to separate the terms.

How can I get rid of irritating prefactor of one in Mathematica?

I am trying to refine algebraic expressions into a convenient form using Mathematica to make sure I don't drop a sign or make some other trivial slip. After a lot of swearing, I have come to accept that this is not a deterministic process and I will have to do it step by step and also that the algebraic manipulation palette is my friend. However, there is still one thing that's driving me nuts. Sometimes mathematica spits out expressions with these seemingly extraneous leading ones. For example, right now I'm looking at:
0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2])
when I would much rather be looking at:
0.5*(Log[-a^2 + bigr^2] - Log[a^2 - 2.*a*bigr + bigr^2])
It's more than just a cosmetic problem because it confuses Factor[] when I try to apply it to some of the obvious quadratic factorizations in the above expression. Any clean fixes?
your = 0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2])
your /. {1. -> 1, -1. -> -1}
(* -> 0.5 (Log[-a^2 + bigr^2] - Log[a^2 - 2. a bigr + bigr^2]) *)
The dot after the 1 tells mathematica to treat the number as a non-exact quantity. So for example
1. * 1/2
(* -> .5 *)
but
1 * 1/2
(* -> 1/2 *)
Use exact quantities in your calculations (1, 2, 1/2) instead of decimal numbers (1., 2. ,0.5) if you need exact results
expr = 0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2]);
Convert all approximate numbers to precise:
expr // Rationalize
1/2 (Log[-a^2 + bigr^2] - Log[a^2 - 2 a bigr + bigr^2])
Selectively:
expr /. x_ /; x == 1 -> 1
0.5 (Log[-1. a^2 + bigr^2] - 1. Log[a^2 - 2. a bigr + bigr^2])

Resources