Proper way to simplify integral result in Mathematica given integer constraints - wolfram-mathematica

Evaluating the following integral should be non-zero, and mathematica correctly gives a non-zero result
Integrate[ Cos[ (Pi * x)/2 ]^2 * Cos[ (3*Pi*x)/2 ]^2, {x, -1, 1}]
However, attempting a more general integral:
FullSimplify[
Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2],
{x, -1, 1}],
Element[{m, n}, Integers]]
yields zero, which is definitely not true for m = n = 1
I'd expect a conditional expression. Is it possible to "tell" mathematica about my constraints on m and n before the integral is evaluated so that it handles the special cases properly?

While I'm late to the party, no one has given a complete solution, thus far.
Sometimes, it pays to understand the integrand better before you integrate. Consider,
ef = TrigReduce[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2]]/.
Cos[a_] :> Cos[ Simplify[a, Element[{m,n}, Integers] ] ]
which returns
(2 Cos[(m - n) Pi x] + Cos[(1 + m - n) Pi x] + Cos[(1 - m + n) Pi x] +
Cos[(m + n) Pi x] + 2 Cos[(1 + m + n) Pi x] + Cos[(2 + m + n) Pi x] )/8
where each term has the form Cos[q Pi x] with integral q. Now, there are two cases to consider when integrating Cos[q Pi x] over -1 to 1 (where q is integral): q == 0 and q != 0.
Case q = 0: This is a special case that Mathematica misses in the general result, as it implies a constant integrand. (I'll often miss it, also, when doing this by hand, so Mathematica isn't entirely to blame.) So, the integral is 2, in this case.
Strictly speaking, this isn't true. When told to integrate Cos[ q Pi x ] over -1 < x < 1, Mathematica returns
2 Sin[ Pi q ]/( Pi q )
which is 0 except when q == 0. At that point, the function is undefined in the strict sense, but Limit[Sin[x]/x, q -> 0] == 1. As the singularity at q == 0 is removable, the integral is 2 when q -> 0. So, Mathematica does not miss it, it is just in a form not immediately recognized.
Case q != 0: Since Cos[Pi x] is periodic with period 2, an integral of Cos[q Pi x] from x == -1 to x == 1 will always be over q periods. In other words,
Integrate[ Cos[q Pi x], {x, -1, 1},
Assumptions -> (Element[ q, Integers ] && q != 0) ] == 0
Taken together, this means
Integrate[ Cos[q Pi x], {x, -1, 1}, Assumptions -> Element[ q, Integers ] ] ==
Piecewise[{{ q == 0, 2 }, { 0, q!=0 }}]
Using this, we can integrate the expanded form of the integrand via
intef = ef /. Cos[q_ Pi x] :> Piecewise[{{2, q == 0}, {0, q != 0}}] //
PiecewiseExpand
which admits non-integral solutions. To clean that up, we need to reduce the conditions to only those that have integral solutions, and we might as well simplify as we go:
(Piecewise[{#1,
LogicalExpand[Reduce[#2 , {m, n}, Integers]] //
Simplify[#] &} & ### #1, #2] & ## intef) /. C[1] -> m
\begin{Edit}
To limit confusion, internally Piecewise has the structure
{ { { value, condition } .. }, default }
In using Apply (##), the condition list is the first parameter and the default is the second. To process this, I need to simplify the condition for each value, so then I use the second short form of Apply (###) on the condition list so that for each value-condition pair I get
{ value, simplified condition }
The simplification process uses Reduce to restrict the conditions to integers, LogicalExpand to help eliminate redundancy, and Simplify to limit the number of terms. Reduce internally uses the arbitrary constant, C[1], which it sets as C[1] == m, so we set C[1] back to m to complete the simplification
\end{Edit}
which gives
Piecewise[{
{3/4, (1 + n == 0 || n == 0) && (1 + m == 0 || m == 0)},
{1/2, Element[m, Integers] &&
(n == m || (1 + m + n == 0 && (m <= -2 || m >= 1)))},
{1/4, (n == 1 + m || (1 + n == m && (m <= -1 || m >= 1)) ||
(m + n == 0 && (m >= 1 || m <= 0)) ||
(2 + m + n == 0 && (m <= -1 || m >= 0))) &&
Element[m, Integers]},
{0, True}
}
as the complete solution.
Another Edit: I should point out that both the 1/2 and 1/4 cases include the values for m and n in the 3/4 case. It appears that the 3/4 case may be the intersection of the other two, and, hence, their sum. (I have not done the calc out, but I strongly suspect it is true.) Piecewise evaluates the conditions in order (I think), so there is no chance of getting this incorrect.
Edit, again: The simplification of the Piecewise object is not as efficient as it could be. At issue is the placement of the replacement rule C[1] -> m. It happens to late in the process for Simplify to make use of it. But, if it is brought inside the LogicalExpand and assumptions are added to Simplify
(Piecewise[{#1,
LogicalExpand[Reduce[#2 , {m, n}, Integers] /. C[1] -> m] //
Simplify[#, {m, n} \[Element] Integers] &} & ### #1, #2] & ## intef)
then a much cleaner result is produce
Piecewise[{
{3/4, -2 < m < 1 && -2 < n < 1},
{1/2, (1 + m + n == 0 && (m >= 1 || m <= -2)) || m == n},
{1/4, 2 + m + n == 0 || (m == 1 + n && m != 0) || m + n == 0 || 1 + m == n},
{0, True}
}]

Not always zero ...
k = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2],
{x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];
(*Let's find the zeroes of the denominator *)
d = Denominator[k];
s = Solve[d == 0, {m, n}]
(*The above integral is indeterminate at those zeroes, so let's compute
the integral again there (a Limit[] could also do the work) *)
denZ = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] /.s,
{x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];
(* All possible results are generated with m=1 *)
denZ /. m -> 1
(*
{1/4, 1/2, 1/4, 1/4, 1/2, 1/4}
*)
Visualizing those cases:
Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2]
/. s /. m -> 1, {x, -1, 1}]
Compare with a zero result integral one:
Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2]
/. {m -> 1, n -> 4}, {x, -1, 1}]

If you just drop the whole FullSimplify part, mathematica does the integration neatly for you.
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}]
To include the condition that m and n are integers, it's better to use the Assumptions option in Integrate.
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]]

Lets use some conclusive conditions about the two integers m=n||m!=n.
Assuming[{(n \[Element] Integers && m \[Element] Integers && m == n)},
Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2],
{x, -1, 1}]]
The answer for this case is 1/2. For the other case it is
Assuming[{(n \[Element] Integers && m \[Element] Integers && m != n)},
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}]]
and the answer is 0.
However I am amazed to see that if we add this two conditions as an "either or stuff", Mathematica returns one zero after integration. I mean in case of the following I am getting only zero but not ``1/2||0`.
Assuming[{(n \[Element] Integers && m \[Element] Integers &&
m == n) || (n \[Element] Integers && m \[Element] Integers &&
m != n)},
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}]]
By the way we can see the conditions exclusively where this integral becomes Indeterminate.
res = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}] // Simplify
The output is here.
Now lets see all the relations m and n can have to make the Integral bad!
BadPart = (res*4 Pi);
Flatten#(Solve[(Denominator[#] == 0), m] & /#
Table[BadPart[[i]], {i, 1, Length#BadPart}] /.
Rule -> Equal) // TableForm
So these are the special cases which as Sjoerd mentioned are having infinite instances.
BR

Related

How to do numerical inversion of inverse laplace transform in mathematica?

I am trying to calculate the inversion Laplace transform of,
F(s) = Erfc[s]
at t = 100
I have tried the following way using Stehfest method(76 Mathematical Journal, 1994),
csteh[n_, i_] = (-1)^(i + n/2) Sum[k^(n/2)(2k) !/((n/2 - k) ! k ! ( k -1 ) !(i - k) !(2k - i) !), {k, Floor[(i + 1)/2], Min[i, n/2]}];
NLInvSteh[F_, s_, t_, n_] := log[2]/t Sum[ csteh[n,i] F /.s -> i log[2]/t, {i, 1, n}] //N
My function:
F[s_] = Erfc[s]
%NLInvSteh[F[s], s, t, N]
NLInvSteh[F[s], s, 100, 6]
The output is-
(Erfc[log[2.]]-49. Erfc[2. log[2.]]+366. Erfc[3. log[2.]]-858. Erfc[4. log[2.]]+810. Erfc[5. log[2.]]-270. Erfc[6. log[2.]]) log[2.]
Can we get the simplify value of the output.
Use upper case Log.
NLInvSteh[F_, s_, t_, n_] :=
Log[2]/t Sum[csteh[n, i] F /. s -> i Log[2]/t, {i, 1, n}] // N
NLInvSteh[F[s], s, 100, 6]
0.000052055

transform equation with Mathematica

I'm try to use Mathematica to transform a couple of komplex equations, but I didn't get the right syntax.
Solve[{EI*w[x] == Piecewise[{{(x - a)^3, x >= a}, {0, x < a}}]*F*(1/6) +
ca*(x³)*(1/6) + cb*(x^2)*0.5 + cc*x + cd,
EI*w'[x] == Piecewise[{{(x - a)², x >= a}, {0, x < a}}]*F*0.5 + ca*(x^2)*0.5 + cb*x + cc,
EI*w''[x] == Piecewise[{{(x - a)^1, x >= a}, {0, x < a}}]*F + ca*x*0.5 + cb,
EI*w'''[x] == Piecewise[{{(x - a)^0, x >= a}, {0, x < a}}]*F + ca,
w[0] == -EIw'''[0]/ka, w[l] == EIw'''[0]/kb, w'[0] == 0, w'[l] == 0},
{ca, cb, cc, cd}]
Can someone give me a hint how to solve these equations with Mathematica.
Begin by cleaning up some typesetting issues.
We simplify this by looking only at the domain x>=a
Solve[{EI*w[x] == (x-a)^3*F*(1/6)+ca*(x^3)*(1/6)+cb*(x^2)*(1/2)+cc*x+cd,
EI*w'[x] == (x-a)^2*F*(1/2)+ca*(x^2)*(1/2)+cb*x+cc,
EI*w''[x] == (x-a)^1*F+ca*x*(1/2)+cb,
EI*w'''[x] == (x-a)^0*F+ca,
w[0] == -EI*w'''[0]/ka, w[1] == EI*w'''[0]/kb, w'[0] == 0, w'[1] == 0},
w[x], x]
Your second equation is just derivative of first equation and provides no additional information
In[1]:= D[(x-a)^3*F*(1/6)+ca*(x^3)*(1/6)+cb*(x^2)*(1/2)+cc*x+cd, x] ==
(x-a)^2*F*(1/2)+ca*(x^2)*(1/2)+cb*x+cc
Out[1]= True
Your third and fourth equations give information about ca
In[2]:= Simplify[D[(x-a)^1*F+ca*x*(1/2)+cb, x] == (x-a)^0*F+ca]
Out[2]= ca == 0
OR it seems more and more likely that this is the result of a typo in the equations from the original poster. Without more information from the original poster it isn't possible to decide whether this is correct or not. But the person should be able to look at the method used and get the solution needed.
Thus
w[x] == ((x-a)^3*F*(1/6)+cb*(x^2)*(1/2)+cc*x+cd)/EI
In[3]:= w'[x] == D[((x-a)^3*F*(1/6)+cb*(x^2)*(1/2)+cc*x+cd)/EI, x]
Out[3]= w'[x] == (cc+cb x+1/2 F (-a+x)^2)/EI
In[4]:= w'''[x] == D[((x-a)^3*F*(1/6)+cb*(x^2)*(1/2)+cc*x+cd)/EI, {x, 3}]
Out[4]= w'''[x] == F/EI
We already know ca, solve for the remaining three variables using the first three boundary conditions
In[5]:= Simplify[Solve[{((0-a)^3*F*(1/6)+cb*(0^2)*(1/2)+cc*0+cd)/EI == -EI*(F/EI)/ka,
((l-a)^3*F*(1/6)+cb*(l^2)*(1/2)+cc*l+cd)/EI == EI*(F/EI)/kb,
(cc+cb x+1/2 F (-a+0)^2)/EI == 0}, {cb, cc, cd}]]
Out[5]= {{cb -> (F (6 EI (ka+kb)+ka kb (3 a-l) l^2))/(3 ka kb l (l-2 x)),
cc -> -((F (3 a^2 ka kb l (l-2 x)+6 a ka kb l^2 x+
2 (6 EI (ka+kb)-ka kb l^3) x))/(6 ka kb l (l-2 x))),
cd -> 1/6 F (a^3-(6 EI)/ka)}}
Look at the final boundary condition
In[7]:= Simplify[(cc+cb x+1/2 F (-a+l)^2)/EI == 0 /. Out[5]]
Out[7]= {(F l (-2 a+l))/EI == 0}
Check every step of this carefully and verify that no errors were made.
Then go back and see if you can use these same methods for the domain x < a

Mathematica FullSimplify[Sqrt[5+2 Sqrt[6]]] yields Sqrt[2]+Sqrt[3] but FullSimplify[-Sqrt[5+2 Sqrt[6]]] is not simplified, why?

I was playing with the (beautiful) polynomial x^4 - 10x^2 + 1.
Look what happens:
In[46]:= f[x_] := x^4 - 10x^2 + 1
a = Sqrt[2];
b = Sqrt[3];
Simplify[f[ a + b]]
Simplify[f[ a - b]]
Simplify[f[-a + b]]
Simplify[f[-a - b]]
Out[49]= 0
Out[50]= 0
Out[51]= 0
Out[52]= 0
In[53]:= Solve[f[x] == 0, x]
Out[53]= {{x->-Sqrt[5-2 Sqrt[6]]},{x->Sqrt[5-2 Sqrt[6]]},{x->-Sqrt[5+2 Sqrt[6]]},{x->Sqrt[5+2 Sqrt[6]]}}
In[54]:= Simplify[Solve[f[x] == 0, x]]
Out[54]= {{x->-Sqrt[5-2 Sqrt[6]]},{x->Sqrt[5-2 Sqrt[6]]},{x->-Sqrt[5+2 Sqrt[6]]},{x->Sqrt[5+2 Sqrt[6]]}}
In[55]:= FullSimplify[Solve[f[x] == 0, x]]
Out[55]= {{x->Sqrt[2]-Sqrt[3]},{x->Sqrt[5-2 Sqrt[6]]},{x->-Sqrt[5+2 Sqrt[6]]},{x->Sqrt[2]+Sqrt[3]}}
Sqrt[5-2 Sqrt[6]] is equal to Sqrt[3]-Sqrt[2].
However, Mathematica's FullSimplify does not simplify Sqrt[5-2 Sqrt[6]].
Question: Should I use other more specialized functions to algebraically solve the equation? If so, which one?
Indeed, Solve doesn't simplify all roots to the max:
A FullSimplify postprocessing step simplifies two roots and leaves two others untouched:
Same initially happens with Roots:
Strange enough, now FullSimplify simplifies all roots:
The reason for this is, I assume, that for the default ComplexityFunction some of the solutions written above in nested radicals are in a sense simpler than the others.
BTW FunctionExpand knows how to deal with those radicals:
FullSimplify[ Solve[x^4-10x^2+1==0,x]
,
ComplexityFunction ->
(StringLength[ToString[
InputForm[#1]]] & )]
gives
{{x -> Sqrt[2] - Sqrt[3]}, {x -> -Sqrt[2] + Sqrt[3]}, {x -> -Sqrt[2] -
Sqrt[3]}, {x -> Sqrt[2] + Sqrt[3]}}

Using NProbability[] or Probability[] to work out the probability of 3 or more Heads from 4 coin tosses

Is it possible to work out the probability of 3 or more Head from 4 coin tosses using the Probability or NProbability functions.
This is not a question about the trivial answer to this problem, it is more to get an understanding of how to solve this kind of problem with Mathematica using distributions.
So using 4 random variables from Distribution P
I was hoping something like this would do the trick, but it does not work. I get 0.
P = BernoulliDistribution[0.5];
vars = List[Distributed[a,P],Distributed[b,P],Distributed[c,P],Distributed[c,P]];
NProbability[Count[ {a,b,c,d}, 1] >= 3, vars]
Any ideas would be greatly appreciated.
Not an expert using Mma for statistics here, but this seems to work:
l = TransformedDistribution[
x + y + w + z, {x \[Distributed] BernoulliDistribution[0.5],
y \[Distributed] BernoulliDistribution[0.5],
z \[Distributed] BernoulliDistribution[0.5],
w \[Distributed] BernoulliDistribution[0.5]}];
Table[NProbability[x > i, x \[Distributed] l], {i, -1, 4}]
(*
{1, 0.9375, 0.6875, 0.3125, 0.0625, 0.}
*)
In[10]:= Probability[a + b + c + d >= 3, vars]
Out[10]= 0.3125
Coin flipping is easier described with a BinomialDistribution:
In[12]:= Probability[m >= 3, m \[Distributed] BinomialDistribution[4, 0.5]]
Out[12]= 0.3125

How to ask mathematica to compute higher order derivatives evaluated at 0

I have a function, let's say for example,
D[x^2*Exp[x^2], {x, 6}] /. x -> 0
And I want to replace 6 by a general integer n,
Or cases like the following:
Limit[Limit[D[D[x /((-1 + x) (1 - y) (-1 + x + x y)), {x, 3}], {y, 5}], {x -> 0}], {y -> 0}]
And I want to replace 3 and 5 by a general integer m and n respectively.
How to solve these two kinds of problems in general in mma?
Many thanks.
Can use SeriesCoefficient, sometimes.
InputForm[n! * SeriesCoefficient[x^2*Exp[x^2], {x,0,n}]]
Out[21]//InputForm=
n!*Piecewise[{{Gamma[n/2]^(-1), Mod[n, 2] == 0 && n >= 2}}, 0]
InputForm[mncoeff = m!*n! *
SeriesCoefficient[x/((-1+x)*(1-y)*(-1+x+x*y)), {x,0,m}, {y,0,n}]]
Out[22]//InputForm=
m!*n!*Piecewise[{{-1 + Binomial[m, 1 + n]*Hypergeometric2F1[1, -1 - n, m - n,
-1], m >= 1 && n > -1}}, 0]
Good luck extracting limits for m, n integer, in this second case.
Daniel Lichtblau
Wolfram Research
No sure if this is what you want, but you may try:
D[x^2*Exp[x^2], {x, n}] /. n -> 4 /. x -> 0
Another way:
f[x0_, n_] := n! SeriesCoefficient[x^2*Exp[x^2], {x, x0, n}]
f[0,4]
24
And of course, in the same line, for your other question:
f[m_, n_] :=
Limit[Limit[
D[D[x/((-1 + x) (1 - y) (-1 + x + x y)), {x, m}], {y, n}], {x ->
0}], {y -> 0}]
These answers don't give you an explicit form for the derivatives, though.

Resources