# How do I round off the answer for the depth to within 2mm?

2 views (last 30 days)
Hamza Aboumaray on 17 May 2020
Commented: Hamza Aboumaray on 17 May 2020
% Values of constants
syms h
r = 0.3;
V = 0.35;
L = 3;
eqn = (0.5*pi*r^2-r^2*asin(h/r)-h*sqrt(r^2-h^2))*L == V;
depth = r - answer % depth of water in the trough
0.041305887729811791004451374702525
depth =
0.25869411227018820899554862529748

Show 1 older comment
Hamza Aboumaray on 17 May 2020
metres
KSSV on 17 May 2020
double(depth)*1000
Hamza Aboumaray on 17 May 2020
Thanks! Just want to ask one question, what it does mean when they ask to get the answer to within 2 mm? Does that mean to 3 decimal places since 2 mm is 0.002 metres?

If you want to round to a specific unit (m, mm, etc), you can do this by just adding an extra parameter to the round function with the number of commas you want to round. If, however, you want to round in steps of 2 mm , it gets a little bit trickier although also not so difficult. The idea is to verify how many groups of 2 mm you have, round it and then add to the round version of your data without rounding mm. Something like this:
a = 2.355123; % m
Nonzeromm = (a-round(a,2))*1000; % Find how many mm there is in the value
Nof2mm = round(Nonzeromm/2); % Count how many multiple of 2 are there
aRounded = round(a,2)+Nof2mm/1000*2 % Calculate the round without mm and then ad the ones you finded before
aRounded =
2.3560

Hamza Aboumaray on 17 May 2020
I copied your code but I get an error saying: Unrecognized function or variable 'Non'. Can you please fix this?
There's no "Non" variable in my code, you probably didn't copied entirely or made some extra modification. Mixing both codes the exact code you should have is this one:
syms h
r = 0.3;
V = 0.35;
L = 3;
eqn = (0.5*pi*r^2-r^2*asin(h/r)-h*sqrt(r^2-h^2))*L == V;
depth = double(r - answer) % depth of water in the trough
Nonzeromm = (depth-round(depth,2))*1000; % Find how many mm there is in the value
Nof2mm = round(Nonzeromm/2); % Count how many multiple of 2 are there
depthRounded = round(depth,2)+Nof2mm/1000*2 % Calculate the round without mm and then ad the ones you finded before
0.041305887729811791004451374702525
depth =
0.258694112270188
depthRounded =
0.258000000000000
Hamza Aboumaray on 17 May 2020
Thank you very much! Very much appreciated!