Plotting the summation of a unit step function
Afficher commentaires plus anciens
I need to graph the following forcing function for a HW assignment:

I looked up some tutorials online for summation in this form:
syms x
k = 1:200 ;
W = zeros(size(k)) ;
for i = 1:length(k)
W(i) = symsum(x/2,x,0,k(i)) ;
end
plot(k,W)
So I took that, switched around x and k (I made sure this worked with the original equation and subbed in my function to make this:
syms k
x = 1:200 ;
W = zeros(size(x)) ;
for i = 1:length(x)
W(i) = symsum(heaviside(x-pi*k)*(-1).^k, k, 0, x(i)) ;
end
plot(x,W)
I'm ignoring the 1 + 2* part of the sum at the moment. When I try to run that second version I get this error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in hw5 (line 15)
W(i) = symsum(heaviside(x-pi*k)*(-1).^k, k, 0, x(i)) ;
How do I get past this and actually graph my function?
Réponses (1)
Walter Roberson
le 3 Déc 2017
x = 1:200 ;
makes x a vector.
W = zeros(size(x)) ;
makes W a vector the same size as x
for i = 1:length(x)
W(i) = symsum(heaviside(x-pi*k)*(-1).^k, k, 0, x(i)) ;
end
THe x-pi*k part is a vector because x is a vector, so the symsum is going to be a vector. You are trying to store that vector into the single location W(i) . Perhaps you only wanted x(i) instead of x.
1 commentaire
Sam Ajami
le 3 Déc 2017
Catégories
En savoir plus sur Mathematics 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!