Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: College student needing help with a function....
Replies: 2   Last Post: Sep 3, 2013 6:54 PM

 Messages: [ Previous | Next ]
 Hunter Bray Posts: 2 Registered: 8/29/13
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

Date Subject Author
8/29/13 Hunter Bray
8/30/13 John D'Errico
9/3/13 Hunter Bray