Why matlab make this calculation error?

for this code i wrote for my home work, when i was running, SOME of the output from line 17 is not correct. Can any one explain to me why this is not correct? (I use number: 38 3,-8.9,4.1 as 4 sample input)
T = input('please enter the force: '); %%1
x = input('please enter the x coordinate: '); %%2
y = input('please enter the y coordinate: '); %%3
z = input('please enter the z coordinate: '); %%4
fprintf('\n');%%5
fprintf('\n'); %%6
fprintf(' x = %4.2f , y = %4.2f, z= %4.2f', x,y,z);%%7
fprintf('\n'); %%7
fprintf('\n'); %%8
R = sqrt(x^2+y^2+z^2); %%9
norm_x = x/R; %%10
norm_y = y/R;%%11
norm_z = z/R;%%12
fprintf('||dist|| = %4.2f',R); %%13
fprintf('\n'); %%14
fprintf(' norm= %4.3f i+%4.3fj+ %4.3f k', norm_x,norm_y,norm_z); %%15
fprintf('\n'); %%16
fprintf('T = %4.3f i + %4.3f j + %4.3f k',T*norm_x, T * norm_y, norm_z*T);%%17%%The first two output has errors.
fprintf('\n');%%18
The incorrect output is shown here, it should be 11.134, -32.984, 15.2
T = 11.124 i + -33.002 j + 15.203 k

2 comentarios

Star Strider
Star Strider el 24 de Feb. de 2016
Are you sure your ‘correct’ values are in fact correct?
I see no obvious errors, and if the norm values in Line 15 are correct, and ‘T’ is correctly entered, Line 17 should be correct.
BEICHEN LIU
BEICHEN LIU el 24 de Feb. de 2016
Either the line 15 output is correct or not, the output in this pic(line 17) is not all correct(first two incorrect, and last one correct).

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Feb. de 2016

0 votos

No, the output is correct and it is your expected output that is wrong. In particular, when you calculated the norm components, you rounded to three decimal places before doing the multiplication by the force, and that gave you your "expected" answer; when you do not round to three decimal places you get the actual answer the code shows.

4 comentarios

BEICHEN LIU
BEICHEN LIU el 24 de Feb. de 2016
Here is the results from the data i gave above, but there is no way i could get 38*0.293 =11.124, and 38*-0.868 =-33.002. The third one is correct. I have no idea why this could happen
Stephen23
Stephen23 el 24 de Feb. de 2016
Editada: Stephen23 el 24 de Feb. de 2016
@BEICHEN LIU: MATLAB is not making any mistakes, although your title accuses it of this. In fact you are doing two different calculations which, not surprisingly, give two different results:
>> T*0.293
ans = 11.134
>> T*norm_x
ans = 11.124
You are confusing the rounded printed value of norm_x for the actual stored numeric value of norm_x. These are, however, clearly not the same, no matter how much you imagine them to be.
Roger Stafford
Roger Stafford el 24 de Feb. de 2016
@Beichen Liu: Please read Walter's explanation more carefully! Multiplying 38 by .293 is incorrect, because the actual value of norm_x is .292742143, and when you multiply that by 38, you get 11.12420143 which rounds to 11.124, not 11.134. You were misled by your own intermediate rounding displays using 'fprintf'.
Steven Lord
Steven Lord el 24 de Feb. de 2016
You can easily check to convince yourself that Walter and Stephen are correct. Display (using DISP, not using FPRINTF) the value 0.293-norm_x. That value will be nonzero. [It's probably going to be somewhere in the vicinity of 2.6e-4 based on T having a value of 38.]

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 24 de Feb. de 2016

Comentada:

el 24 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by