I'd like a simple interface where the user can input (by text) a function $f(x)$, and then the function, its even part, and its odd part are all plotted on the same axes. I'd also like PlotLegend to show the mathematical form of $f(x)$, as well as the even and the odd functions.
Recall that $f_e = \frac{f(x) + f(-x)}{2}$ and $f_o = \frac{f(x) - f(-x)}{2}$. So for instance if the user puts in Sin[x] + 3 Cos[x] + x^2
, I'd like the following three graphs plotted:
- Sin[x] + 3 Cos[x] + x^2
- 3 Cos[x] + x^2
- Sin[x]
I'd also like the PlotLegends to be
- $f(x) = \sin x + 3 \cos x + x^2$
- $f_e (x) = 3 \cos x + x^2$
- $f_o (x) = \sin x$
Here is a weak start:
Panel[DynamicModule[{f = Sin[x] + 2 Cos[x] + x^2},
Column[{InputField[Dynamic[f]],
Dynamic[Plot[{f, f/2, f/3}, {x, -5, 5},
ImageSize -> 300,
PlotLegends -> {"f = " f,
"\!\(\*SubscriptBox[\(f\), \(e\)]\) = " f/2,
"\!\(\*SubscriptBox[\(f\), \(o\)]\) = " f/4}]]}]]]
Note that this does not compute the even and the odd parts.
I cannot get the plot legends to be properly typeset either. For instance there are spurious and unnecessary parentheses around entire expression; the equations are sometimes mangled by being "factored," and so on.
Finally, I want each time the user types a new equation into the InputField, the previous graph disappears. That is, I don't want the user to use this code 10 times with different equations and get a set of 10 graphs remaining; only the most recent graph should remain.