Clear Filters
Clear Filters

How to plot multi-line graph for nested loop?

4 views (last 30 days)
Hi, I want to plot a multi-line graph for 2 ranges of variable input by user which are R and N. A graph of d_pred vs N must be plotted, whereby each line represents the d_pred calculated for each range of N at interval of 50 for each range of R input by users (interval 0.1). A table must be displayed for each of the lines plotted in the graph. The experimental value d_exp vs N must also be plotted in the same graph where it satisfies the range of N given and calculate AARD value between d_pred and d_exp representing each line plotted. Below is the code that I tried to run but failed. I used the nested loop command as the range of R and N affects each other in the equations used. Please help me.
% Given constants
p = 997;
Di = 0.01;
t = 40 * (10^-3);
% Prompt the user to input the range of variable N and R
N_range = input('Enter the range of variable N [start, end] (in rpm): ');
R_range = input('Enter the range of variable R [start, end]: ');
% Generate the range of variable N with intervals of 50
N_interval = 50;
N_values = N_range(1):N_interval:N_range(2);
% Initialize figure
figure;
% Nested loops to vary N and R
for i = 1:length(R_range)
R = R_range(i);
for j = 1:length(N_values)
N = N_values(j)/60;
% Calculate We for the current R value
We = (p * (N^2) * (Di^3)) / t;
% Calculate d_pred for the current R value
d_pred = (0.0219 * (1 + 1.1309 * R) * (We^-0.6)) * Di;
% Plot the graph for d_pred vs N for the current R
plot(N, d_pred, 'LineWidth', 2, 'DisplayName', ['R=' num2str(R)]);
hold on;
% Display the values of N and d_pred at every interval in a table
table_values = table(N', d_pred', 'VariableNames', {'N', 'd_pred'});
disp(['Predicted Values (R=' num2str(R) '):']);
disp(table_values);
% Calculate and display AARD
aard = mean(abs((d_exp - interp1(N_values, d_pred, N_exp))/d_exp));
disp(['Absolute average relative deviation (AARD) between experimental droplet diameter and predicted droplet diamter is ' num2str(aard)]);
end
end
% Experimental data
N_exp = [6000 7000 8000 9000];
d_exp_N = [11 9 8 7] * (10^-6);
% Display the values of N_exp and d_exp_N
table_values_exp = table(N_exp', d_exp_N', 'VariableNames', {'N_exp', 'd_exp'});
disp('Experimental Values (N):');
disp(table_values_exp);
% Add a plot for d_exp_N vs N in the same graph
scatter(N_exp, d_exp_N, 'r', 'filled', 'DisplayName', 'Experimental (N)');
% Add labels and legend
title('Nested Loop Graph: d\_pred vs N for Different R Values');
xlabel('N (rpm)');
ylabel('d\_pred');
legend('Location', 'Best');
grid on;
% Hold off to stop adding to the current plot
hold off;

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 Nov 2023
From a quick scan the following inconsiostencies are spotted in your code.
(1) d_exp is not pre-defined but used in the loop. Maybe it d_exp_N then it should be define before the loop.
(2) R is not predicted value but pre-assigned from the user prompt of R_range.
(3) entries in interpolation .. interp1(N_values, d_pred, N_exp) is not correct. It should be .. interp1(d_pred, N_exp, N_values).
(4) size of d_pred and N_exp must match.
(5) why two value sof R_range is used. Probably it should be R_range_ii = linspace(R_range(1), R_range(2), N_interval).
(6) It looks like all calcs can be done using vectorization by matching the sizes of input entries.
  3 Comments
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 25 Nov 2023
(1) d_exp is not pre-defined but used in the loop. Maybe it d_exp_N then it should be define before the loop. See the calculation of variable aard:
...
for j = 1:length(N_values)
...
% Calculate and display AARD
aard = mean(abs((d_exp - interp1(N_values, d_pred, N_exp))/d_exp));
disp(...)
(2) R is not predicted value but pre-assigned from the user prompt of R_range. See your code:
for i = 1:length(R_range)
R = R_range(i);
for j=length(N_values)
...
% Display the values of N and d_pred at every interval in a table
table_values = table(N', d_pred', 'VariableNames', {'N', 'd_pred'});
disp(['Predicted Values (R=' num2str(R) '):']);
(5) why two values of R_range is used. Probably it should be R_range_ii = linspace(R_range(1), R_range(2), N_interval). R_range has two values only which are R_range(1) and R_range(2) correspond to [start, end]
R_range = input('Enter the range of variable R [start, end]: ');

Sign in to comment.

Categories

Find more on Live Scripts and Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!