Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.