Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sum array display a wrong result

Subject: sum array display a wrong result

From: John Wong

Date: 21 Jul, 2011 04:48:07

Message: 1 of 7

Here is the code for linear regression calculation (some introductory stuff... not homework at all.. I use this to calculate things):

http://codepad.org/ywvHR7OE

On line 60, I summed the array res, which gives :


sum_res =

  2.2737e-013

This number is wrong. I did the calculation by hand, and I got 0.7564.
The previous number was too small. Using format long I can see that 0.7564 gives +0.00001 of an increment.

Can anyone help me on this?

Thanks.

Subject: sum array display a wrong result

From: Bruno Luong

Date: 21 Jul, 2011 07:56:09

Message: 2 of 7

"John Wong" wrote in message <j08b27$i4u$1@newscl01ah.mathworks.com>...
> Here is the code for linear regression calculation (some introductory stuff... not homework at all.. I use this to calculate things):
>
> http://codepad.org/ywvHR7OE
>
> On line 60, I summed the array res, which gives :
>
>
> sum_res =
>
> 2.2737e-013
>
> This number is wrong. I did the calculation by hand, and I got 0.7564.

Unlike you, I'm pretty sure the hand calculation is wrong or inaccurate.

Bruno

Subject: sum array display a wrong result

From: John Wong

Date: 25 Jul, 2011 00:04:08

Message: 3 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <j08m2p$ec1$1@newscl01ah.mathworks.com>...
> "John Wong" wrote in message <j08b27$i4u$1@newscl01ah.mathworks.com>...
> > Here is the code for linear regression calculation (some introductory stuff... not homework at all.. I use this to calculate things):
> >
> > http://codepad.org/ywvHR7OE
> >
> > On line 60, I summed the array res, which gives :
> >
> >
> > sum_res =
> >
> > 2.2737e-013
> >
> > This number is wrong. I did the calculation by hand, and I got 0.7564.
>
> Unlike you, I'm pretty sure the hand calculation is wrong or inaccurate.
>
> Bruno

Yes, you are right. The hand calculation should indeed be 0.0613.
However, MATLAB is still giving me 2.2737e-013

res =

   -6.7271 8.2729 -4.7793 -16.1821 13.8179 0.8179 34.7136 -26.6892 -13.7935 -0.8456 21.1544 -23.3006 -14.7035 28.2444

>> sum(res)

ans =

  2.2737e-013

Thanks, Bruno.

Subject: sum array display a wrong result

From: Roger Stafford

Date: 25 Jul, 2011 02:27:10

Message: 4 of 7

"John Wong" wrote in message <j0ibtn$k6g$1@newscl01ah.mathworks.com>...
> Yes, you are right. The hand calculation should indeed be 0.0613.
> However, MATLAB is still giving me 2.2737e-013
>
> res =
> -6.7271 8.2729 -4.7793 -16.1821 13.8179 0.8179 34.7136 -26.6892 -13.7935 -0.8456 21.1544 -23.3006 -14.7035 28.2444
>
> >> sum(res)
> ans =
> 2.2737e-013
- - - - - - - - - -
  I believe your hand calculation is still in error, John. The sum of the numbers you display should be exactly 0.0002, but that is not incompatible with matlab's answer of 2.2737e-013. It just means that you and matlab are adding different numbers. Those you see in that display have all been rounded off to four-decimal place accuracy and the sum can therefore be expected to lie in that fourth decimal place, as it is in the 0.0002 answer. Matlab is computing with the equivalent of about fifteen or so decimal places, but round-off errors in the regression computation have given you a sum equal to 2.2737e-013 rather than an ideal zero.

  If in the future you want to do hand calculations to check on your results, you should always display those results to an appropriate accuracy such as is given by "format long" - or better still use 'fprintf' techniques to get the display accuracy as high as you need for checking purposes.

Roger Stafford

Subject: sum array display a wrong result

From: John Wong

Date: 25 Jul, 2011 03:45:09

Message: 5 of 7

"Roger Stafford" wrote in message <j0ik9u$8as$1@newscl01ah.mathworks.com>...
> "John Wong" wrote in message <j0ibtn$k6g$1@newscl01ah.mathworks.com>...
> > Yes, you are right. The hand calculation should indeed be 0.0613.
> > However, MATLAB is still giving me 2.2737e-013
> >
> > res =
> > -6.7271 8.2729 -4.7793 -16.1821 13.8179 0.8179 34.7136 -26.6892 -13.7935 -0.8456 21.1544 -23.3006 -14.7035 28.2444
> >
> > >> sum(res)
> > ans =
> > 2.2737e-013
> - - - - - - - - - -
> I believe your hand calculation is still in error, John. The sum of the numbers you display should be exactly 0.0002, but that is not incompatible with matlab's answer of 2.2737e-013. It just means that you and matlab are adding different numbers. Those you see in that display have all been rounded off to four-decimal place accuracy and the sum can therefore be expected to lie in that fourth decimal place, as it is in the 0.0002 answer. Matlab is computing with the equivalent of about fifteen or so decimal places, but round-off errors in the regression computation have given you a sum equal to 2.2737e-013 rather than an ideal zero.
>
> If in the future you want to do hand calculations to check on your results, you should always display those results to an appropriate accuracy such as is given by "format long" - or better still use 'fprintf' techniques to get the display accuracy as high as you need for checking purposes.
>
> Roger Stafford

