Can't use function in summation
Show older comments
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);
Answers (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 Comments
Ethan White
on 10 Feb 2023
Matt J
on 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.
Torsten
on 10 Feb 2023
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!