-1

I am running a script in Matlab R2020b. The script contains an array with following values:

a=[500, 500, 500, 1000, 750, 750, 567.79 613.04]

The script as an equation:

(a(1)*(a(8)-a(6)) + a(7)*(a(6)-a(2))+ a(5)*(a(2)-a(8)))

When running on Matlab the above equation gives the answer -11312 for the values of array a. But when I calculate each value separately and add them the Matlab compiler gives a different answer.

a(1)*(a(8)-a(6)) = -68480
a(7)*(a(6)-a(2)) = 1.419e+05
a(5)*(a(2)-a(8)) = -84780
>>(-68480) + (1.419e+05) +(-84780) 

the answer for the above is -11310.

A screenshot of the commands is also attached.

screenshot of matlab commands

kindly tell me why Matlab compiler gives these different answers??

2 Answers2

2

The problem is that MATLAB's default format is 'short', and this is not showing you complete precision. Try format long.

>> format long
>> a(7)*(a(6)-a(2))
ans =
     1.419475000000000e+05
Edric
  • 23,676
  • 2
  • 38
  • 40
  • does format change the result of the operators too? Because in the first case when the result is calculated using a single equation the answer is **-11312** and in the second case when the values are calculated separately and then the addition is performed answer is **-11310**. – akash yadav Aug 31 '22 at 07:21
  • @akashyadav those numbers you report are not what MATLAB outputs. – Ander Biguri Aug 31 '22 at 09:09
2

You are wrong.

If you add format long g you can see the real numbers:

format long g

a=[500, 500, 500, 1000, 750, 750, 567.79 613.04]
res1=(a(1)*(a(8)-a(6)) + a(7)*(a(6)-a(2))+ a(5)*(a(2)-a(8)))
a2=a(7)*(a(6)-a(2))
a1=a(1)*(a(8)-a(6))
a3=a(5)*(a(2)-a(8))
res2=a1+a2+a3

results in:

res1 =

                  -11312.5


a2 =

                  141947.5


a1 =

                    -68480


a3 =

                    -84780


res2 =

                  -11312.5
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • Thanks for the coded answer. I am new to Matlab. Just want to know it is common knowledge to change the format in Matlab before computation or not. And is it required every time to set the format when we deal with the real numbers in Matlab – akash yadav Aug 31 '22 at 09:26
  • 3
    @akashyadav you are only changing the format of display, the math happening is exactly the same. Its just how MATLAB shows them to you – Ander Biguri Aug 31 '22 at 09:32
  • 1
    Note that in your original question you made another mistake: you did not take the full number of `a2`, as edric explained to you – Ander Biguri Aug 31 '22 at 09:37
  • after **format long g** option now the values of **res1** and **res2** are same. But for some time **res2** value is showing followed by whitespace. and for those values, **res1==res2** is **false** although the value of res1 and res2 is the same. – akash yadav Aug 31 '22 at 09:54
  • @akashyadav you may need to read this: https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab . That is how computers works. Floating point math is not associative: https://stackoverflow.com/questions/10371857/is-floating-point-addition-and-multiplication-associative – Ander Biguri Aug 31 '22 at 10:48