matlab中的求导函数diff

2018-01-19

今天发现我的标题写错了,diff不知怎么写成了fill,醉醉醉

---------------------------------------------------------------------------------------------------------------

matlab中用函数diff做求导

函数描述:DIFF Difference and approximate derivative.

语法格式:

Y = diff(X)
Y = diff(X,n)
Y = diff(X,n,dim)

1.  Y = diff(X)

  (1).  DIFF(X), for a vector X, is [X(2)-X(1)  X(3)-X(2) ... X(n)-X(n-1)].

X是长度为n容器,也就是长度为n的行向量,返回n-1长度的行向量,后一个数减去前一个数

>> x = [1 2 3 4 5];
>> diff(x)

ans =

     1     1     1     1
>>
>> x=1:10;
>> y=2*x;
>> diff(y)

ans =

     2     2     2     2     2     2     2     2     2

>> 
 
原来长度为5,结果长度为4.


   (2).  DIFF(X), for a matrix X, is the matrix of row differences,
       [X(2:n,:) - X(1:n-1,:)].

 X是个n行的矩阵,返回结果是n-1行的矩阵,后一行数减去前一行数

>> x = [ 1 1 1;2 2 2;4 4 4];
>> diff(x)

ans =

     1     1     1
     2     2     2

>> 

   (3). DIFF(X), for an N-D array X, is the difference along the firstnon-singleton dimension of X.

这个不大会,以后回了在更新吧,不过意思跟前面应该差不多。

  (4).还有一种是单个点的导数值

>> syms x;
>> y=power(x,2);
>> y1=diff(y);
>> subs(y1,x,1)

ans =

     2

>> y,y1
 
y =
 
x^2
 
 
y1 =
 
2*x
 
 
>> 

2.  Y = diff(X,n)  

      diff(X,N) is the N-th order difference along the first non-singleton
       dimension (denote it by DIM). If N >= size(X,DIM), diff takes
       successive differences along the next non-singleton dimension.

n是求导阶数

>> x=[1 1 1;2 2 2 ;4 4 4;7 7 7];
>> diff(x)

ans =

     1     1     1
     2     2     2
     3     3     3

>> diff(x,2)

ans =

     1     1     1
     1     1     1

>> 
>> syms x;
>> y=power(x,3);
>> y1=diff(y,2);
>> subs(y1,x,2)

ans =

    12

>> y1
 
y1 =
 
6*x
 
>> 


3.  diff(X,N,DIM)

diff(X,N,DIM)is the Nth difference function along dimension DIM.
       If N >= size(X,DIM), diff returns an empty array.

求导阶数不能大于DIM

>> syms x;
>> y=power(x,3);
>> y1=diff(y,2);
>> y2=diff(y,3,2)
 
y2 =
 
6*x
 
 
>> y2=diff(y,3,3)
 
y2 =
 
6
 
 
>>


  • 15
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值