Search code examples
matlaboptimizationsymbolic-mathgradient-descent

Derivate using chain rule doesn't work in MATLAB


I am trying to derive the gradient and hessian for a given function. When i directly do the gradient it works well but when I apply chain rule it doesn't works and throws me an error as below

Error using sym/diff (line 70)
Second argument must be a variable or a nonnegative integer specifying the number of differentiations.

Error in EO_a1 (line 12)
dfr = diff(f(x),r(x));

My MATLAB code

syms x a b const r(x)

const = (a*x);
r(x) = (const - b);
f(x) = (1/2)*(r(x)^2);

gradient = diff(f(x));
gradient;
hessian = diff(gradient);
hessian;

%gradient applying the chain rule
dfr = diff(f(x),r(x));
dfr;
drx = diff(dfr,x);
drx;

Solution

  • Probably not be ideal but you can take the derivative with respect to the function r(x) by using the function functionalDerivative(). Note that r(x) will have to be declared after evaluating the first part of the chain rule. After that the second part of the chain rule r'(x) can be evaluated after substituting the symbolic representation/equation for r(x) using the subs(). Following this the two parts of the chain rule can be multiplied. After this process the result matches the step solution in your first solution by taking the diff(f(x)) which differentiates with respect to x which can also be done by diff(f(x),x) for brevity.

    %METHOD 2: CHAIN RULE%
    clear;
    syms x a b const r(x)
    const = (a*x);
    
    f(x) = (1/2)*(r(x)^2);
    
    %First part of chain rule%
    dfr = functionalDerivative(f,r(x));
    dfr = subs(dfr,r(x),(const - b));
    
    %Second part of chain rule%
    drx = diff(dfr,x);
    
    %Product of parts of the chain rule%
    Chain_Rule_Result = dfr*drx;
    Chain_Rule_Result
    

    Ran using MATLAB R2019b