guys! Sorry in advance about this.
Let's say I want to convolve two functions (f and g), a gaussian with a breit-wigner:
f[x_] := 1/(Sqrt[2 \[Pi]] \[Sigma])Exp[-(1/2) ((x - \[Mu])/\[Sigma])^2];
g[x_] := 1/\[Pi] (\[Gamma]/((x - \[Mu])^2 + \[Gamma]^2));
One way is to use Convolve like:
Convolve[f[x],g[x],x,y];
But that gives:
(\[Gamma] Convolve[E^(-((x - \[Mu])^2/(2 \[Sigma]^2))),1/(\[Gamma]^2 + (x - \[Mu])^2), x, y])/(Sqrt[2] \[Pi]^(3/2) \[Sigma])
,which means it couldn't do the convolution.
I then tried the integration (the definition of the convolution):
Integrate[f[x]*g[y - x], {x, 0, y}, Assuptions->{x > 0, y > 0}]
But again, it couldn't integrate. I know that there are functions that can't be integrated analytically, but it seems to me that whenever I go into convolution, I find another function that can't be integrated.
Is the numerical integration the only way to do convolution in Mathematica (besides those simple functions in the examples), or am I doing something wrong?
My target is to convolute a crystal-ball with a breit-weigner. The CB is something like:
Piecewise[{{norm*Exp[-(1/2) ((x - \[Mu])/\[Sigma])^2], (
x - \[Mu])/\[Sigma] > -\[Alpha]},
{norm*(n/Abs[\[Alpha]])^n*
Exp[-(1/2) \[Alpha]^2]*((n/Abs[\[Alpha]] - Abs[\[Alpha]]) - (
x - \[Mu])/\[Sigma])^-n, (x - \[Mu])/\[Sigma] <= -\[Alpha]}}]
I've done this in C++ but I thought I try it in Mathematica and use it to fit some data. So please tell me if I have to make a numerical integration routine in Mathematica or there's more to the analytic integration.
Thank you,
Adrian
I Simplified your functions a little bit(it might look little, but its huge in the spirit).
In this case I have set [Mu] to be zero.
\[Mu] = 0;
Now we have:
f[x_] := 1/(Sqrt[2 \[Pi]] \[Sigma]) Exp[-(1/2) ((x)/\[Sigma])^2];
g[x_] := 1/\[Pi] (\[Gamma]/((x)^2 + \[Gamma]^2));
Asking Mathematica to Convolve:
Convolve[f[x], g[x], x, y]
-((I E^(-((y + I \[Gamma])^2/(2 \[Sigma]^2))) (E^((2 I y \[Gamma])/\[Sigma]^2) \[Pi] Erfi[((y - I \[Gamma]) Sqrt[1/\[Sigma]^2])/Sqrt[2]] - \[Pi] Erfi[((y + I \[Gamma]) Sqrt[1/\[Sigma]^2])/Sqrt[2]] - Log[-y - I \[Gamma]] - E^((2 I y \[Gamma])/\[Sigma]^2) Log[y - I \[Gamma]] + E^((2 I y \[Gamma])/\[Sigma]^2) Log[-y + I \[Gamma]] + Log[y + I \[Gamma]]))/(2 Sqrt[2] \[Pi]^(3/2) \[Sigma]))
Although this is not precisely what you asked for, but it shows if your function was a tiny bit simpler, Mathematica would be able to do the integration. In the case of your question, unless we know some more information about [Mu], I don't think the result of Convolve has a closed form. You can probably ask math.stackexchange.com guys about your integral and see if someone comes up with a closed form.
Related
How to calculate the integral with the condition in Wolfram like
Integral(1/(x^4 + y^4)dxdy) where y >=x^2+1
I assume you mean the definite integral over all x, y>x^2+1. The syntax is this:
Integrate[1/(x^4 + y^4), {x, -Infinity, Infinity},{ y, x^2 + 1,Infinity}]
Note mathematica's ordering of the integration variables is reverse of standard convention, ie. left to right is outside to inside. This takes quite a while to report that it does not converge. However the numerical integration gives a result:
NIntegrate[1/(x^4 + y^4), {x, -Infinity, Infinity},{ y, x^2 + 1,Infinity}]
0.389712
My guess is the numeric result is correct and mathematica is simply wrong about the analytic convergence. You might try math.stackexchange.com or mathematica.stackexchange.com if you need to prove convergence. I am doubtful there is a nice analytic result.
How about
Integrate[1/(x^4 + y^4), y, x, Assumptions -> y >= x^2 + 1]
note also Multiple Integral
I just started to use Mathematica and wanted to try out if I got a result correct from a residue integral i made. I have two poles in the UHP, actually looking more complicated but I just wanted to see if Mathematica could to this. This is my code:
x1 = Iw;
x2 = 2 Iw;
Integrate [e^(Ixt)/((x - x1) (x - x2)), {x, -infinity, infinity}, Assumptions -> t > 0]
Is this integral possible to do in Mathematica?
Mathematica sees Iw as a two character variable name, but I w as the product of two things.
Mathematica is fanatic about correct capitalization and spelling.
x1=I w; x2=2 I w;
Integrate[E^(I x t)/((x-x1)(x-x2)), {x,-Infinity,Infinity}, Assumptions-> t>0]
returns a large complicated result depending on the signs and whether w might be zero. If you can supply additional domain information the result might be simplified.
I'm trying to obtain the real part of the result of an operation which involves an undefined variable (let's say x).
How can I have Mathematica return x when I execute Re[x] if I know that x will never be a complex number? I think this involves telling Mathematica that x is a real, but I don't know how.
In my case the expression for which I want the real part is more complicated than a simple variable, but the concept will remain the same.
Some examples:
INPUT OUTPUT DESIRED RESULT
----- ------ --------------
Re[x] Re[x] x
Re[1] 1 1
Re[Sin[x]] Re[Sin[x]] Sin[x]
Re[1+x+I] 1 + Re[x] 1+x
Re[1 + x*I] 1-Im[x] 1
You can use for example the input Simplify[Re[x], x \[Element] Reals] which will give x as output.
Use ComplexExpand. It assumes that the variables are real unless you indicate otherwise. For example:
In[76]:= ComplexExpand[Re[x]]
Out[76]= x
In[77]:= ComplexExpand[Re[Sin[x]]]
Out[77]= Sin[x]
In[78]:= ComplexExpand[Re[1+x+I]]
Out[78]= 1+x
Two more possibilities:
Assuming[x \[Element] Reals, Refine[Re[x]]]
Refine[Re[x], x \[Element] Reals]
Both return x.
It can at times be useful to define UpValues for a symbol. This is far from robust, but it nevertheless can handle a number of cases.
Re[x] ^= x;
Im[x] ^= 0;
Re[x]
Re[1]
Re[1 + x + I]
Re[1 + x*I]
x
1
1 + x
1
Re[Sin[x]] does not evaluate as you desire, but one of the transformations used by FullSimplify does place it in a form that triggers Re[x]:
Re[Sin[x]] // FullSimplify
Sin[x]
I came across some weird behaviour when using GroebnerBasis. In m1 below, I used a Greek letter as my variable and in m2, I used a Latin letter. Both of them have no rules associated with them. Why do I get vastly different answers depending on what variable I choose?
Image:
Copyable code:
Clear["Global`*"]
g = Module[{x},
x /. Solve[
z - x (1 - b -
b x ( (a (3 - 2 a (1 + x)))/(1 - 3 a x + 2 a^2 x^2))) == 0,
x]][[3]];
m1 = First#GroebnerBasis[\[Kappa] - g, z]
m2 = First#GroebnerBasis[k - g, z]
EDIT:
As pointed out by belisarius, my usage of GroebnerBasis is not entirely correct as it requires a polynomial input, whereas mine is not. This error, introduced by a copy-pasta, went unnoticed until now, as I was getting the answer that I expected when I followed through with the rest of my code using m1 from above. However, I'm not fully convinced that it is an unreasonable usage. Consider the example below:
x = (-b+Sqrt[b^2-4 a c])/2a;
p = First#GroebnerBasis[k - x,{a,b,c}]; (*get relation or cover for Riemann surface*)
q = First#GroebnerBasis[{D[p,k] == 0, p == 0},{a,b,c},k,
MonomialOrder -> EliminationOrder];
Solve[q==0, b] (*get condition on b for double root or branch point*)
{{b -> -2 Sqrt[a] Sqrt[c]}, {b -> 2 Sqrt[a] Sqrt[c]}}
which is correct. So my interpretation is that it is OK to use GroebnerBasis in such cases, but I'm not all too familiar with the deep theory behind it, so I could be completely wrong here.
P.S. I heard that if you mention GroebnerBasis three times in your post, Daniel Lichtblau will answer your question :)
The bug that was shown by these examples will be fixed in version 9. Offhand I do not know how to evade it in versions 8 and prior. If I recall correctly it was caused by an intermediate numeric overflow in some code that was checking whether a symbolic polynomial coefficient might be zero.
For some purposes it might be suitable to specify more variables and possibly a non-default term order. Also clearing denominators can be helpful at least in cases where that is a valid thing to do. That said, I do not know if these tactics would help in this example.
I'll look some more at this code but probably not in the near future.
Daniel Lichtblau
This may be related to the fact that Mathematica does not try all variable orders in functions like Simplify. Here is an example:
ClearAll[a, b, c]
expr = (c^4 b^2)/(c^4 b^2 + a^4 b^2 + c^2 a^2 (1 - 2 b^2));
Simplify[expr]
Simplify[expr /. {a -> b, b -> a}]
(b^2 c^4)/(a^4 b^2 + a^2 (1 - 2 b^2) c^2 + b^2 c^4)
(a^2 c^4)/(b^2 c^2 + a^2 (b^2 - c^2)^2)
Adam Strzebonski explained that:
...one can try FullSimplify with all
possible orderings of chosen
variables. Of course, this multiplies
the computation time by
Factorial[Length[variables]]...
Let's say I have a relation r^2 = x^2 + y^2. Now suppose after a calculation i get a complicated output of x and y, but which could in theory be simplified a lot by using the above relation. How do I tell Mathematica to do that?
I'm referring to situations where replacement rules x^2+y^2 -> r^2 and using Simplify/FullSimplify with Assumptions won't work, e.g. if the output is x/y + y/x = (x^2+y^2)/(xy) = r^2/(xy).
Simplification works really well with built in functions but not with user defined functions! So essentially I would like my functions to be treated like the built in functions!
I believe you are looking for TransformationFunctions.
f = # /. x^2 + y^2 -> r^2 &;
Simplify[x/y + y/x, TransformationFunctions -> {Automatic, f}]
(* Out= r^2/(x y) *)
In the example you give
(x/y + y/x // Together) /. {x^2 + y^2 -> r^2}
==> r^2/(x y)
works. But I've learned that in many occasions replacements like this don't work. A tip I once got was to replace this replacement with one which has a more simpler LHS like: x^2 -> r^2-y^2 (or even x->Sqrt[r^2-y^2] if you know that the values of x and y allow this).