-1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 4
    `input` is undefined. Do you mean `input2`? And what is your colleague's result? Show a [minimal, complete, verifiable example](http://stackoverflow.com/help/mcve)! – Luis Mendo Feb 13 '15 at 10:50
  • Yes sorry it was a typo, fixed now. – Charlotte Taylor Feb 13 '15 at 10:51
  • 3
    What result do you and your colleagues get? Are you sure none of the variables/functions have been redefined? In particular, is `sqrt` really MATLAB's built-in function for computing the square root? – jub0bs Feb 13 '15 at 10:53
  • I tested it and me and my colleague get exactly the same result, so it doesn't seem to depend on the colleague. :) (Sorry, just a joke). – NoDataDumpNoContribution Feb 18 '15 at 14:25

1 Answers1

-1

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).

Adam893
  • 143
  • 1
  • 3
  • 16
  • Many thanks for the help. Seems to have corrected the problem. – Charlotte Taylor Feb 13 '15 at 11:08
  • 6
    @CharlotteTaylor Would you be so kind as to post a minimal example where computing the powers inside and outside `sqrt` gives different results? – Luis Mendo Feb 13 '15 at 11:09
  • @LuisMendo I was going to ask about the same thing – roni Feb 13 '15 at 11:13
  • 2
    @LuisMendo @roni it sounds like an MWE is not really possible here as the behaviour is supposedly different for different machines. My guess is the `sqrt` function has been overwritten on OP's machine. If this is however a 'real' issue it sounds quite serious as it is not at all the sort of behaviour you would expect from the `sqrt` / power function. I've got to say this accepted answer sounds more like dealing with the symptom than the underlying cause, whatever it may be. – Wouter Kuijsters Feb 13 '15 at 13:25