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

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 Kommentare

Change
p1(i) = p1(i)
to
p1(i) = p1(i);
Hi! Thanks for helping out! With the ; it give me any output tho.
Shubham Gupta
Shubham Gupta am 15 Nov. 2019
Bearbeitet: Shubham Gupta am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 15 Nov. 2019

Beantwortet:

am 18 Nov. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by