# while loops , error in use of the word else and else

1 view (last 30 days)
Eti Rastogi on 14 Sep 2020
Commented: Rik on 14 Sep 2020
Length_cylindrical_section = (total_tank_length - (2*tank_radius)); % finding length of cylindrical section
% using variables A, B and C to calculate the total tank volume
A = ((pi*(tank_diameter)^2) *((3*tank_radius) - tank_diameter)/3);
tank_volume_max = A+Length_cylindrical_section*(B-C);
% using fluid heights to find total volume tolerance
v_tol = (tank_volume_max * (fluid_height1)) - (tank_volume_max * (fluid_height2));
fluid_height = 0
A = ((pi*(fluid_height)^2) *((3*tank_radius) - fluid_height)/3);
fluid_volume = A+Length_cylindrical_section*(B-C);
v_tol = (fluid_volume * (fluid_height1)) - (fluid_volume * (fluid_height2));
safe_tank_volume = safety_percent*tank_volume_max
index_count = 1;
while fluid_volume <= (tank_volume_max - v_tol )
Fluid_height = fluid_height + fluid_height_increment;
index_count = index_count+1;
fprintf('The final fluid volume is %.2f m^3. /n' , fluid_volume);
end
plot (fluid_volume ,fluid_height);
xlabel = 'Fluid Height [m]';
ylabel = 'Fluid Volume [m^3]' ;
title = ' Fluid Volume vs Fluid Height';
%% ____________________
%% FORMATTED TEXT & FIGURE DISPLAY
fprintf(' Length of Cylindrical section is %.2f m /n' ,Length_cylindrical_section);
fprintf(' Tank volume is %.2f m^3 /n' ,tank_volume);
fprintf(' Total volume tolerance is %.2f m^3 /n' ,v_tol);
Eti Rastogi on 14 Sep 2020
i am trying to convert the flowchart into a mathlab script but it is running indefinately and doesnt account for all the changes in the height.
Rik on 14 Sep 2020
Is this a duplicate of your other question? Please edit this one and close that one, or explain what is the difference.

Jon on 14 Sep 2020
At least one problem you have is in the loop:
while fluid_volume <= (tank_volume_max - v_tol )
Fluid_height = fluid_height + fluid_height_increment;
index_count = index_count+1;
fprintf('The final fluid volume is %.2f m^3. /n' , fluid_volume);
end
The termination for this loop is based upon the fluid volume being greater than or equal to the allowable tank volume, but note that you never update the fluid volume inside of the loop. You change the fluid height, but then don't do anything with that value. So since the calculated fluid volume doesn't change the loop runs forever.