Skip to main content

calculus and analysis - Solution to a specific problem caused by generic simplification


I'm trying to get MMA to help me evaluate certain integrals of trig functions. Here is an example: (The actual expressions I want to evaluate are more complicated than this one, but this illustrates the problem.)



Assuming[
n ∈ Integers && m ∈ Integers,
Integrate[Cos[n π x] Cos[m π x], {x, 0, 1}]
]
(* 0 *)

This answer is of course wrong, strictly speaking. The correct answer is $\frac{1}{2}\left(\delta_{n,m}+\delta_{n,-m}\right)$. As discussed here, for instance, MMA aims to produce generically correct results, and the special case $m=\pm n$ ends up being overlooked. I understand all that.


My question is whether anyone can suggest a straightforward workaround to get MMA to produce a more generally correct result for slightly more complex cases such as Integrate[Sin[k π x] Cos[n π x] Sin[m π x], {x,0,1}].


To clarify, I will add that there is no difficulty evaluating the special cases, if you know what they are. For instance:


Assuming[

n ∈ Integers,
Integrate[Cos[n π x] Cos[n π x], {x, 0, 1}]
]
(* 1/2 *)

So this is one of those vexing questions where it's easy to find the answer, once you know what it is. And of course you can use trig identities to get the answer, but getting the signs right is a tedious, fiddly business. I'd like to let MMA do it for me.


In case anyone cares, these integrals arise from PDEs when the solutions are represented as a cosine series.


Thanks.



Answer



Here is a fully automated solution, using ideas from my previous answer and Dr. Hintze's. It is in the form of two functions: trigIntegralToPiecewise and piecewiseToDelta. trigIntegralToPiecewise takes the thing to be integrated (as a pure function of the integration variable) and a list of the integer variables. It returns a Function that evaluates to a Piecewise function of the variables. If trigIntegralToPiecewise returns successfully, this piecewise function should always be equivalent to the integral.



The function can then be used as input to piecewiseToDelta. This is a bit of a kludge but seems to work fairly well. It is called using something like:


{df, check1, check2} = piecewiseToDelta[pwf, {k, m, n}];

df1 is now a pure function of k, m, n that evaluates to a sum of Kronecker deltas. (This is useful not only because the delta form is typically more compact and easier to understand than the Piecewise form, but also because MMA is good at simplifying sums over Kronecker deltas.) It is not guaranteed to be equivalent to pwf. The check1 and check2 returns allow you to reassure yourself. check1 is an Inactive expression using Reduce that, activated, should evaluate to True if the two functions are equivalent. I have not always had good results with Reduce, so check2 is something else. check2[10] evaluates pwf[k,m,n]==df[k,m,n] for $k$, $m$, and $n$ from -10 to 10 and returns True if all equalities hold.


trigIntegralToPiecewise::nonzero = 
"works only for generically zero integrals";

trigIntegralToPiecewise[
func_,
vars_

] := Module[{ri, num, den, dvs, cases, res, pw, pwfunc},
ri = Integrate[func[x], {x, 0, 1}];
num = Numerator[Together[ri]];
den = Denominator[Together[ri]];
If[0 =!= Simplify[num, Assumptions -> vars \[Element] Integers],
Message[trigIntegralToPiecewise::nonzero];
Return[$Failed]
];
cases = BooleanConvert[Reduce[0 == den, vars]];
cases = If[Head[cases] === Or,

List @@ cases,
{cases}
];
cases = Reverse[Subsets[cases]];
cases = And @@@ cases;
cases = DeleteDuplicates[Reduce[#, vars] & /@ cases];
pw = Table[
res = Simplify[
Integrate[func[x], {x, 0, 1},
Assumptions -> case],

vars \[Element] Integers && case
];
{res, case},
{case, cases}
];
pw = Append[pw, {0, True}]; (* To be safe *)
pwfunc = Function @@ {
vars,
Piecewise[
pw

]
};
pwfunc
]

piecewiseToDelta[
pwf_,
vars_,
max_: 3,
extrabasis_: {}

] := Module[{nv, basis, kds, iterators, lb, ub, cs, eqs, cvals,
dfunc, check1, check2},
nv = Length[vars];
iterators =
Transpose[{vars, ConstantArray[lb, nv], ConstantArray[ub, nv]}];
basis = Join[{1}, vars, extrabasis];
kds = Plus @@@ Tuples[Outer[Times, Drop[vars, 1], {1, -1}]];
kds = Sum[
(Sum[C[i, j] basis[[j]], {j, Length[basis]}]) KroneckerDelta[
vars[[1]], kds[[i]]],

{i, Length[kds]}
];
cs = Flatten@Table[
C[i, j],
{j, Length[basis]}, {i, Length[kds]}
];
eqs = Table @@
Prepend[iterators /. {lb -> -max, ub -> max},
kds == pwf @@ vars];
eqs = DeleteCases[Flatten[eqs], True];

cvals = Solve[eqs, cs][[1]];
dfunc = Function @@ {
vars,
kds /. cvals /. C[_, _] -> 0
};
check1 = Inactivate[
Assuming[
vars \[Element] Integers,
Simplify[
Reduce[pwf @@ vars == dfunc @@ vars]

]
]
];
check2[cmax_, it_: iterators] := Module[{iters},
iters = it /. {lb -> -cmax, ub -> cmax};
And @@ Flatten[
Table @@ Prepend[
iters,
pwf @@ vars == dfunc @@ vars
]

]
];
{dfunc, check1, check2}
]

Example:


pwf1 = trigIntegralToPiecewise[x ↦ Sin[k 𝝿 x] Cos[n 𝝿 x] Sin[m 𝝿 x], {k, m, n}];

{df1, check11, check21} = piecewiseToDelta[pwf1, {k, m, n}];
df1 // TraditionalForm


$$\{k,m,n\}↦-\frac{1}{4} \delta _{k,n-m}-\frac{1}{4} \delta _{k,-m-n}+\frac{\delta _{k,m-n}}{4}+\frac{\delta _{k,m+n}}{4}$$


check11[20]
(* True *)

Activate@check21
(* True *)

Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.