how can i use symsum to make function with a variable apart from syms k?

5 ビュー (過去 30 日間)
JaeHyeong Park
JaeHyeong Park 2016 年 9 月 29 日
編集済み: Paul 2021 年 3 月 31 日
hello
i'm trying to perform convolution and plot the result without using conv function in matlab.
i first created two function with variable n to perform convolution.
then, by using the definition of convolution in discrete domain,
i used symsum function to find the sum of each product in the series.
the problem is that the symsum keeps giving me errors.
what should i do to solve the problem? please help
below are the codes i made
close all
clear all
clc
n=-40:40;
unitstep=@(n)round(heaviside(n));
discretedelta=@(n)(n==0);
x=@(n)unitstep(n)-unitstep(n-21);
y=@(n)discretedelta(n-1)+2*discretedelta(n-2)+3*discretedelta(n-3)+2*discretedelta(n-4)+discretedelta(n-5);
syms k;
convxy=@(n)symsum(x(k)*y(n-k),k,-inf,inf);
subplot(2,2,1),
stem(n,x(n))
axis([-4 21 0 3])
hold on
subplot(2,2,2),
stem(n,y(n));
axis([-4 21 0 3])
hold on
subplot(2,2,[3 4]),
stem(n,convxy(n))
axis([-inf inf 0 inf])
hold on

回答 (2 件)

myFirst Name
myFirst Name 2021 年 3 月 30 日
Hi,
move "syms k;" line to line before x=@...

Paul
Paul 2021 年 3 月 31 日
This code works for the specified sequences. It would need to be modified if either x[n] or y[n] take on non-zero values for n < 0
syms n k real
x(n) = heaviside(n) - heaviside(n-21);
y(n) = kroneckerDelta(n-1) + 2*kroneckerDelta(n-2) + 3*kroneckerDelta(n-3) + 2*kroneckerDelta(n-4) + kroneckerDelta(n-5);
convxy(n) = symsum(x(k)*y(n-k),k,0,n); % summation limits based on fact that x and y are zero for n < 0
% conv solution for comparison
xnum = ones([1 21]);
ynum = [0 1 2 3 2 1];
cnum = conv(xnum,ynum);
stem(0:25,cnum)
hold on;
stem(0:28,double(convxy(sym(0:28))),'x')
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 31 日
You might want to modify sympref('HeavisideAtOrigin')
Paul
Paul 2021 年 3 月 31 日
編集済み: Paul 2021 年 3 月 31 日
Great point. I changed mine long ago and it's reflected in the code posted above.
>> sympref('HeavisideAtOrigin')
ans =
1
It's too bad the SMT doesn't have a standard, discrete unit step function.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by