How can I check if my derivative for an implicit function is correct?

$\begingroup$

For explicit functions I can calculate the derivative at a certian point using the original function: $$\frac{f(1+0.1) - f(1)}{0.1}$$

And then use $\frac{d}{dx}f(1)$ to check if the function is correct. But what can I do for implicit funcions? how can I calculate the change then compare it with my derivative function

Edit: for example if I was asked to differentiate $f(x)=x^2+\tanh(x)$ and if I am unsure about my answer I could type in on the calculator: $$\frac{f(5+0.000001) - f(5)}{0.000001}$$ and then check my $\frac{d}{dx}f(5)$ they should be approximately equal. My question is if I have an implicit function like $$xy^3=\tan(x+2y)-(x^2-1)$$ and after I differentiate it how can I check if it is correct

$\endgroup$ 7

3 Answers

$\begingroup$

You can first calculate the real derivative for a pair of $(x,y)$ that satisfies the equation, then choose a $Δx$, then test if $\left(x+\Delta x,y+\Delta x\frac{dy}{dx}\right)$ approximately satisfies the equation.

For example, looks like $\left(0, -\pi/8\right)$ is a root of your equation. So you can choose $x' = 0.000001$ and find the corresponding $y'$ using the derivative:

$$\begin{align*} 3xy^2\frac{dy}{dx} + y^3 &= \sec^2(x+2y)\left(1+2\frac{dy}{dx}\right) -2x\\ 3xy^2\frac{dy}{dx} -2\sec^2(x+2y)\frac{dy}{dx} &= \sec^2(x+2y) -2x - y^3\\ \frac{dy}{dx} &= \frac{\sec^2(x+2y) -2x - y^3}{3xy^2 -2\sec^2(x+2y)}\\ \left.\frac{dy}{dx}\right|_{\left(0,-\frac\pi8\right)} &= \frac{2+\frac{\pi^3}{8^3}}{-4} \end{align*}$$

So let $$y' = -\frac\pi8 +\frac{2+\frac{\pi^3}{8^3}}{-4}\cdot0.000001 \approx -0.392699596839\ldots$$

$$LHS = x'\left(y'\right)^3 = -6.0559372465\ldots×10^{-8}$$ $$RHS = \tan\left(x'+2y'\right)-\left(x'\right)^2 + 1 = -6.056\ldots×10^{-8}$$

And hope it is close enough...

$\endgroup$ 2 $\begingroup$

Great question - this sort of things shows up all the time when writing code to solve constrained optimization problems.

The standard strategy is as follows. First, you need a mechanism to solve for $y$ given $x$. For example, you could use Newton's method, or bisection search, or whatever. I recommend Newton's method with backtracking linesearch. You can check the correctness of your solver code by plugging $x,y$ into the original equation and verifying that the residual is zero.

Then that complicated solver code is your function $y=f(x)$, and you can check your derivative with finite differences $(f(x+h)-f(x))/h$ with a random direction $h$, like you mention in the original post.

To make sure that your code doesn't converge to a different root when you compute $y_2 = f(x+h)$, use your original $y_1 = f(x)$ as the initial guess in your solver.

Another tip is to choose several different finite difference stepsizes, say $10^{-1}, 10^{-2}, 10^{-3} \dots, 10^{-7}$, and plot the error in the derivative on a log-log plot. The error should linearly decrease until around the square root of machine epsilon - for doubles machine epsilon is around $10^{-15}$, so usually you will see a straight line going down until the step size is around $10^{-7}$, after which point numerical rounding takes over. If your code is working, you should get something that looks like the following plot.finite difference check log-log plot

$\endgroup$ 1 $\begingroup$

For an implicit function F(x,y) the formula is:

$$ \dfrac{dy}{dx} = - \dfrac{F_x}{F_y} ...(1*) $$

$$ F = x\, y^3 + (x^2-1) - \tan ( x+ 2 y) $$

$$ F_x = y^3 + 2 x - \sec^2 ( x+ 2 y) ; F_y = 3 x y^2 -2 \sec^2 ( x+ 2 y) ; $$

and plug them in, to evaluate $\frac {dy}{dx}$ at any (x,y).

How does one get this result?

Differentiate F(x,y)= 0 ; Total differential

$$ F_x dx + F_y dy = 0.$$

To check by calculation at any point, hold x constant and compute $F_y $ differential the way you stated, then hold y constant and compute $ F_x $ same way, use (1*).

$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like