Pls help I hate loops

When you post code for other people to review, you should make sure that you use descriptive variable names and include comments in the code.

I see that your h variable is height and ranges from [0,4.625] incremented by 1. Your r variable is radius which is computed based on height. You want to determine at which heights correspond to 3, 8, and 14.5 oz of volume.

You decide to use the trapz(X, Y) function to attempt to calculate the volume. I am not familiar with the function, so I will assume the syntax you are using calculates the volume at a particular height.

You then include a redundant while loop which tries to do the exact same thing your for loop is doing.

Your while loop is never entered because voltrap never equals 3. Your voltrap ~= 3 expression checks that it is exactly equal to 3. Rather than saying voltrap > 3. Or 2.9<voltrap<3.1

/r/matlab Thread Parent