MATLAB: Vectorizing a for loop

vectorization

end
I need to vectorize this for loop, but i am having trouble. so far I have this. I do not understand what I am doing wrong. I understand the concept of what I am trying to do, but having trouble applying it.
ni = 8;
k=2:9;
j=[];
y=[];
y(1) = 12 + 6*cos(0);
j =cumsum((k-1)*((20)/ni));
y(k) = 12 + 6*cos(2*pi.*j./20);
any help would be greatly appreciated.

Best Answer

You're overcomplicating. I'm not sure what you're trying to do with cumsum
tstart = 0;
tend = 20;
ni = 8;
t = linspace(tstart, tend, ni+1);
y = 12 + 6*cos(2*pi*t/(tend-tstart)); %same expression as original with the index
Related Question