Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. (50 points) The divide and average method, an old-time method for approximating the square root of any positive number a, can be formulated as
1. (50 points) The "divide and average" method, an old-time method for approximating the square root of any positive number a, can be formulated as Write a well-structured M-file function based on the while...break loop structure to implement this algorithm. Use proper indentation so that the structure is clear. At each step estimate the error in your approximation as TnewXold new Repeat the loop until 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 example, the square root of-4 would return 2. Test your program by evaluating a-0, 2, 10, and 4 for1 10-4 Please use the following template to write your code: function s = squareRoot(a, eps) a is the number that you want to find the square root of 8 eps is the desired error that you want your error to be smaller than Fill in "YOUR CONDITIONS and "YOUR STATEMENTs" with your own lines ind-1; % if a is non-zero % if a is negative % make it positive 8 add j as an imaginary number indicator if YOUR CONDITION YOUR STATEMENT ind-ji end % Initial Value while (1) y = YOUR STATEMENT e = YOUR STATEMENT x = y; Infinite loop for the calculation % Do the divide and average Error calculation if YOUR CONDITION % check if the error is satisfied break end end Sxi else s=0; end s-s ind % multiply the square root with the indicator % (j for imaginary and 1 for real numbers) Verify your results with the followings: >> SquareRoot (0,1e-4) ans >> SquareRoot (2,1e-4) ans 1.4142 >SquareRoot (10,le-4) ans 3.1623 >> SquareRoot (-4,1e-4) 0+2.00001
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started