Reputation: 1544
I am trying to evaluate the derivative of a function at a point (3,5,1) in Mathematica. So, thats my input:
In[120]:= D[Sqrt[(z + x)/(y - 1)] - z^2, x]
Out[121]= 1/(2 (-1 + y) Sqrt[(x + z)/(-1 + y)])
In[122]:= f[x_, y_, z_] := %
In[123]:= x = 3
y = 5
z = 1
f[x, y, z]
Out[124]= (1/8)[3, 5, 1]
As you can see I am getting some weird output. Any hints on evaluating that derivative at (3,5,1) please?
Upvotes: 1
Views: 1414
Reputation: 10695
The result you get for Out[124]
leads me to believe that f
was not cleared of a previous definition. In particular, it appears to have what is known as an OwnValue
which is set by an expression of the form
f = 1/8
(Note the lack of a colon.) You can verify this by executing
g = 5;
OwnValues[g]
which returns
{HoldPattern[g] :> 5}
Unfortunately, OwnValues
supersede any other definition, like a function definition (known as a DownValue
or, its variant, an UpValue
). So, defining
g[x_] := x^2
would cause g[5]
to evaluate to 5[5]
; clearly not what you want. So, Clear
any symbols you intend to use as functions prior to their definition. That said, your definition of f
will still run into problems.
At issue, is your use of SetDelayed
(:=
) when defining f
. This prevents the right hand side of the assignment from taking on a value until f
is executed later. For example,
D[x^2 + x y, x]
f[x_, y_] := %
x = 5
y = 6
f[x, y]
returns 6
, instead. This occurs because 6
was last result generated, and f
is effectively a synonym of %
. There are two ways around this, either use Set
(=
)
Clear[f, x, y]
D[x^2 + x y, x];
f[x_, y_] = %
f[5, 6]
which returns 16
, as expected, or ensure that %
is replaced by its value before SetDelayed
gets its hands on it,
Clear[f, x, y]
D[x^2 + x y, x];
f[x_, y_] := Evaluate[%]
Upvotes: 6