MATLAB Answers

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

2 views (last 30 days)
Victoria Helm
Victoria Helm on 28 Aug 2021
Commented: Walter Roberson on 29 Aug 2021
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!!!

Answers (2)

John D'Errico
John D'Errico on 29 Aug 2021
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 = 
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 on 29 Aug 2021
If you have numerical recordings instead of fumula/expressions for input, the should use numerical diff. "findpeaks" may be also helpful.
Walter Roberson
Walter Roberson on 29 Aug 2021
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!