Derivate using chain rule doesn't work in MATLAB
4 views (last 30 days)
Show older comments
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;
0 Comments
Answers (1)
Star Strider
on 8 Nov 2020
This works for me:
syms f(x) r(x) x
dfr = diff(f(x)*r(x))
producing:
dfr =
f(x)*diff(r(x), x) + r(x)*diff(f(x), x)
that to me appears to bear a strong resemblance to the chain rule for the product of two functions.
I have no idea what you are doing in the last part of your Question.
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!