Manipulate[g[x_] = f;
Plot[{g[x], g[2]}, {x, -9, 9}],
{{f, "Enter a function...", "f(x) ="}}]
Hi, I'm relatively new to Mathematica and this is my first post on stackexchange, so forgive me for any formatting issues in my post.
I'm trying to create a program that takes a function from the user through Manipulate and plots the function. So far I've been able to do this, though my issue is that I also want to plot the horizontal line given by the function evaluated at some number (2 in this case), but I am simply getting the plot g[x]=x twice.
If I type
f = x;
g[x_] = f;
then g[x] returns x, and g[2] returns 2, which is what I am looking for. Though when I put everything in Manipulate, g[x] and g[2] both return x. I think I understand why, because g[2] calls on f, which is x, so the f is not being replaced by x prior to the substitution of 2. I just don't understand how to fix this problem.
Additionally, the Manipulate part is necessary because I want to use the same concept here in a separate project that I'm working on. In the other project I will need to take the arbitrary input function g[x_] = f and use it for other calculations within the same Manipulate block, i.e. plotting a point such as (2,g[2]+g[5]).
Thanks! :)
Manipulate[g[x_] = f;Information["g"], {{f, "Enter a function...", "f(x) ="}}]
. What you'll see is that the formal argument of g isn't actuallyx
. That explains why g[x] and g[2] evaluate to the same thing. I'll have to fiddle with it a bit to come up with something that works and isn't ugly--unless someone else beats me to it. $\endgroup$Manipulate[g[x_] := f; Plot[Evaluate@{g[x], g[x] /. x -> 2}, {x, -9, 9}], {{f, "Enter a function...", "f(x) ="}}]
$\endgroup$