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

1 回表示 (過去 30 日間)
Hamza Aboumaray
Hamza Aboumaray 2020 年 5 月 17 日
コメント済み: Hamza Aboumaray 2020 年 5 月 17 日
% 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 件のコメント
KSSV
KSSV 2020 年 5 月 17 日
double(depth)*1000
Hamza Aboumaray
Hamza Aboumaray 2020 年 5 月 17 日
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?

サインインしてコメントする。

採用された回答

Thiago Henrique Gomes Lobato
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
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
Hamza Aboumaray
Hamza Aboumaray 2020 年 5 月 17 日
Thank you very much! Very much appreciated!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by