Can't use function in summation

Hi, I'm trying to plot the sum of a function x(n), but the code gives an error saying Conversion to logical from sym is not possible. I feel like this should be simple... am I missing something about properly creating or utilizing the function?
%%% Functions and calculations
syms n;
x(n) = (1/n) * uStep(n-1);
energyFunc(n) = symsum( abs( x(k) )^2 ,k,1,n);
domain = 1:150;
energy = 1:150;
for i=1:150
energy(i) = energyFunc(domain(i));
end
%%% Plot energy convergence
plot(domain,energy)
grid on
title('Energy of x(n)')
xlabel('iteration n')
ylabel('energy')
%%%%% Functions
%%% Unit Step Function
function output = uStep(x)
if x >= 0
output = 1;
else
output = 0;
end
end
Conversion to logical from sym is not possible.
Error in Lab2q5>uStep (line 31)
if x >= 0
Error in Lab2q5 (line 8)
x(n) = (1/n) * uStep(n-1);

回答(1 个)

I would suggest not using syms, except when it is truly necessary.
domain=1:150;
uStep=(domain>=1);
energy=cumsum(uStep./domain.^2);
%%% Plot energy convergence
plot(domain,energy)
grid on
title('Energy of x(n)')
xlabel('iteration n')
ylabel('energy')

3 个评论

Hi Matt, cumsum seems like it might be what I need, but it has to reference that x(n) equation which uses the uStep function. How would that be implemented?
but it has to reference that x(n) equation which uses the uStep function.
I don't think it does. I think my answer already implements in full what you were aiming for in your post.
I think energyFunc is intended to be a series of functions x_n(t), not as a simple sum of numbers.
Maybe
x_n(t) = 1/n * uStep(t-(n-1))
or something similar.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

编辑:

2023-2-10

Community Treasure Hunt

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

Start Hunting!

Translated by