How to output a for to a 10x27 table (for loop keeps overwriting the variable)
1 回表示 (過去 30 日間)
古いコメントを表示
Marshall Thompson
2023 年 12 月 3 日
コメント済み: Marshall Thompson
2023 年 12 月 6 日
I am trying to iterate an equation using a range of values as a linspace(2, 3, 10) for one variable and set of 27 variable for the second variable.
The equation I am trying to iterate is assigned to a variable sigma_x_case1 (See Attached - Equation.png)
The For Loop that I currently have is as follows:
height_range_data = linspace(2, 3, 10);
diameter_range_data = [0.405, 0.54, 0.675, 0.84, 1.05, 1.315, 1.66, 1.9 ,2.375, 2.8753, 5, 4, 4.5, 5, 5.563, 6.625, 8.625, 10.75, 12.75, 14, 16, 18, 20, 24, 30, 36, 48];
d = diameter_range_data;
for h = height_range_data;
sigma_x_case1_it1 = (subs(sigma_x_case1))
end
*** EDIT ***
*** Should be a 10 row (h) by 27 (d) table ***
sigma_x_case1 is a equation that was solved using other variables... would I need to convert this equation to a function (d, h) to perform the loop?
Please see the attached code for reference (Question_Thompson_Marshall_AENG_502_Project_Calcs.mlx) - All imshow functions commented out so the code can run.
The output of the for loop gives me the correct values when d and h are subbed into the equation using the "subs" function; however, the output gives the "sigma_x_case1_it1" variable 10 times and when I try to call it, it outputs the last iteration which indicates that it is writing over it (See Attached - Output.png). How do I get the for loop to output a 10x27 table or array that I can eventually write to Excel? Do I need to have the for loop output sigma_x_case1_it1 as 27 seperate variable then combine somehow in a matrix or table? If so, how do I do that or is there any easier way to solve this.
Any help would be appreciated!
Marshall
2 件のコメント
採用された回答
Peter Perkins
2023 年 12 月 4 日
Marshall, the advice to try to vectorize is good advice, but if that's not possible, you might try something like at this:
height_range_data = linspace(2,3,10),
diameter_range_data = [0.405,0.54,0.675,0.84,1.05,1.315,1.66,1.9,2.375,2.8753,5,4,4.5,5,5.563,6.625,8.625,10.75,12.75,14,16,18,20,24,30,36,48],
[h,d] = ndgrid(height_range_data,diameter_range_data);
h = h(:); d = d(:);
t = table(h,d)
t2 = [t rowfun(@(h,d) h/d,t,OutputVariableName="Fun_hd")]
You asked for a 10x27 array. You can easily turn this output into that, but depending on what you are doing after this, the above might (or might not) be a more convenient form.
その他の回答 (1 件)
Sulaymon Eshkabilov
2023 年 12 月 3 日
It can be computed using vectorization instead of loop - see this:
sigma_x_case1 = @(d, h)122.23*d.*(814.735*d+167.7312*h+335.4624) ./((d-1).^4-d.^4);
height_range_data = linspace(2, 3, 27);
h=height_range_data;
diameter_range_data = [0.405, 0.54, 0.675, 0.84, 1.05, 1.315, 1.66, 1.9 ,2.375, 2.8753, 5, 4, 4.5, 5, 5.563, 6.625, 8.625, 10.75, 12.75, 14, 16, 18, 20, 24, 30, 36, 48];
d = diameter_range_data;
[D, H] = meshgrid(d, h);
SS = sigma_x_case1(D,H);
surfc(D,H, SS)
xlabel('d')
ylabel('h')
zlabel('sigma_x_case1(d,h)')
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!