Hi Roger, thank you very much.

I punched into the IT-83 and yes, 2e-4. You are right!!

>>but round-off errors in the regression computation have given you a sum equal to 2.2737e-013 rather than an ideal zero.

I don't get this part. How can I avoid this issue when MATLAB has already done the internal round-off?

fprintf('Output: %f\n',sum_res)
fprintf('Output: %.15f\n',sum_res)



Thanks, Roger!

Subject: sum array display a wrong result

From: Greg Heath

Date: 26 Jul, 2011 10:28:29

Message: 6 of 7

On Jul 24, 11:45 pm, "John Wong" <gokoproj...@gmail.com> wrote:
> "Roger Stafford" wrote in message <j0ik9u$8a...@newscl01ah.mathworks.com>...
> > "John Wong" wrote in message <j0ibtn$k6...@newscl01ah.mathworks.com>...
> > > Yes, you are right. The hand calculation should indeed be 0.0613.
> > > However, MATLAB is still giving me 2.2737e-013
>
> > > res =
> > >    -6.7271    8.2729   -4.7793  -16.1821   13.8179    0.8179   34.7136  -26.6892  -13.7935   -0.8456   21.1544  -23.3006  -14.7035   28.2444
>
> > > >> sum(res)
> > > ans =
> > >   2.2737e-013
> > - - - - - - - - - -
> >   I believe your hand calculation is still in error, John.  The sum of the numbers you display should be exactly 0.0002, but that is not incompatible with matlab's answer of 2.2737e-013.  It just means that you and matlab are adding different numbers.  Those you see in that display have all been rounded off to four-decimal place accuracy and the sum can therefore be expected to lie in that fourth decimal place, as it is in the 0.0002 answer.  Matlab is computing with the equivalent of about fifteen or so decimal places, but round-off errors in the regression computation have given you a sum equal to 2.2737e-013 rather than an ideal zero.
>
> >   If in the future you want to do hand calculations to check on your results, you should always display those results to an appropriate accuracy such as is given by "format long" - or better still use 'fprintf' techniques to get the display accuracy as high as you need for checking purposes.
>
> > Roger Stafford
>
> Hi Roger, thank you very much.
>
> I punched into the IT-83 and yes, 2e-4. You are right!!
>
> >>but round-off errors in the regression computation have given you a sum equal to 2.2737e-013 rather than an ideal zero.
>
> I don't get this part. How can I avoid this issue when MATLAB has already done the internal round-off?
>
> fprintf('Output: %f\n',sum_res)
> fprintf('Output: %.15f\n',sum_res)
>
> Thanks, Roger!- Hide quoted text -
>
> - Show quoted text -

help format
doc format
format long; res
format long e; res
format long g; res

Hope this helps.

Greg

Subject: sum array display a wrong result

From: Roger Stafford

Date: 26 Jul, 2011 15:38:09

Message: 7 of 7

"John Wong" wrote in message <j0ios5$hu2$1@newscl01ah.mathworks.com>...
> >>but round-off errors in the regression computation have given you a sum equal to 2.2737e-013 rather than an ideal zero.
> I don't get this part. How can I avoid this issue when MATLAB has already done the internal round-off?
- - - - - - - - - - -
  I'm not sure what it is that puzzles you, John.

  The formula for linear regression is designed so that the sum of the differences between your data points and the line of regression should add up to zero. In the ideal world of precise mathematics this sum would indeed be exactly zero. However in the process of performing the arithmetic operations to realize this line of regression, a computer with only a finite number of bits must necessarily suffer round off errors while performing this arithmetic, so that this sum will no longer be precisely zero. In your case it apparently made an accumulated error of 2.2737e-013 in all the steps that led up to that final summation. I would say that this is rather good considering the number of additions, multiplications, and divisions that were necessary to achieve your results. After all, your computer is using only 53 bit accuracy in its calculations which is about one part in 10^15.

  However, it may possible to improve matters a little if you use a more robust method of calculation than you have shown in the website you referenced. In that code you did this in effect:

 Sxy = sum(x.*y) - sum(x)*sum(y)/n
 Sxx = sum(x.^2) - sum(x)^2/n

It is better to do things this mathematically equivalent way:

 Sxy = sum((x-sum(x)/n).*(y-sum(y)/n));
 Sxx = sum((x-sum(x)/n).^2);

That is, subtract mean values before you take the products or squares. From a numerical analysis point of view the errors generated tend to be smaller this way, particularly if the values of x and y are strongly biased away from zero, as in your case.

  Aside from such techniques, there is nothing you can do about such errors as these except to perform arithmetic with a higher accuracy. That is possible with matlab's symbolic toolbox but only at the cost of very much slower computation times.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us