Plotting the summation of a unit step function

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)

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 Comment

Ah see, I'm very new to Matlab, I've been following tutorials and basic instructions this whole time. So I have no idea how to do the things you said

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 3 Dec 2017

Commented:

on 3 Dec 2017

Community Treasure Hunt

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

Start Hunting!