How do I save vector values from for loop?

2 visualizaciones (últimos 30 días)
Jaevon Stewart
Jaevon Stewart el 4 de Abr. de 2024
Comentada: Voss el 4 de Abr. de 2024
I have this section of code here
az = 0:90:270;
for k = 1:length(az)
rR = 0:.1:1;
L1 = (rR.*rR.*.5)+(mu.*rR.*thetanaught.*sind(az(k)))+(.5.*thetanaught.*mu.^2.*sind(az(k)).^2)+(.5.*twist.*rR.^3)+(mu.*twist.*rR.^2.*sind(az(k)))+(.5.*twist.*mu^2.*rR.*sind(az(k)).^2)+(.5.*rR.^2.*Theta_1c.*cosd(az(k)))+(rR.*mu.*Theta_1c.*sind(az(k)).*cosd(az(k)));
L2 = (.5.*rR.^2.*Theta_1s.*sind(az(k)))+(.5.*mu.^2.*Theta_1s.*sind(az(k)).^3)-(.5.*lambda.*rR)-(.5.*mu.*lambda.*sind(az(k)))-(.5.*rR.*mu.*Beta.*cosd(az(k)))-(.5.*mu.^2*Beta.*cosd(az(k)).*sind(az(k)));
Lc = L1+L2
end
I want to be able to plot Lc for each of the az values. So it looks something like this.
What is an efficient way to get matlab to store the vectors from each loop so I can do this? The way I was doing it before was just saving the values and I ended up with a 1x44 vector.

Respuesta aceptada

Voss
Voss el 4 de Abr. de 2024
Editada: Voss el 4 de Abr. de 2024
az = 0:90:270;
rR = 0:0.1:1; % pull rR out of the loop (it never changes)
N = numel(az);
Lc = zeros(numel(rR),N); % make Lc a matrix with N columns
for k = 1:N
L1 = (rR.*rR.*0.5)+(mu.*rR.*thetanaught.*sind(az(k)))+(0.5.*thetanaught.*mu.^2.*sind(az(k)).^2)+(0.5.*twist.*rR.^3)+(mu.*twist.*rR.^2.*sind(az(k)))+(0.5.*twist.*mu^2.*rR.*sind(az(k)).^2)+(0.5.*rR.^2.*Theta_1c.*cosd(az(k)))+(rR.*mu.*Theta_1c.*sind(az(k)).*cosd(az(k)));
L2 = (0.5.*rR.^2.*Theta_1s.*sind(az(k)))+(0.5.*mu.^2.*Theta_1s.*sind(az(k)).^3)-(0.5.*lambda.*rR)-(0.5.*mu.*lambda.*sind(az(k)))-(0.5.*rR.*mu.*Beta.*cosd(az(k)))-(0.5.*mu.^2*Beta.*cosd(az(k)).*sind(az(k)));
Lc(:,k) = L1+L2; % store each result in a column of Lc
end
figure
plot(Lc) % plot N lines (each column of Lc is plotted as a separate line)
  2 comentarios
Jaevon Stewart
Jaevon Stewart el 4 de Abr. de 2024
You always come through for me. I appreciate it!
Voss
Voss el 4 de Abr. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by