The “divide and average” method, an old-timemethod for approximating the square
ID: 1829461 • Letter: T
Question
The “divide and average” method, an old-timemethod forapproximating the square root of any positive number a can be
formulated as
(a) Write a well-structured pseudocode to implement this
algorithm. Use proper indentation so that the structure is
clear.
(b) Develop, debug, and document a Matlab function to
implement this equation.
P.S. I really need help with this, I will aprecciate a lot Iwill rate highest!!!
Explanation / Answer
(a) Pseudo code for the above algorithm is shown below. SQR_ROOT(A,iter) // A = input positive integer , iter= number of iterations 1. x = 1.0 ; // Initialise x (= sqrt(A) ) to 1 2 . for i = 1 to iter 3. x =0.5 * ( A/x + x) 4. end 5. return x In above code , we have initialised x to 1 and not to 0 in order toavoid "Divide by Zero " error (since we have A/x term). (b) .. MATLAB Code for above algorithm. % sqr_root.m function root =sqr_root(A,iter) root =1; if ( A> 0) for i = 1: iter root = 0.5*(A/root + root); end else root = -1 ; % If A < 0 , no real root , return -1 end end Save the above code as sqr_root.m (same as the function namei.e. sqr_root) Now to test it , create "test.m" in the same directory (the one withsqr_root.m) % test.m A = 2 ; % Anyinteger of choice. rootA =sqr_root(A,100) B = -3 ; rootB =sqr_root(B,100) Run the above code in MATLAB as test You should get the following output. rootA = 1.4142 rootB = -1Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.