How to increase size of array

Hey guys, I have this code (below) that leads to a plot. My question is how can I increase the size of Zh so my w linspace can have more data points? Right now, it can only do 33 because Zh is a 1x33. Is there any way I can change that? I'm not too advanced in Matlab, so I'm not sure how to increase it.
w_limit = 5*2*pi;
wn = sqrt(k./m);
F_harm = 1;
w = 0;
Zh = 0;
for w = 0:w_limit
r = w./wn;
Ft = k*Y*(r.^2)*(sqrt((1 + ((2*z*r).^2))/(((1-(r.^2)).^2)+((2*z*r).^2))));
F_harm = [F_harm;Ft];
Z = m*(w.^2)*0.0254/sqrt(((k-(m*(w.^2))).^2)+(c*w).^2);
Zh = [Zh Z];
w = w + 0.001;
end
w = linspace(0,5*2*pi,33);
figure(4)
plot(w,Zh)

 Accepted Answer

w_limit = 5*2*pi;
5*2*pi is 10*pi which is about 31.4 .
for w = 0:w_limit
that would be 0:31.4-ish which would be 0, 1, 2, 3, ... 29, 30, 31 . That is 32 different iterations.
Zh = 0;
Zh starts out with a 0
Zh = [Zh Z];
Each of those 32 iterations, a new value is put at the end of Zh. Because Zh started with 0, the array will have 32+1 = 33 entries.
Z = m*(w.^2)*0.0254/sqrt(((k-(m*(w.^2))).^2)+(c*w).^2);
With w starting out as 0, the Z entry corresponding to w == 0 will be 0. So when Z is put on the end of the Zh that starts out as 0, the first two entries are going to end up being 0.
w = w + 0.001;
w is your for loop variable. Each iteration of the for loop, any change you made to w at the end of the loop will be ignored, as if you had not changed w (exception: the very last change "sticks")
So the second iteration will not use w of 0.001, and the one after that will not use w of 0.002 -- you are using w = 0:w_limit and that tells it to proceed by integer steps.
If you had used
for w = 0:0.001:w_limit
then your w would change 0.001 at a time.

1 Comment

Okay I get it, thank you. I increased it a bit and the plot seems to be much more fluid, as well as the subsequent plots not shown here.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!