How do I save vector values from for loop?

2 vues (au cours des 30 derniers jours)
Jaevon Stewart
Jaevon Stewart le 4 Avr 2024
Commenté : Voss le 4 Avr 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.

Réponse acceptée

Voss
Voss le 4 Avr 2024
Modifié(e) : Voss le 4 Avr 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 commentaires
Jaevon Stewart
Jaevon Stewart le 4 Avr 2024
You always come through for me. I appreciate it!
Voss
Voss le 4 Avr 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by