I am trying to plot the function that uses kroneckerDelta
resulting from the following code. I tried many ways to plot the deltas, but nothing is working. I tried fplot
and converting the S
variable to a double before plotting. But nothing actually worked.
close all;clear all;clc; %#ok<*CLSCR>
syms y(n) x(n) H(Z) z
H(Z) = 5*x + 2*x*z^-2 + 3*x*z^-1;
h(n) = iztrans(H(Z), z, n)
S = subs(h(n), {x}, {1})
The output is:
h(n) = 5*x(n)*kroneckerDelta(n, 0) + 3*x(n)*kroneckerDelta(n - 1, 0) + 2*x(n)*kroneckerDelta(n - 2, 0)
S = 3*kroneckerDelta(n - 1, 0) + 2*kroneckerDelta(n - 2, 0) + 5*kroneckerDelta(n, 0)
You don't show how you're trying to plot so it's impossible to say what you're doing wrong. However, due to the discrete nature of the Kronecker delta, you should probably use the stem
function for your plots. There's no need to explicitly convert to floating point, but you do need to substitute in your values for n
:
syms n;
S = 3*kroneckerDelta(n - 1, 0) + 2*kroneckerDelta(n - 2, 0) + 5*kroneckerDelta(n, 0);
N = -5:5;
S2 = subs(S,n,N);
stem(N,S2);
Another option is to use matlabFunction
to convert the symbolic expression to a numeric function:
syms n;
S = 3*kroneckerDelta(n - 1, 0) + 2*kroneckerDelta(n - 2, 0) + 5*kroneckerDelta(n, 0);
S2 = matlabFunction(S);
N = -5:5;
stem(n,S2(N));