MATLAB REQUIRED TO CODE. PLEASE FOLLOW INSTRUCTIONS PLEASE FIND THE QUESTION (3.
ID: 3664760 • Letter: M
Question
MATLAB REQUIRED TO CODE. PLEASE FOLLOW INSTRUCTIONS
PLEASE FIND THE QUESTION (3.13) BELOW ALONG WITH THE INSTRUCTIONS:
ADDITIONAL MATERIAL FOR QUESTION:
PLEASE SOLVE WITH DETAIL. THANK YOU!
86 PROGRAMMING WITH MATLAB 3.13 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 2 Write a well-structuredM-file functionbased on the while...break loop structure to implement this algo- rithm. Use proper indentation so that the structure is clear. At each step estimate the error in your approximation as Tnew Xold Xnew 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. Make sure that it can evaluate the square root of numbers that are equal to and less than zero. For the latter case, display the result as an imaginary number. For exam ple, the square root of -4 would return 2i. Test your program by evaluating a 0, 2, 10 and-4 for -x 10Explanation / Answer
% 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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.