How to output a for to a 10x27 table (for loop keeps overwriting the variable)

1 vue (au cours des 30 derniers jours)
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
Unrecognized function or variable 'sigma_x_case1'.
*** 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 commentaires
Marshall Thompson
Marshall Thompson le 3 Déc 2023
sigma_x_case1 is an equation in terms of d and h that was solved with many other variables.

Connectez-vous pour commenter.

Réponse acceptée

Peter Perkins
Peter Perkins le 4 Déc 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),
height_range_data = 1×10
2.0000 2.1111 2.2222 2.3333 2.4444 2.5556 2.6667 2.7778 2.8889 3.0000
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],
diameter_range_data = 1×27
0.4050 0.5400 0.6750 0.8400 1.0500 1.3150 1.6600 1.9000 2.3750 2.8753 5.0000 4.0000 4.5000 5.0000 5.5630 6.6250 8.6250 10.7500 12.7500 14.0000 16.0000 18.0000 20.0000 24.0000 30.0000 36.0000 48.0000
[h,d] = ndgrid(height_range_data,diameter_range_data);
h = h(:); d = d(:);
t = table(h,d)
t = 270×2 table
h d ______ _____ 2 0.405 2.1111 0.405 2.2222 0.405 2.3333 0.405 2.4444 0.405 2.5556 0.405 2.6667 0.405 2.7778 0.405 2.8889 0.405 3 0.405 2 0.54 2.1111 0.54 2.2222 0.54 2.3333 0.54 2.4444 0.54 2.5556 0.54
t2 = [t rowfun(@(h,d) h/d,t,OutputVariableName="Fun_hd")]
t2 = 270×3 table
h d Fun_hd ______ _____ ______ 2 0.405 4.9383 2.1111 0.405 5.2126 2.2222 0.405 5.487 2.3333 0.405 5.7613 2.4444 0.405 6.0357 2.5556 0.405 6.31 2.6667 0.405 6.5844 2.7778 0.405 6.8587 2.8889 0.405 7.1331 3 0.405 7.4074 2 0.54 3.7037 2.1111 0.54 3.9095 2.2222 0.54 4.1152 2.3333 0.54 4.321 2.4444 0.54 4.5267 2.5556 0.54 4.7325
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 commentaires
Marshall Thompson
Marshall Thompson le 6 Déc 2023
Thank you!
The output gives me a 270x3 table which is perfect for my analysis since that would be easiler to analyze the"Diameter Range" visually once I get the data to excel!
I am having trouble writing the table to excel...I am currently using these line of code to output the table and write to excel:
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);
sigma_x_iteration1 = [t rowfun(@(h,d) subs(sigma_x_case1),t,OutputVariableName="sigma_x_case1_it1")];
writetable(sigma_x_iteration1,excel_filename,"Sheet","Sigma_X (My) - IT 1");
The bolded line gives me an output table as shown in (Excel Table Output.png) where the sigma_x_case1_it1 values won't write to excel to Column C. I am confused since it is showing up in matlab as a 270x3 table so it should write the 3 coloumn of the table correctly but it seems like it is having issues with the values that are outputted from the subs(sigma_x_case1)? I've tried using "Range" in the writetable function as well as converting the 3rd column to an array using sigma_x_iteration{:,3} then back to table using array2table, then trying to writetable again but no luck.
Thanks!
Marshall Thompson
Marshall Thompson le 6 Déc 2023
Disregard!
double(subs(sigma_x_case1)) converted the 1x1 syms in the sigma_x_iteration1 output variable and I was able to write that to excel.
Thank you so much for the help @Peter Perkins
Marshall

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 3 Déc 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 commentaire
Marshall Thompson
Marshall Thompson le 3 Déc 2023
Modifié(e) : Marshall Thompson le 3 Déc 2023
sigma_x_case1 is a equation based on many other variables so I can’t break it out and hard code the terms like you did since I will possibily need to change the input variables for futher analysis. Is there anyway I can apply the (d, h) to my sigma_x_case1 equation variable as a function and it will result in the same response?
Please see the attached file (Question_Thompson_Marshall_AENG_502_Project_Calcs.mlx) for all of the code.
Thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by