Search code examples
matlabindicessquare-root

Error with square root function and powers in Matlab R2014b


I am carrying out a series of calculations using the sqrt() function. My colleagues and I have noticed that we get different results when using the same inputs. Has anyone encountered this problem before?

This is an example:

input1 = 4;
input2 = 8;

result = sqrt(input1^2 + input2^2)

Result then displays a different value from my colleagues result. We have contacted MathWorks about this issue and have yet to receive a reply.


Solution

  • My team and I came across the same problem a year or two ago.

    MathWorks explained that the sqrt() function has an issue with powers when they are added. To overcome this issue and achieve the same result, square each term outside of the sqrt() function:

    input1 = 4^2;
    input2 = 8^2;
    
    result = sqrt(input1 + input2)
    

    This solved it for my team and I. MathWorks didn't clarify as to the reason for the issue, but told us they were in the process of updating their documentation (haven't seen anything as of yet).