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!