4

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}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user44840
  • 421
  • 3
  • 13

3 Answers3

7

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) *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 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 09 '14 at 08: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 09 '14 at 13:03
  • @user44840 Or perhaps you didn't execute the definition of d. – Michael E2 Jul 09 '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 04 '17 at 02:52
2
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}
]
user29165
  • 565
  • 3
  • 10
2

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*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
  • Funny, this gives Indeterminate for f = x + y and 1 for f = x + y + z, both of which are incorrect. –  Jul 08 '14 at 23:09