Derivative in function handle

77 ビュー (過去 30 日間)
vincenzo
vincenzo 2017 年 9 月 11 日
コメント済み: James Tursa 2017 年 9 月 12 日
f=@(x) x + log(x);
f1=diff(f)
f2=diff(f1)
I want to assign first derivative of 'f' to 'f1', and second derivative for 'f1' to 'f2' But i have this error "Undefined function 'diff' for input arguments of type 'function_handle'". How to fix? Thanks

回答 (2 件)

José-Luis
José-Luis 2017 年 9 月 11 日
編集済み: José-Luis 2017 年 9 月 11 日
If you're gonna do this numerically, you need to specify an interval in which to evaluate. Note that diff doesn't really give the derivative, but I'll stick to your nomenclature.
limits = [1,10];
f = @(interval) (interval(1):interval(2)) + log(interval(1):interval(2));
f1 = diff(f(limits));
f2 = diff(f1);
You could also do it symbolically but I can't help you there because I don't have the symbolic math toolbox.

James Tursa
James Tursa 2017 年 9 月 11 日
編集済み: James Tursa 2017 年 9 月 11 日
E.g., if you want function handles you could get at them with the symbolic toolbox
>> syms x
>> f = @(x) x + log(x)
f =
@(x)x+log(x)
>> f1 = eval(['@(x)' char(diff(f(x)))])
f1 =
@(x)1/x+1
>> f2 = eval(['@(x)' char(diff(f1(x)))])
f2 =
@(x)-1/x^2
If you plan on feeding vectors or matrices etc to these function handles, then you could wrap the expressions appropriately with the vectorize( ) function. E.g.,
>> f1 = eval(['@(x)' vectorize(char(diff(f(x))))])
f1 =
@(x)1./x+1
>> f2 = eval(['@(x)' vectorize(char(diff(f1(x))))])
f2 =
@(x)-1./x.^2
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 9 月 11 日
No need for the eval()
syms x
f = @(x) x + log(x)
f1 = matlabFunction( diff(f(x)) );
f2 = matlabFunction( diff(f1(x)) );
James Tursa
James Tursa 2017 年 9 月 12 日
@Walter: +1

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

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by