Error with square root function and powers in Matlab R2014b [closed]
Asked Answered
E

1

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

Esquiline answered 13/2, 2015 at 10:49 Comment(4)
input is undefined. Do you mean input2? And what is your colleague's result? Show a minimal, complete, verifiable example!Storytelling
Yes sorry it was a typo, fixed now.Esquiline
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?Courante
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).Euboea
M
-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).

Mendiola answered 13/2, 2015 at 10:56 Comment(4)
Many thanks for the help. Seems to have corrected the problem.Esquiline
@CharlotteTaylor Would you be so kind as to post a minimal example where computing the powers inside and outside sqrt gives different results?Storytelling
@LuisMendo I was going to ask about the same thingCarthy
@LuisMendo @Carthy 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.Spinks

© 2022 - 2024 — McMap. All rights reserved.