Recursive symbolic differentiation with anonymous functions

2 ビュー (過去 30 日間)
Leo Simon
Leo Simon 2012 年 8 月 13 日
This is closely related to an earlier post of mine, but i thought I should make it a separate thread, since it's not really the same topic
I'd like the commands
syms x lambda
f = @(x)B(x);
g = @(x)lambda*C(x);
h = @(x)f(x) - g(x);
diff(h,x)
to "go inside of h(x)" and differentiate both f and g, to return
diff(B(x), x) - lambda*diff(C(x), x)
Instead it returns
diff(f(x), x) - diff(g(x), x)
I get the answer I want if I use
syms x
f = sym('B(x)')
g = sym('lambda*C(x)')
h = f - g; diff(h,x)
but this coding is less flexible than the former, since the argument 'x' is "hard-coded" into my functions, so I would have to use subs everytime I want to give my functions different arguments.
So in short, what I'm hoping for is the best of both worlds, the flexibility provided by anonymous functions plus the recursive (and other) properties provided when I use "sym"
  1 件のコメント
Oleg Komarov
Oleg Komarov 2012 年 8 月 13 日
編集済み: Oleg Komarov 2012 年 8 月 13 日
I don't see the problem, the subs() in a symbolic context it's just a line of code.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 8 月 13 日
diff() cannot be applied to function handles.

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by