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);

Risposte (1)

Matt J
Matt J il 10 Feb 2023
Modificato: Matt J il 10 Feb 2023
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 Commenti

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?
Matt J
Matt J il 10 Feb 2023
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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 10 Feb 2023

Modificato:

il 10 Feb 2023

Community Treasure Hunt

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

Start Hunting!

Translated by