How to output a for to a 10x27 table (for loop keeps overwriting the variable)
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Marshall Thompson
am 3 Dez. 2023
Kommentiert: Marshall Thompson
am 6 Dez. 2023
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 Kommentare
Akzeptierte Antwort
Peter Perkins
am 4 Dez. 2023
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.
2 Kommentare
Weitere Antworten (1)
Sulaymon Eshkabilov
am 3 Dez. 2023
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)')
1 Kommentar
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!