Plotting the summation of a unit step function
Show older comments
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?
Answers (1)
Walter Roberson
on 3 Dec 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.
Categories
Find more on Mathematics 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!