Partial derivatives of the inline function
Ältere Kommentare anzeigen
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.
Antworten (2)
Star Strider
am 12 Mai 2018
0 Stimmen
Walter Roberson
am 12 Mai 2018
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.
Kategorien
Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!