Partial derivatives of the inline function
2 次查看(过去 30 天)
显示 更早的评论
I have defined an inline function in a script
function a = Test(A,B,C)
I want to symbolically define partial derivatives of this Test function with respect to A, B, C. Please advise.
0 个评论
回答(2 个)
Walter Roberson
2018-5-12
syms A B C
fun = Test(A,B,C);
Now fun will be a symbolic expression involving A, B, C, that you can calculate gradient of, or can directly calculate
diff(fun, A)
for example.
Note that this will not work if Test uses "if" statements testing the values of the inputs, or does logical indexing based upon the values, or if it initializes vectors or arrays to zeros() and tries to assign values calculated from A, B, C into them. Sometimes you need to change a function a bit to make it usable with symbolic inputs. Sometimes you need to resort to tests such as
if issym(A) || issym(B) || issym(C)
y = piecewise(....);
else
if A < pi || B > sqrt(2)
y = 11;
else
y = 9;
end
end
-- that is, sometimes you need to test if you are doing symbolic work and create a piecewise() expression because you cannot test unresolved symbols against specific numbers.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!