# How do I conditionally (or temporarily) assign a numeric value to a variable [closed]

I've used TI's CAS system quite a bit, and I have a nice pattern for evaluating expressions without setting and then later clearing a variable.

For example, on my TI-89, I can do the following:

$-\frac{1}{3}x^3 + 3x^2 + 7x$ when x = 3

This would output a numeric (or exact) answer, evaluating the expression as if x had been assigned 3, but still leaving x as unassigned.

Can I do the same thing or something similar in Mathematica?

Generally, I use something like -1/3x^3+3x^2+7x /. x->3. For most simple equations, I find it most convenient to use /. (aka ReplaceAll) to do this sort of thing. One reason I like to use /. is because it lets me take a few steps to interactively build out a formula first.

For instance, here I'm trying to evaluate $\frac{d}{dx} (-\frac{1}{12}x^4+x^3+\frac{7}{2}x^2)$ at $x=3$:

In:= -x^4/12+x^3+7/2x^2

2         4
7 x     3   x
Out= ---- + x  - --
2          12

In:= D[%,x]

3
2   x
Out= 7 x + 3 x  - --
3

In:= % /. x->3

Out= 39


Depending on what you're doing, it may be easiest to create a function instead. For instance, if you define f[x_] = -1/3x^3+3x^2+7x, you can evaluate f etc. as needed.

You also can simply define y = -1/3x^3+3x^2+7, and then evaluate y /. x->3.