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

1 次查看(过去 30 天)
tank_radius = tank_diameter/2; % finding tank radius
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);
B = ((tank_radius)^2) * (acos((tank_radius -tank_diameter)/tank_radius));
C = (tank_radius) - (tank_diameter)*(sqrt((2*tank_radius*tank_diameter)- (tank_diameter)^2));
tank_volume_max = A+Length_cylindrical_section*(B-C);
% using fluid heights to find total volume tolerance
fluid_height1 = tank_radius + (0.5*fluid_height_increment);
fluid_height2 = tank_radius - (0.5*fluid_height_increment);
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);
B = ((tank_radius)^2) * (acos((tank_radius - fluid_height)/tank_radius));
C = (tank_radius) - (fluid_height)*(sqrt((2*tank_radius*fluid_height)- (fluid_height)^2));
fluid_volume = A+Length_cylindrical_section*(B-C);
fluid_height1 = tank_radius + (0.5*fluid_height_increment);
fluid_height2 = tank_radius - (0.5*fluid_height_increment);
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(' Tank Radius is %.2f m /n' , tank_radius);
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);
  2 个评论
Eti Rastogi
Eti Rastogi 2020-9-14
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
Rik 2020-9-14
Is this a duplicate of your other question? Please edit this one and close that one, or explain what is the difference.

请先登录,再进行评论。

回答(1 个)

Jon
Jon 2020-9-14
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.

类别

Help CenterFile Exchange 中查找有关 Fluid Mechanics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by