Derivate using chain rule doesn't work in MATLAB
13 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Star Strider
2020-11-8
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!