How do I create a vector from a for loop with different intervals?

3 views (last 30 days)
Hi all!
I am trying to get a vector out of this loop, but it just gives me a vector for every iteration.
Does anyone know how to solve this?
t1 = 0.6; %[sec]
po = 100; %[kip]
E = 3000; %[ksi]
m = 20/386; %[kip^2/in]
k = 36.2222;
wn = sqrt(k/m); %[rad/sec]
T = (2*pi)/wn; %[sec]
zeta = 0.02;
c = 2*zeta*m*wn; % Damping constant [lb*s/in]
nstep = (6*T)/0.05;
nsteps = int16(fix(nstep))
ivalues = [1:nsteps]';
tvalues = [0:0.05:6*T]';
%Excitation functions
p1 = zeros(nsteps,1);
for i = 1:length(tvalues)
if tvalues(i) < (t1/2)
p1(i) = (2*po*tvalues(i))/t1;
elseif tvalues(i) > (t1/2) & tvalues(i) < t1
p1(i) = 2*po*(1-(tvalues(i)/t1));
else
p1(i) = 0;
end
p1(i) = p1(i)
end % I need a 1x28 vector with the p values.
Thank you in advance!
Josefine
  3 Comments
Shubham Gupta
Shubham Gupta on 15 Nov 2019
Edited: Shubham Gupta on 15 Nov 2019
After the loop use "fprintf()" or "disp()" to diplay p1 vector. Or you can simply write p1 without ; to display it. semicolon (;) stops MATLAB from printng out data in command window just get a grasp when do you want MATLAB to show the results.

Sign in to comment.

Answers (1)

Athul Prakash
Athul Prakash on 18 Nov 2019
Hey Josefine,
Everytime MATLAB executs the line
p1(i) = p1(i)
It prints the result, which in this case is the entire vector 'p1'.
This line gets you the output during every iteration.
My suggestion is to remove this line, since it doesn't serve another purpose, and use
disp(p1);
or simply
p1
after the for loop ends.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!