What is a symbolic expression (syms) and how do I use this when doing differentiation in matlab?

조회 수: 10 (최근 30일)
Hi I have some data with voltage spike recordings that I need to count. I want to use the diff() function to count the number of spikes (i.e. the number of rises and drops) that occur over a period of time. However, I am seeing that I need to use a symbolic expression for the thing that I am differentiating with respect to. Can someone explain to me what this means? Can I make an array of symbolic expressions? (For example, an array times that are of type syms)?
Thank you!!!

답변 (2개)

John D'Errico
John D'Errico 2021년 8월 29일
No. You misunderstand what diff does, and why diff is used there.
diff, when applied to a numeric vector, forms the difference between successive elements. This is NOT a derivative.
For example,
X = primes(20)
X = 1×8
2 3 5 7 11 13 17 19
dx = diff(X)
dx = 1×7
1 2 2 4 2 4 2
All it did was compute the difference between pairs of successive elements. We can get the same result by the operation:
dx_2 = X(2:end) - X(1:end-1)
dx_2 = 1×7
1 2 2 4 2 4 2
Again, this is NOT a derivative. Your confusion stems from the fact that diff can also be used to perform differentiation, when applied to a symbolic expression. And that is where syms comes in. For example:
syms x
whos x
Name Size Bytes Class Attributes x 1x1 8 sym
So x is a symbolic variable. It contains no numerical value here.
y = x^3 + 3*x^2 - 2
y = 
diff(y,x)
ans = 
As you can see, differentiation of an expression was performed. In both cases, a function named diff was used, but they do entirely different things, depending upon the input arguments.

Chunru
Chunru 2021년 8월 29일
If you have numerical recordings instead of fumula/expressions for input, the should use numerical diff. "findpeaks" may be also helpful.
  댓글 수: 3
Chunru
Chunru 2021년 8월 29일
The function name is same for both symbolic and numerical:
a = rand(8, 1 )
a = 8×1
0.9286 0.9374 0.5973 0.5671 0.1409 0.2975 0.7340 0.3726
b = diff(a)
b = 7×1
0.0088 -0.3401 -0.0302 -0.4262 0.1566 0.4365 -0.3614
Walter Roberson
Walter Roberson 2021년 8월 29일
For numeric derivative estimation, typically using gradient() is a better choice.
Numeric differences can be estimated in several different ways. Forward differences; Backwards Differences; Central Differences; and others.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by