# How do I index outputs from a loop?

1 回表示 (過去 30 日間)
Nick Lavanture 2022 年 5 月 11 日

Below is my code, but I would like to plot d and dd_dN vs each iteration number
So I believe I need to index and save each outputted d and dd_dN for each iteration, a total of (1+240+1800)*25 = 51025 values for both d and dd_dN
Any help is greatly appreciated, thank you!
clc,clear
format long g
c = 1.1e-10;
m = 3.3;
p500 = [ -0.3142 2.3172 -7.0249 11.2867];
p2000 = [ -1.2577 9.2728 -28.1056 45.1501];
p3150 = [ -1.9807 14.6042 -44.2653 71.1096];
d = 0.015;
for i=1:25
dd_dN = c*((0.706*(p3150(1)+3.150)+0.537*(2*d/pi)*p3150(2)+0.448*(d^2./2)*p3150(3)+0.393*(4*d^3/(3*pi))*p3150(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
for j=1:240
dd_dN = c*((0.706*(p2000(1)+2.000)+0.537*(2*d/pi)*p2000(2)+0.448*(d^2./2)*p2000(3)+0.393*(4*d^3/(3*pi))*p2000(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
end
for j=1:1800
dd_dN = c*((0.706*(p500(1)+.500)+0.537*(2*d/pi)*p500(2)+0.448*(d^2./2)*p500(3)+0.393*(4*d^3/(3*pi))*p500(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
end
end
fprintf('The final crack depth is %18.16f inches',d)
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Jeffrey Clark 2022 年 5 月 12 日
% d is changed for every calculation of dd_dN which uses d
% want to create two output vectors for dd_dN and d itteration values
d_vals = zeros(1,(1+240+1800)*25);
dd_dN_vals = d_vals;
val_index = 1;
c = 1.1e-10;
m = 3.3;
p500 = [ -0.3142 2.3172 -7.0249 11.2867];
p2000 = [ -1.2577 9.2728 -28.1056 45.1501];
p3150 = [ -1.9807 14.6042 -44.2653 71.1096];
d = 0.015;
for i=1:25
dd_dN = c*((0.706*(p3150(1)+3.150)+0.537*(2*d/pi)*p3150(2)+0.448*(d^2./2)*p3150(3)+0.393*(4*d^3/(3*pi))*p3150(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
dd_dN_val(val_index) = dd_dN;
d_val(val_index) = d;
val_index = val_index+1;
for j=1:240
dd_dN = c*((0.706*(p2000(1)+2.000)+0.537*(2*d/pi)*p2000(2)+0.448*(d^2./2)*p2000(3)+0.393*(4*d^3/(3*pi))*p2000(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
dd_dN_val(val_index) = dd_dN;
d_val(val_index) = d;
val_index = val_index+1;
end
for j=1:1800
dd_dN = c*((0.706*(p500(1)+.500)+0.537*(2*d/pi)*p500(2)+0.448*(d^2./2)*p500(3)+0.393*(4*d^3/(3*pi))*p500(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
dd_dN_val(val_index) = dd_dN;
d_val(val_index) = d;
val_index = val_index+1;
end
end
% Note that at the end val_index is 1 larger than the size of the _val arrays
fprintf('The final crack depth is %18.16f inches',d)
KSSV 2022 年 5 月 12 日
dd_dN_val(val_index) = dd_dN;
d_val(val_index) = d;
The above lines should be:
dd_dN_vals(val_index) = dd_dN;
d_vals(val_index) = d;
Which you have initialized into array right?

サインインしてコメントする。

### 回答 (1 件)

Siraj 2023 年 9 月 18 日
Hi!
It is my understanding that you would like to plot the variables "d" and "dd_dN" against the iteration number. To plot the variables "d" and "dd_dN" against the iteration number, it is necessary to first store all the values of "d" and "dd_dN" during the iterations.
As the total number of iterations is known, it makes it easier to preallocate arrays to store the values of "d" and "dd_dN" for each iteration. After the loop, you can utilize the "plot" function to plot the values of "d" and "dd_dN" against the iteration number. I have incorporated some additional lines of code into your existing code. The added lines are as follows:
d_vals = zeros(1,(1+240+1800)*25);
dd_dN_vals = zeros(1,(1+240+1800)*25);
val_index = 1;
Here, two arrays, d_vals and dd_dN_vals, are initialized with zeros. The size of these arrays is determined by the total number of iterations to be performed, which is (1+240+1800)*25. The variable val_index is set to 1, which will be used as an index to store values in the arrays.
After calculating the value of dd_dN, it is stored in the dd_dN_vals array at the current val_index position. Similarly, the current value of d is stored in the d_vals array. Then, val_index is incremented by 1 to move to the next position in the arrays.
These lines of code are repeated within the nested loops, ensuring that for each iteration, the corresponding values of dd_dN and d are stored in the respective arrays.
Here is the complete code with the additional lines for your reference and better understanding.
d_vals = zeros(1, (1 + 240 + 1800) * 25); % Preallocate array to store values of "d" for each iteration
dd_dN_vals = zeros(1, (1 + 240 + 1800) * 25); % Preallocate array to store values of "dd_dN" for each iteration
val_index = 1; % Initialize index for storing values in arrays
c = 1.1e-10;
m = 3.3;
p500 = [-0.3142 2.3172 -7.0249 11.2867];
p2000 = [-1.2577 9.2728 -28.1056 45.1501];
p3150 = [-1.9807 14.6042 -44.2653 71.1096];
d = 0.015;
for i = 1:25
dd_dN = c * ((0.706 * (p3150(1) + 3.150) + 0.537 * (2 * d / pi) * p3150(2) + 0.448 * (d^2 / 2) * p3150(3) + 0.393 * (4 * d^3 / (3 * pi)) * p3150(4)) * (sqrt(pi * d)))^m;
d = d + dd_dN;
dd_dN_vals(val_index) = dd_dN; % Store value of "dd_dN" at current index
d_vals(val_index) = d; % Store value of "d" at current index
val_index = val_index + 1; % Increment index
for j = 1:240
dd_dN = c * ((0.706 * (p2000(1) + 2.000) + 0.537 * (2 * d / pi) * p2000(2) + 0.448 * (d^2 / 2) * p2000(3) + 0.393 * (4 * d^3 / (3 * pi)) * p2000(4)) * (sqrt(pi * d)))^m;
d = d + dd_dN;
dd_dN_vals(val_index) = dd_dN; % Store value of "dd_dN" at current index
d_vals(val_index) = d; % Store value of "d" at current index
val_index = val_index + 1; % Increment index
end
for j = 1:1800
dd_dN = c * ((0.706 * (p500(1) + 0.500) + 0.537 * (2 * d / pi) * p500(2) + 0.448 * (d^2 / 2) * p500(3) + 0.393 * (4 * d^3 / (3 * pi)) * p500(4)) * (sqrt(pi * d)))^m;
d = d + dd_dN;
dd_dN_vals(val_index) = dd_dN; % Store value of "dd_dN" at current index
d_vals(val_index) = d; % Store value of "d" at current index
val_index = val_index + 1; % Increment index
end
end
fprintf('The final crack depth is %18.16f inches', d)
The final crack depth is 0.0150000007332667 inches
figure, plot(d_vals)
title("d vs index")
figure, plot(dd_dN_vals)
title("dd\_dN vs index")
After the loops, the plot function is used to create two separate figures. The first figure displays the values of d against the index(iteration), while the second figure shows the values of dd_dN against the index(iteration).
Hope this helps.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by