How to output a for to a 10x27 table (for loop keeps overwriting the variable)
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
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
Marshall Thompson
am 3 Dez. 2023
Bearbeitet: Marshall Thompson
am 3 Dez. 2023
Kategorien
Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
