Error: Conversion to logical from sym is not possible.

Why I get the error in title?
syms x h myconv
myconv=0;
h=0;
x(n)=(1/2^(-n+1))*(heaviside(n+2)-heaviside(n-2));
if (0<n)&&(n<7)
for i=-1000:1:n
h=h+(sin(2*i)+(-1)^i)*(heaviside(i+3)-heaviside(i-5));
end
else
h=0;
end
for i=-1000:1:1000
myconv=x(i)*h(n-i)+myconv;
end
N=-5:1:5;
figure
subplot(2,2,1);
stem(N, subs(x,n,N), 'bo');
title('x(n)')
subplot(2,2,2);
stem(N, subs(h,n,N), 'ro');
title('h(n)')
subplot(2,2,3);
stem(N, subs(myconv,n,N), 'ko');
title('convolution')

5 件のコメント

Walter Roberson
Walter Roberson 2018 年 3 月 19 日
n is not defined.
Based on your previous questions, n would be expected to be symbolic, but your code here is treating it as numeric and as a positive integer, up until you try to subs() using n as the name of a variable to substitute for.
geometry geometry
geometry geometry 2018 年 3 月 19 日
Could you explain more?
how I change my code to fix this?
geometry geometry
geometry geometry 2018 年 3 月 19 日
I changed my code and tried this:
syms x h myconv H n
myconv=0;
h=0;
H(n)=heaviside(n)+(1/2)*piecewise(n==0,1,0);
% x=input('Enter x: ');
% h=input('Enter h: ') ;
for i=-1000:1:n
h=h+(sin(2*i)+(-1)^i)*(H(i+3)-H(i-5));
end
h(n)=(H(n-1)-H(n-7))*h(n);
x(n)=(1/2^(-n+1))*(heaviside(n+2)-heaviside(n-2));
% h = matlabFunction(h);
% x = matlabFunction(x);
for i=-1000:1:1000
myconv=x(i)*h(n-i)+myconv;
end
N=-5:1:5;
figure
subplot(2,2,1);
stem(N, subs(x,n,N), 'bo');
title('x(n)')
subplot(2,2,2);
stem(N, subs(h,n,N), 'ro');
title('h(n)')
subplot(2,2,3);
stem(N, subs(myconv,n,N), 'ko');
title('convolution')
Now I'm getting error:
Error using : (line 38) Cannot compute the number of steps from -1000 to n by 1.
Error in problem42 (line 7) for i=-1000:1:n
Walter Roberson
Walter Roberson 2018 年 3 月 19 日
You do not change your code to fix this. Your entire premise is broken. You are not going to be able to find the infinite limit of the convolution of arbitrary functions using the techniques you are using. You would need to apply analytical reasoning to figure out the zone in which the function is not zero, do the calculation within that zone, and then the infinite limit is the same since going out further will just be zeros outside that range.
Walter Roberson
Walter Roberson 2018 年 3 月 19 日
"Error using : (line 38) Cannot compute the number of steps from -1000 to n by 1."
It is not permitted to have a symbolic variable as a bound for a for loop.

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

回答 (0 件)

カテゴリ

質問済み:

2018 年 3 月 19 日

コメント済み:

2018 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by