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

1 view (last 30 days)

Show older comments

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 Comments

Rik
on 14 Sep 2020

### Answers (1)

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.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!