3. The divide and average method, an old-time method for approximating the squar
ID: 664291 • Letter: 3
Question
3. The divide and average method, an old-time method for approximating the square root of any positive number a, can be formulated as x a/x Write a MATLAB m function to implement this algorithm. Use proper indentation so that the structure is clear. At each step estimate the error in your approximation as new Xold new Repeat the loop until E is less than or equal to a specified value. Design your program so that it returns both the result and the error. Test your program by evaluating a 0, 2, 4 for E 1*10Explanation / Answer
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
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.