matlab 出现Function 'diff' is not supported for class 'function_handle'

2024-05-18 12:52

1. matlab 出现Function 'diff' is not supported for class 'function_handle'

diff有几种重载形式,可分为两种功能:
(1)求差分:是built-in函数,对于向量X,得到的结果是[X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)];
(2)求导数:输入参数有两种形式,一种是sym表达式,另一种是字符串。字符串会被自动转换为sym对象再调用sym/diff这种重载形式。
 
而你遇到的错误是,输入为函数句柄类型(function_handle),diff并不支持这样的用法。
 
例子:
>> x = [1 2 3 4 5];>> y = diff(x)y =     1     1     1     1>> syms x>> diff(x^2+3*x) ans = 2*x+3 >> diff('x^2+3*x') ans = 2*x+3

matlab 出现Function 'diff' is not supported for class 'function_handle'