why I'm getting error with symsum?

I have tried this code to calculate energy and power of discrete time functions. but I get errors. how can I fix it?
syms n f z N;
f=input('enter function: ','s');
f = symfun(eval(f), n);
f=f*conj(f);
f = matlabFunction(f);
y(N)=symsum(f, -N , N);
energy=limit(y(N),N,inf);
z(N)=y(N)/(2*N+1);
pow=limit(z(N),N,inf);

9 件のコメント

Walter Roberson
Walter Roberson 2018 年 3 月 19 日
Why are you using eval() for that code??
If you are going to use eval() then why not just use input() without the 's' option?
geometry geometry
geometry geometry 2018 年 3 月 19 日
So in that way is f recognized by matlab a function of 'n'?
I tried this but again I get error with symfun:
"Error using symengine Singularity.
Error in sym/symsum (line 68) rSym = mupadmex('symobj::map',fsym.s,'symobj::symsum',x.s,a.s,b.s);
Error in problem32 (line 6) y(N)=symsum(f, -N , N);"
syms n f z N;
f=input('enter function: ');
f=f*conj(f);
f = matlabFunction(f);
y(N)=symsum(f, -N , N);
energy=limit(y(N),N,inf);
z(N)=y(N)/(2*N+1);
pow=limit(z(N),N,inf);
Walter Roberson
Walter Roberson 2018 年 3 月 19 日
What function are you entering?
geometry geometry
geometry geometry 2018 年 3 月 19 日
It works fine for some functions and doesn't work for some functions like 1/n (which I get the above errors).
and for some functions like cos((pi/4)*n) it gives the outputs:
energy =
limit(symsum(cos((pi*n)/4)^2, n, -N, N), N, Inf, 'Left')
>> pow
pow =
limit(symsum(cos((pi*n)/4)^2, n, -N, N)/(2*N + 1), N, Inf)
Walter Roberson
Walter Roberson 2018 年 3 月 19 日
... Yes?
1/n is expected to have a problem since you have n pass through 0.
limit() seldom knows how to take the limit of a symsum.
geometry geometry
geometry geometry 2018 年 3 月 19 日
How can I use discrete time dirac function in input? in my previous questions you suggested to use piecewise(n==0,1,0). but I get a lot of errors:
Error using symengine Cannot generate code for piecewise for use in anonymous functions.
Error in sym/matlabFunction>mup2mat (line 401) res = mupadmex('symobj::generateMATLAB',r.s,ano,spa,0);
Error in sym/matlabFunction>mup2matcell (line 373) r = mup2mat(c{1},true,sparseMat);
Error in sym/matlabFunction (line 184) body = mup2matcell(funs, opts.Sparse);
Error in problem32 (line 5) f = matlabFunction(f);
my input was:
piecewise(n==4,1,0)-2*heaviside(n+3)+2*heaviside(n-3)
Walter Roberson
Walter Roberson 2018 年 3 月 19 日
Use the 'file' option of matlabFunction. The .m that is written will use if to implement the piecewise(). Note, however, that the result will not be vectorized
Note: you should not be using symsum() on a function handle. symsum() should only be executed on a symbolic expression.
symsum() is not very good at reasoning about dirac and heaviside
geometry geometry
geometry geometry 2018 年 3 月 19 日
編集済み: geometry geometry 2018 年 3 月 19 日
I changed the code and tried without symsum and using "for" but again I get a lot of errors:
syms f n y x Z p s H i E P
E=0;
P=0;
H(n)=heaviside(n)+(1/2)*piecewise(n==0,1,0);
%x(n)=piecewise(n==0,1,0)+piecewise(n==4,1,0)-2*heaviside(n+3)+2*heaviside(n-3);
%Z(n)=(-1)^(n)*cos(n*pi/2)*x(n);
f=input('enter function: ');
f=f*conj(f);
f = matlabFunction(f);
for i=-N:N
E=E+f(i);
end
P(N)=E/(2*N+1);
E=limit(E(N),N,inf);
P=limit(P(N),N,inf);
when I enter x(n) and z(n) (which are commented in the code).
Walter Roberson
Walter Roberson 2018 年 3 月 19 日
You did not use the 'file' option of matlabFunction .
You have
for i=-N:N
but you have not defined N .
Your later line E=limit(E(N),N,inf); would expect N to be sym, but it is not permitted to have a for loop over symbolic range.
Note: you should probably be replacing your heaviside with piecewise, as heaviside has ambiguous meaning at 0.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

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