Plotting graph for iterations of a for loop

13 views (last 30 days)
Essentially I have the roots of a polynomial, which have been converted into a 3x7 matrix. I want my code to run through each column of the matrix and plot a graph for each iteration. I can get it to run through the loop and plot multiple graphs, but it's always for the first column of the graph, and not moving on to the next one. Is anyone able to advise how I might be able to move my for-loop onto the next column? Thanks!
alpha_0 = [-1,-0.5,-0.1,0,0.1,0.5,1];
N = numel(alpha_0);
R = nan(3,N);
for alpha = 1:N % loop over indices
p = [1, 2*alpha_0(alpha), alpha_0(alpha)^2, -1/2];
R(:,alpha) = roots(p);
end
Roots = array2table(R)
[numRows, numCols] = size(R);
for col = R([1:7]) %for each column
for rows = 1:size(numRows) %for each row
i = R(:,rows)
delta_w = (i * (n_bn_0)^(1/3)) .* (k .* V_s)./((1 + (klambda_D).^2).^(1/2));
omega3 = ((k .* V_s)./((1 + (klambda_D).^2).^(1/2)) + delta_w)/w_pe;
h = figure;
plot(klambda_D, omega3, 'DisplayName', 'omega_3');
ylim([0 0.1])
xlabel('k * \lambda_D')
ylabel(' omega \omega')
grid on
hold on
end
saveas(h,sprintf('omega3%d.png',rows));
hold off
lgd = legend;
lgd.FontSize = 14;
lgd.Title.String = 'Disp relations';
col = col + 1;
end

Accepted Answer

Voss
Voss on 7 Feb 2023
First, let me run the code that calculates R:
alpha_0 = [-1,-0.5,-0.1,0,0.1,0.5,1];
N = numel(alpha_0);
R = nan(3,N);
for alpha = 1:N % loop over indices
p = [1, 2*alpha_0(alpha), alpha_0(alpha)^2, -1/2];
R(:,alpha) = roots(p);
end
R
R =
1.5652 + 0.0000i 1.1573 + 0.0000i 0.8617 + 0.0000i -0.3969 + 0.6874i -0.4642 + 0.6862i -0.7500 + 0.6614i -1.1486 + 0.6028i 0.2174 + 0.5217i -0.0786 + 0.6526i -0.3309 + 0.6861i -0.3969 - 0.6874i -0.4642 - 0.6862i -0.7500 - 0.6614i -1.1486 - 0.6028i 0.2174 - 0.5217i -0.0786 - 0.6526i -0.3309 - 0.6861i 0.7937 + 0.0000i 0.7285 + 0.0000i 0.5000 + 0.0000i 0.2972 + 0.0000i
Now, R([1:7]) (or R(1:7), since the [] are unnecessary here), is the first seven elements of R, so when used in the for loop below col will take the values of R(1) through R(7):
for col = R([1:7])
disp(col);
end
1.5652 0.2174 + 0.5217i 0.2174 - 0.5217i 1.1573 -0.0786 + 0.6526i -0.0786 - 0.6526i 0.8617
Notice that MATLAB counts down the first column of R first, then element #4 is the first element of the second column, and so on.
Now let me run another line:
[numRows, numCols] = size(R)
numRows = 3
numCols = 7
numRows and numCols are both scalars, so size(numRows) is [1 1]:
size(numRows)
ans = 1×2
1 1
When you use an array with the colon operator like 1:size(numRows), only the first element of the array is used, so 1:size(numRows) is the same as 1:1, which is the same as 1, so the following loop will iterate once and rows will have the value 1:
for rows = 1:size(numRows)
disp(rows);
end
1
The point is: both of the for loops in your code are probably not what you intended.
If you want to iterate over the columns of R, then try something like this:
for col = 1:numCols
i = R(:,col);
delta_w = (i * (n_bn_0)^(1/3)) .* (k .* V_s)./((1 + (klambda_D).^2).^(1/2));
omega3 = ((k .* V_s)./((1 + (klambda_D).^2).^(1/2)) + delta_w)/w_pe;
h = figure;
plot(klambda_D, omega3, 'DisplayName', 'omega_3');
ylim([0 0.1])
xlabel('k * \lambda_D')
ylabel(' omega \omega')
grid on
% hold on % no need to hold on/off
lgd = legend; % I guess you want the legend to show up in the saved image
lgd.FontSize = 14;
lgd.Title.String = 'Disp relations';
saveas(h,sprintf('omega3%d.png',col));
end
But it's hard to say for sure if that's right, without having the missing variables or knowing exactly what you want to plot.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!