4
$\begingroup$

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}
$\endgroup$
5
  • $\begingroup$ what is z? constant? function of x? $\endgroup$
    – Nasser
    Commented Jul 8, 2014 at 12:28
  • $\begingroup$ z is just another variable, oops it should be y(x,z) $\endgroup$
    – user44840
    Commented Jul 8, 2014 at 12:30
  • $\begingroup$ 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? $\endgroup$
    – acl
    Commented Jul 8, 2014 at 12:40
  • 1
    $\begingroup$ Note: In Mathematica, Sin[y], not sin[y] and so forth. $\endgroup$
    – Michael E2
    Commented Jul 8, 2014 at 14:49
  • 1
    $\begingroup$ 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$. $\endgroup$
    – Michael E2
    Commented Jul 8, 2014 at 15:07

3 Answers 3

7
$\begingroup$

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) *)
$\endgroup$
4
  • $\begingroup$ 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] $\endgroup$
    – user44840
    Commented Jul 9, 2014 at 8:29
  • $\begingroup$ 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.) $\endgroup$
    – Michael E2
    Commented Jul 9, 2014 at 13:03
  • $\begingroup$ @user44840 Or perhaps you didn't execute the definition of d. $\endgroup$
    – Michael E2
    Commented Jul 9, 2014 at 16:00
  • $\begingroup$ Perhaps slightly cleaner is d[eqn_Equal, y_Symbol, x_Symbol] := Dt[y] /. First @ Solve[Dt[eqn], Dt[y]] /. {Dt[x]->1, _Dt->0} $\endgroup$
    – Carl Woll
    Commented Nov 4, 2017 at 2:52
2
$\begingroup$
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}
]
$\endgroup$
8
  • $\begingroup$ it doesn't work $\endgroup$
    – user44840
    Commented Jul 8, 2014 at 12:32
  • $\begingroup$ What do you mean? The second line calculates $\frac{\partial y}{\partial x}$ at $x = 0.3$? $\endgroup$
    – user29165
    Commented Jul 8, 2014 at 12:33
  • $\begingroup$ I ran the code in mathematica, doesn't work.. $\endgroup$
    – user44840
    Commented Jul 8, 2014 at 12:38
  • 1
    $\begingroup$ @user44840 it's probably useful to explain what "doesn't work" means. $\endgroup$
    – acl
    Commented Jul 8, 2014 at 12:39
  • 1
    $\begingroup$ It sounds like you set the variables with some values already. Try and run the following command before the 2 lines above. Clear["Global`*"] $\endgroup$
    – user29165
    Commented Jul 8, 2014 at 12:45
2
$\begingroup$

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*)
$\endgroup$
1
  • $\begingroup$ Funny, this gives Indeterminate for f = x + y and 1 for f = x + y + z, both of which are incorrect. $\endgroup$
    – user484
    Commented Jul 8, 2014 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.