Search code examples
matlabsymbolic-mathderivativedifferentiation

Implicit differentiation - Second derivative using Matlab


The equation is 4*x^2-2*y^2==9. Using implicit differentiation, I can find that the second derivative of y with respect to x is -9/y^3, which requires a substitution in the final step.

I am trying to duplicate this answer using Matlab's symbolic toolbox. I did find some support for the first derivative here, and was successful finding the first derivative.

clear all
syms x y f
f=4*x^2-2*y^2-9
sol1=-diff(f,x)/diff(f,y)

But I am unable to continue onward to find the second derivative with the final simplification (replacing 4*x^2-2*y^2 with 9).

Can someone show me how to do this in Matlab?


Solution

  • To my knowledge there is no direct way to obtain an implicit second derivative in Matlab. And working with implicit functions in Matlab can be fairly tricky. As a start, your variable y is implicitly a function of x s you should define it as such:

    clear all
    syms y(x) % defines both x and y
    f = 4*x^2-2*y^2-9
    

    Here y(x) is now what is called an arbitrary or abstract symbolic function, i.e., one with no explicit formula. Then take the derivative of f with respect to x:

    s1 = diff(f,x)
    

    This returns a function in terms of the implicit derivative of y(x) with respect to x, diff(y(x), x) (in this case diff(y) is shorthand). You can solve this function for diff(y) algebraically with subs and solve:

    syms dydx % arbitrary variable
    s2 = subs(s1,diff(y),dydx)
    s3 = solve(s2,dydx)
    

    This yields the first implicit derivative. You can then take another derivative of this expression to obtain the second implicit derivative as a function of the first:

    s4 = diff(s3,x)
    

    Finally, substitute the expression for the first implicit derivative into this and simplify to obtain the final form:

    s5 = simplify(subs(s4,diff(y),s3))
    

    This yields (2*(y(x)^2 - 2*x^2))/y(x)^3. And then you can eliminate x using the original expression for f with further substitution and solving:

    syms x2
    f2 = subs(f,x^2,x2)
    x2 = solve(f2,x2)
    s6 = subs(s5,x^2,x2)
    

    Finally, you can turn this back into an explicit algebraic expression with a final substitution, if desired:

    s7 = subs(s6,y,'y')
    

    This yields your solution of -9/y^3.

    This whole process can be written more concisely (but very unclearly) as:

    clear all
    syms y(x) dydx x2
    f = 4*x^2-2*y^2-9;
    s1 = solve(subs(diff(f,x),diff(y),dydx),dydx)
    s2 = simplify(subs(subs(subs(diff(s1,x),diff(y),s1),x^2,solve(subs(f,x^2,x2),x2)),y,'y'))
    

    There are many other ways to achieve the same result. See also these two tutorials: [1], [2].