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
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 CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by