# Implicitly differentiate an equation, then solve the resulting equation

Suppose I have an extremely tedious equation to differentiate, and I want Mathematica to help do the differentiation and solve.

Consider a less tedious equation:

$$y (x,z) = \sin \left(\frac{1}{x} \right) - \frac{1}{x} - z$$

Can Mathematica find $\frac{\partial y}{\partial x}$ at $x=0.3$ by performing implicit differentiation on both sides and solving for $\frac{\partial y}{\partial x}$?

Now suppose we try implicit differentiation:

$$\sin (y) + y = \sin \left(\frac{1}{x} \right) + xz$$

I want to find $\frac{\partial y}{\partial x}$ at $x=0.3$, $y=0.4$ and $z=0.5$. It should give a value of $5.94$.

Tried this, but didn't work:

sin [y] + y == sin [1/x] + x z
D[y[x_, y_, z_], x] /. {x -> 0.3,  y -> 0.2, z -> 0.5}

• what is z? constant? function of x? – Nasser Jul 8 '14 at 12:28
• z is just another variable, oops it should be y(x,z) – user44840 Jul 8 '14 at 12:30
• So you'd like mma to differentiate, obtaining $\partial_x y=f(x,z)$, then set $x=3$ and obtain some function of $z$ for the rhs and then what? What's the next step? – acl Jul 8 '14 at 12:40
• Note: In Mathematica, Sin[y], not sin[y] and so forth. – Michael E2 Jul 8 '14 at 14:49
• You could use the formula $\partial y/\partial x = - (\partial f/\partial x) / (\partial f/\partial y)$, if the equation is put in the form $f(x,y,z)=0$. – Michael E2 Jul 8 '14 at 15:07

I often do this sort of thing with Dt. It works on equations, too. It basically gives you the multivariate differential $${\partial f \over \partial x} \;dx + {\partial f \over \partial y} \;dy + \cdots = 0$$ To get $dy/dx$, set $dx = 1$ and any other differential other than $dy$ equal to zero. Then solve for $dy$ to get $dy = - ({\partial f /\partial x})/({\partial f / \partial y})$, which equals $dy/dx$, since we set $dx = 1$.

In Mathematica on the OP's example we get first:

Dt[Sin[y] + y == Sin[1/x] + x z]
(* Dt[y] + Cos[y] Dt[y] == z Dt[x] - (Cos[1/x] Dt[x])/x^2 + x Dt[z] *)


Now set the differentials to their appropriate values:

Dt[Sin[y] + y == Sin[1/x] + x z] /. {Dt[x] -> 1, Dt[z] -> 0}
(* Dt[y] + Cos[y] Dt[y] == z - Cos[1/x]/x^2 *)


Then solve for Dt[y]:

dy = Dt[y] /. First@Solve[
Dt[Sin[y] + y == Sin[1/x] + x z] /. {Dt[x] -> 1, Dt[z] -> 0},
Dt[y]
]
(* (x^3 + x^2 z - Cos[1/x])/(x^2 (1 + Cos[y])) *)


To evaluate numerically, replace the variables with their values:

dy /. {x -> 0.3, y -> 0.2, z -> 0.5}
(* 5.91267 *)


Here's a function to carry out the procedure above for the derivative of an arbitrary variable y with respect to a variable x from an implicit function defined by an equation eqn:

d[eqn_Equal, y_Symbol, x_Symbol] := Dt[y] /. First@Solve[
Dt[eqn] /. {Dt[x] -> 1, HoldPattern@Dt[Except[y]] -> 0},
Dt[y]
]


Examples:

d[Sin[y] + y == Sin[1/x] + x z, y, x]
(* (x^2 z - Cos[1/x])/(x^2 (1 + Cos[y])) *)

d[Sin[y] + y == Sin[1/x] + x z, y, x] /. {x -> 0.3, y -> 0.2, z -> 0.5}
(* 5.76116 *)

d[2 x + 3 y + 5 z + 7 w == 0, x, w]
(* -(7/2) *)

• The answer should be 5.76.The first part is fine. But When I'm using d[Sin[y] + y == Sin[1/x] + x z, y, x] /. {x -> 0.3, y -> 0.2, z -> 0.5} there is an error of "d[False, 0.2, 0.3] – user44840 Jul 9 '14 at 8:29
• The False indicates that the variables in Sin[y] + y == Sin[1/x] + x z had values and the equation evaluated to False; in fact, the 0.2, 0.3 indicate what they are for y and x. You could try executing Clear[x, y, z] first. Alternatively one could write a fancier function d that was HoldAll and Block-ed the variables. (That seemed like overkill to me at the time.) – Michael E2 Jul 9 '14 at 13:03
• @user44840 Or perhaps you didn't execute the definition of d. – Michael E2 Jul 9 '14 at 16:00
• Perhaps slightly cleaner is d[eqn_Equal, y_Symbol, x_Symbol] := Dt[y] /. First @ Solve[Dt[eqn], Dt[y]] /. {Dt[x]->1, _Dt->0} – Carl Woll Nov 4 '17 at 2:52
y[x_, z_] := Sin[1/x] - 1/x - z

D[y[x, z], x] /. x -> 0.3


Not exactly sure what you wanted to do after that.

Update 1

y[x_, z_] := Sin[1/x] - 1/x - x z

D[y[x, z], x] /. {x -> 0.3, z -> 1}


Update 2

eqn = Sin[y[x, z]] + y[x, z] == Sin[1/x] + x z

Solve[
D[eqn, x] /. {x -> 0.3, z -> 0.5, y[x, z] -> 0.4},
D[y[x, z], x] /. {x -> 0.3, z -> 0.5}
]

• it doesn't work – user44840 Jul 8 '14 at 12:32
• What do you mean? The second line calculates $\frac{\partial y}{\partial x}$ at $x = 0.3$? – user29165 Jul 8 '14 at 12:33
• I ran the code in mathematica, doesn't work.. – user44840 Jul 8 '14 at 12:38
• @user44840 it's probably useful to explain what "doesn't work" means. – acl Jul 8 '14 at 12:39
• It sounds like you set the variables with some values already. Try and run the following command before the 2 lines above. Clear["Global*"] – user29165 Jul 8 '14 at 12:45

It maybe done by finding first the total derivative of a function f(x,y,z)

f = Sin[y] + y - Sin[1/x] - x z;
Dt[f]

(*-z Dt[x] + (Cos[1/x] Dt[x])/x^2 + Dt[y] + Cos[y] Dt[y] - x Dt[z]*)


then solve fro both Dt[y] and Dt[x]

dy = Dt[y] /. Solve[Dt[f] == 0, Dt[y]][[1]];
dx = Dt[x] /. Solve[Dt[f] == 0, Dt[x]][[1]];

f2=dy/dx/. {x -> 3/10, y -> 4/10};
f2/.z->0.5

(*-5.93812*)

• Funny, this gives Indeterminate for f = x + y and 1 for f = x + y + z`, both of which are incorrect. – user484 Jul 8 '14 at 23:09