How do I round off the answer for the depth to within 2mm?
1 次查看(过去 30 天)
显示 更早的评论
% 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 个评论
采纳的回答
Thiago Henrique Gomes Lobato
2020-5-17
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 个评论
Thiago Henrique Gomes Lobato
2020-5-17
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!