I'm facing this problem, I want to derivate an expression with respect to a variable depending on another variable (not the actual expressions I'm using, they're way more long and complex):
y := x^2 + x + 1;
z := sqrt[y];
D[z, y]
General::ivar: 1+x+x^2 is not a valid variable.
I saw I can solve the problem if I expand the variables like this
D[sqrt[1 + x + x^2], x]
but for long expressions it doesn't seem viable. Is there a simpler way to solve this problem?
Thank you.
You are not consistent with your notations. If you replace D[z, y] by D[z, x], you will get the desired answer.
y := x^2 + x + 1;
z := Sqrt[y];
D[z, x]
Related
Say that x and y are real numbers and y > 0. And say that I want to find for which values of A do (A + x + y > 0) and (A + x - y > 0) always hold, as long as x, y are in the domain.
How would I specify that on Wolfram Alpha? (Note: obviously these equations have no solution, but I just used it as an example.)
Or, if not on Wolfram, what software/website could I use?
I tried to write: solve for A: [input my first equation], y>0
but that didn't work, as it only gave integer solutions for when A, x, and y vary, instead of finding values of A such that it always holds no matter what x, y are.
https://www.wolframalpha.com/input?i=%28A+%2B+x+%2B+y+%3E+0%29+and+%28A+%2B+x+-+y+%3E+0%29+
[x>-A, -A - x<y<A + x]
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.
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]
Running into a problem with the following example code for which I hope there is a way around.
Say I have defined a function:
f[x_,y_,z_] = x + y + z + x Log[x] + y Log[y] +z Log[z]
and I was to assign
f[x_,y_,z_] = x + y + z + x Log[x] + y Log[y] +z Log[z]//.x->1//.y->1//.z->0
But rather than have Mathematica replace z with 0 I just want z to be ignored to give the result f[x_,y_] = 2 without having to define a new function. Entering the above code into Mathematica results in an obvious Indeterminate solution
Helping this novice out is greatly appreciated.
Assuming that you want the treatment you describe for z to apply to x and y as well, you could do this:
f[x_, y_, z_] := g[x] + g[y] + g[z]
g[0] = 0;
g[x_] := x + x Log[x]
The helper function g handles the zero case explicitly. These definitions yield results like these:
f[1, E, E^2]
(* 1 + 2*E + 3*E^2 *)
f[1, 1, 1]
(* 3 *)
f[1, 1, 0]
(* 2 *)
f[0, 0, E]
(* 2*E *)
First, function application occurs by calling the function:
f[1,1,1]
Second, why not introduce a new function using limit?
f[x_,y_,z_] := x + y + z + x*Log[x] + y*Log[y] +z*Log[z]
g[x_,y_]:=Limit[f[x,y,z],z->0]
g[1,1]
That should give you the 2, though I'm not in front of mathematica now so i havent checked
For example, I have symbollically
1/n*Sum[ee[k] + 1, {k, j, n}]^2
And I want to substitute Sum[ee[k], {k, j+1, n}] to be x. How can I do this? May thanks for your help!
You may use the recurrence relation for the sum. For example:
f[j] := f[j + 1] + (ee[j] + 1);
1/N f[j]^2 /. f[j + 1] -> x
Out
(1 + x + ee[j])^2/N
Edit
Based on several questions you posted, I think you are somehow misinterpreting what the Replace[] command does. It is not "algebraic" based, but "pattern" based. It doesn't understand nor use more algebraic transformations than those already defined (by you or by Mma itself).
For example:
x/. (x-1)->y
will not match anything. But
(x-1) /. x->y-1
Will give you (y-2) because the pattern x is matched.
Moreover:
x = 3;
(x - 1) /. x -> y - 1
will give you 2 because x is evaluated before the possible match, and the x in the pattern is also evaluated (just paste, execute and look at the symbol color).
1/N*Sum[ee[k] + 1, {k, j, N}]^2 /. Sum[ee[k] + 1, {k, j, N}] -> x
Doesn't that work, or do I misunderstand? By the way, you shouldn't use N as a variable. It's a Mathematica function.