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

2 visualizaciones (últimos 30 días)
Josefine Olsson
Josefine Olsson el 15 de Nov. de 2019
Respondida: Athul Prakash el 18 de Nov. de 2019
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 comentarios
Josefine Olsson
Josefine Olsson el 15 de Nov. de 2019
Hi! Thanks for helping out! With the ; it give me any output tho.
Shubham Gupta
Shubham Gupta el 15 de Nov. de 2019
Editada: Shubham Gupta el 15 de Nov. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Athul Prakash
Athul Prakash el 18 de Nov. de 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.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by