Little truble with 'diff'

5 ビュー (過去 30 日間)
Luis Miguel López Santamaría
Luis Miguel López Santamaría 2017 年 5 月 1 日
コメント済み: Star Strider 2017 年 5 月 1 日
Hi everybody,
I'm trying to write the code for the function sing as you know there's an alternatively form:
Well here is the code that but gives me a little of trouble because for dimension
% sng(t) = (d/dt)*|t|
t = -10:10;
y = diff(abs(t));
figure(1);
plot(t,y); xlabel('t'); ylabel('sgn(t)');
%title('signum');
axis ([-10 10 -1.1 1.1])
grid on;
The problem is that when you use 'diff' with a 0 gives you this diff(0) = [], is an empty vector, any advice how can I fix or a way that 'diff' don't give me a empty space.
Thank you.

採用された回答

Star Strider
Star Strider 2017 年 5 月 1 日
The MATLAB numeric diff function produces the difference of matrix or vector elements: [x(2)-x(1),x(3)-x(2),...] so if there is only one element in the argument you give it, it will return the empty array. It also returns a vector one element shorter than the argument vector. (The Symbolic Math Toolbox diff function calculates the symbolic derivative of its argument.)
If you want to take the numeric derivative, use the gradient function.
The derivative of 0 or any other constant will of course be 0.
  2 件のコメント
Luis Miguel López Santamaría
Luis Miguel López Santamaría 2017 年 5 月 1 日
Thank you very much :)
Star Strider
Star Strider 2017 年 5 月 1 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by