Putting for loop values into a matrix/array for plotting
2 views (last 30 days)
Show older comments
Daniel Galaczi
on 20 Jul 2021
Commented: Daniel Galaczi
on 23 Jul 2021
I am trying to find the optimum length of a bridge with regards to cost. I'm trying to put the values from each loop into an array/matrix so I can then find the correspoding road and bridge lengths for the lowest cost on each cost ratio, however the answers from each loop are "seperate" from all other loops and I need them in one array. Does anyone know if this is possible, and if so how I would be able to do this I'm struggling to figure out how it can be done. Thanks.
Ly = 8.7 %total length ravine
Lx = 1.1 %width of ravine
Cs = 1e6 %cost of road per km
for r = 1.1:0.5:5 %varying ratio between road cost per km and bridge cost per km
Ls = 0.1:0.1:8.6; %varying length of road
Lb = sqrt((Ly-Ls).^2 + Lx.^2); %equation to find length of bridge from the length of road
C =((r*Cs)*(Lb))+(Cs*Ls); %equation for total cost of bridge and road
min(C) %minimum value of C for each ratio loop
end
0 Comments
Accepted Answer
Kevin Shi
on 21 Jul 2021
Hi Daniel,
It seems you are trying to collect the result of each iteration in to one array.
If this is the case, you can initialize a double array before the for loop, then append the result of each iteration to the end of the array, below is an example(I just assume that you want collect minium cost, you can change is to whatever you want later);
Ly = 8.7; %total length ravine
Lx = 1.1; %width of ravine
Cs = 1e6; %cost of road per km
result = [];
for r = 1.1:0.5:5 %varying ratio between road cost per km and bridge cost per km
Ls = 0.1:0.1:8.6; %varying length of road
Lb = sqrt((Ly-Ls).^2 + Lx.^2); %equation to find length of bridge from the length of road
C =((r*Cs)*(Lb))+(Cs*Ls); %equation for total cost of bridge and road
result(end + 1) = min(C) %minimum value of C for each ratio loop
end
Hope this is helpful.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!