Mathematica: Define a composite function in which the derivative of the argument appears - wolfram-mathematica

I am trying to write the following expression:
L[y[x]] = y'[x] - 1/h (a0 y[x - h] + a1 y[x] + a2 y[x + h])
I already saw an answer about something similar to this problem: f[y_]:=D[y,x]*2 and I understood the command of delayed definition. The problem is that in my case the argument x is important because I have to evaluate the function y in different points and this is giving me some issue.
How I can write the formula in a proper way?
Thanks in advance

I'm not exactly sure what you are trying to accomplish.
Is there any chance that this helps?
y[x_]:=Sin[x];
L[y_,x_] := (y'[z] - 1/h (a0 y[z - h] + a1 y[z] + a2 y[z + h]))/.z->x;
L[y,x]
L[y,2]
which returns
Cos[x] - (-(a0*Sin[h - x]) + a1*Sin[x] + a2*Sin[h + x])/h
and
Cos[2] - (a1*Sin[2] + a0*Sin[2 - h] + a2*Sin[2 + h])/h
That depends on z and perhaps x not previously having been assigned any values.
There are almost certainly other ways of doing that, like everything else in Mathematica.
Please test this VERY carefully before you even think of depending on this.

Related

How to display higher order terms first?

Is there a way to make Mathematica display higher order terms first? I want x^3 to come before x^2 etc. but this is what I get:
In[1]:= A = x^2 + x
Out[1]= x + x^2
In[2]:= B = 2 x + 3
Out[2]= 3 + 2 x
In[3]:= A + B
Out[3]= 3 + 3 x + x^2
I really just want to reverse the display of terms. How can I get something like this?
In[3]:= A + B
Out[3]= x^2 + 3 x + 3
Multivariate polynomials display curiously as well. Despite the order of the terms of this expression:
x^2 + y^4 + 3 x + 3 + xy^2 + y + y^3
I get
3 + 3 x + x^2 + xy^2 + y + y^3 + y^4
I'm not entirely sure of the best way of displaying this, but I know I want higher order terms first. Can this be done?
TraditionalForm may help you
3+x+x^2//TraditionalForm
displays as
x^2+x+3
but you should be cautious because TraditionalForm items cannot have any further sensible math done to them. For example, because of TraditionalForm has higher precedence than = it turned the polynomial into traditional form and then assigned the result.
A=3+x+x^2//TraditionalForm
Solve[A==0,x]
And then the calculation on that traditional form gives you:
{{x->1/2 (-1-Sqrt[-11+4 TraditionalForm^(-1)[0]},
{x->1/2 (-1+Sqrt[-11+4 TraditionalForm^(-1)[0]}}
But if you carefully keep separate those things that are pretty to look at from those things you are using to do further calculations with then this may work for you.

Mathematica integration

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.

Get mathematica to simplify expression with another equation

I have a very complicated mathematica expression that I'd like to simplify by using a new, possibly dimensionless parameter.
An example of my expression is:
K=a*b*t/((t+f)c*d);
(the actual expression is monstrously large, thousands of characters). I'd like to replace all occurrences of the expression t/(t+f) with p
p=t/(t+f);
The goal here is to find a replacement so that all t's and f's are replaced by p. In this case, the replacement p is a nondimensionalized parameter, so it seems like a good candidate replacement.
I've not been able to figure out how to do this in mathematica (or if its possible). I tried:
eq1= K==a*b*t/((t+f)c*d);
eq2= p==t/(t+f);
Solve[{eq1,eq2},K]
Not surprisingly, this doesn't work. If there were a way to force it to solve for K in terms of p,a,b,c,d, this might work, but I can't figure out how to do that either. Thoughts?
Edit #1 (11/10/11 - 1:30)
[deleted to simplify]
OK, new tact. I've taken p=ton/(ton+toff) and multiplied p by several expressions. I know that p can be completely eliminated. The new expression (in terms of p) is
testEQ = A B p + A^2 B p^2 + (A+B)p^3;
Then I made the substitution for p, and called (normal) FullSimplify, giving me this expression.
testEQ2= (ton (B ton^2 + A^2 B ton (toff + ton) +
A (ton^2 + B (toff + ton)^2)))/(toff + ton)^3;
Finally, I tried all of the suggestions below, except the last (not sure how it works yet!)
Only the eliminate option worked. So I guess I'll try this method from now on. Thank you.
EQ1 = a1 == (ton (B ton^2 + A^2 B ton (toff + ton) +
A (ton^2 + B (toff + ton)^2)))/(toff + ton)^3;
EQ2 = P1 == ton/(ton + toff);
Eliminate[{EQ1, EQ2}, {ton, toff}]
A B P1 + A^2 B P1^2 + (A + B) P1^3 == a1
I should add, if the goal is to make all substitutions that are possible, leaving the rest, I still don't know how to do that. But it appears that if a substitution can completely eliminate a few variables, Eliminate[] works best.
Have you tried this?
K = a*b*t/((t + f) c*d);
Solve[p == t/(t + f), t]
-> {{t -> -((f p)/(-1 + p))}}
Simplify[K /. %[[1]] ]
-> (a b p)/(c d)
EDIT: Oh, and are you aware of Eliminiate?
Eliminate[{eq1, eq2}, {t,f}]
-> a b p == c d K && c != 0 && d != 0
Solve[%, K]
-> {{K -> (a b p)/(c d)}}
EDIT 2: Also, in this simple case, solving for K and t simultaneously seems to do the trick, too:
Solve[{eq1, eq2}, {K, t}]
-> {{K -> (a b p)/(c d), t -> -((f p)/(-1 + p))}}
Something along these lines is discussed in the MathGroup post at
http://forums.wolfram.com/mathgroup/archive/2009/Oct/msg00023.html
(I see it has an apocryphal note that is quite relevant, at least to the author of that post.)
Here is how it might be applied in the example above. For purposes of keeping this self contained I'll repeat the replacement code.
replacementFunction[expr_, rep_, vars_] :=
Module[{num = Numerator[expr], den = Denominator[expr],
hed = Head[expr], base, expon},
If[PolynomialQ[num, vars] &&
PolynomialQ[den, vars] && ! NumberQ[den],
replacementFunction[num, rep, vars]/
replacementFunction[den, rep, vars],
If[hed === Power && Length[expr] == 2,
base = replacementFunction[expr[[1]], rep, vars];
expon = replacementFunction[expr[[2]], rep, vars];
PolynomialReduce[base^expon, rep, vars][[2]],
If[Head[hed] === Symbol &&
MemberQ[Attributes[hed], NumericFunction],
Map[replacementFunction[#, rep, vars] &, expr],
PolynomialReduce[expr, rep, vars][[2]]]]]]
Your example is now as follows. We take the input, and also the replacement. For the latter we make an equivalent polynomial by clearing denominators.
kK = a*b*t/((t + f) c*d);
rep = Numerator[Together[p - t/(t + f)]];
Now we can invoke the replacement. We list the variables we are interested in replacing, treating 'p' as a parameter. This way it will get ordered lower than the others, meaning the replacements will try to remove them in favor of 'p'.
In[127]:= replacementFunction[kK, rep, {t, f}]
Out[127]= (a b p)/(c d)
This approach has a bit of magic in figuring out what should be the listed "variables". Possibly some further tweakage could be done to improve on that. But I believe that, generally, simply not listing the things we want to use as new replacements is the right way to go.
Over the years there have been variants of this idea on MathGroup. It is possible that some others may be better suited to the specific expression(s) you wish to handle.
--- edit ---
The idea behind this is to use PolynomialReduce to do algebraic replacement. That is to say, we do not try for pattern matching but instead use polynomial "canonicalization" a method. But in general we're not working with polynomial inputs. So we apply this idea recursively on PolynomialQ arguments inside NumericQ functions.
Earlier versions of this idea, along with some more explanation, can be found at the note referenced below, as well as in notes it references (how's that for explanatory recursion?).
http://forums.wolfram.com/mathgroup/archive/2006/Aug/msg00283.html
--- end edit ---
--- edit 2 ---
As observed in the wild, this approach is not always a simplifier. It does algebraic replacement, which involves, under the hood, a notion of "term ordering" (roughly, "which things get replaced by which others?") and thus simple variables may expand to longer expressions.
Another form of term rewriting is syntactic replacement via pattern matching, and other responses discuss using that approach. It has a different drawback, insofar as the generality of patterns to consider might become overwhelming. For example, what does one do with k^2/(w + p^4)^3 when the rule is to replace k/(w + p^4) with q? (Specifically, how do we recognize this as being equivalent to (k/(w + p^4))^2*1/(w + p^4)?)
The upshot is one needs to have an idea of what is desired and what methods might be feasible. This of course is generally problem specific.
One thing that occurs is perhaps you want to find and replace all commonly occurring "complicated" expressions with simpler ones. This is referred to as common subexpression elimination (CSE). In Mathematica this can be done using a function called Experimental`OptimizeExpression[]. Here are several links to MathGroup posts that discuss this.
http://forums.wolfram.com/mathgroup/archive/2009/Jul/msg00138.html
http://forums.wolfram.com/mathgroup/archive/2007/Nov/msg00270.html
http://forums.wolfram.com/mathgroup/archive/2006/Sep/msg00300.html
http://forums.wolfram.com/mathgroup/archive/2005/Jan/msg00387.html
http://forums.wolfram.com/mathgroup/archive/2002/Jan/msg00369.html
Here is an example from one of those notes.
InputForm[Experimental`OptimizeExpression[(3 + 3*a^2 + Sqrt[5 + 6*a + 5*a^2] +
a*(4 + Sqrt[5 + 6*a + 5*a^2]))/6]]
Out[206]//InputForm=
Experimental`OptimizedExpression[Block[{Compile`$1, Compile`$3, Compile`$4,
Compile`$5, Compile`$6}, Compile`$1 = a^2; Compile`$3 = 6*a;
Compile`$4 = 5*Compile`$1; Compile`$5 = 5 + Compile`$3 + Compile`$4;
Compile`$6 = Sqrt[Compile`$5]; (3 + 3*Compile`$1 + Compile`$6 +
a*(4 + Compile`$6))/6]]
--- end edit 2 ---
Daniel Lichtblau
K = a*b*t/((t+f)c*d);
FullSimplify[ K,
TransformationFunctions -> {(# /. t/(t + f) -> p &), Automatic}]
(a b p) / (c d)
Corrected update to show another method:
EQ1 = a1 == (ton (B ton^2 + A^2 B ton (toff + ton) +
A (ton^2 + B (toff + ton)^2)))/(toff + ton)^3;
f = # /. ton + toff -> ton/p &;
FullSimplify[f # EQ1]
a1 == p (A B + A^2 B p + (A + B) p^2)
I don't know if this is of any value at this point, but hopefully at least it works.

Weird behaviour with GroebnerBasis in v7

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]]...

How do I force Mathematica to include user defined functions in Simplify and FullSimplify?

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).

Resources