plotting Sum function , with floor term and rectangular pulse

Hey,
Is there a way to plot this function in Matlab?
y(t) =
while is the function : , and u is the Heaviside function.

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 21 Avr 2020
Modifié(e) : Ameer Hamza le 21 Avr 2020
try this
syms t n
chi = heaviside(t-n) - heaviside(t-2-n);
term = (-1)^n*floor((n+2)/2).*chi;
y = symsum(term, n, 0, inf);
t_vec = linspace(0, 10, 100);
y_vec = vpa(subs(y, t, t_vec));
plot(t_vec, y_vec, '-o')
A faster alternative:
chi = @(t, n) heaviside(t-n) - heaviside(t-2-n);
term = @(t, n) (-1).^n.*floor((n+2)/2).*chi(t,n);
y = @(t) sum(term(t, 0:ceil(t+1)));
t_vec = linspace(0, 10, 100);
y_vec = zeros(size(t_vec));
for i=1:numel(t_vec)
y_vec(i) = y(t_vec(i));
end
plot(t_vec, y_vec, '-o')

3 commentaires

The second code is faster and it takes advantage of the fact that for n > t, the term become zero, so no need to sum those terms.
Thank you :)
Glad to be of help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by