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

Antworten (1)

Matt J
Matt J am 10 Feb. 2023
Bearbeitet: Matt J am 10 Feb. 2023

0 Stimmen

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 Kommentare

Ethan White
Ethan White am 10 Feb. 2023
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 am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 10 Feb. 2023

Bearbeitet:

am 10 Feb. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by