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

1 view (last 30 days)
% 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;
answer = vpasolve(eqn, h)
depth = r - answer % depth of water in the trough
answer =
0.041305887729811791004451374702525
depth =
0.25869411227018820899554862529748
  4 Comments
Hamza Aboumaray
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?

Sign in to comment.

Accepted Answer

Thiago Henrique Gomes Lobato
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
  5 Comments
Thiago Henrique Gomes Lobato
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;
answer = vpasolve(eqn, h)
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
answer =
0.041305887729811791004451374702525
depth =
0.258694112270188
depthRounded =
0.258000000000000

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!