Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



College student needing help with a function....
Posted:
Aug 29, 2013 11:52 PM


Hey guys I'm working on a matlab problem for school and I had a couple questions about this function I wrote. It's supposed to use the "divide and average" method to find the square root of a number whether it is positive, negative, or zero and display it with the error. Is there a more correct way to get the answer for the negative square root than just multiplying by 1i in the output rtanderr? Also, this method gives the correct answer when I run it but the format of the answer is weird. This is what it gives me:
rtanderr4 = 0 + 2.000000000000000i 0
And here is my function:
function rtanderr = sroot(a) x = abs(a)/2; error = 1000; if a > 0 while error >= 0.0001 xnew =((x+(a/x))/2); error = abs((xnew  x)/xnew); if error > 0.0001 x = xnew; else rtanderr = [x,error]; end end elseif a == 0 rtanderr = [0,0]; else while error > 0.0001 xnew =((x+(abs(a)/x))/2); error = abs((xnew  x)/xnew); if error > 0.0001 x = xnew; else rtanderr = [x*1i,error]; end end end
I appreciate any help that can be given.
Thanks, Hunter



