Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me write this 20-30 line code in matlab please? Square Root Algorithm Key programming concepts: variables, input, output, if, while loops Approximate

Can you help me write this 20-30 line code in matlab please?

Square Root Algorithm Key programming concepts: variables, input, output, if, while loops Approximate lines of code: 20 (does not include comments or white space) Commands you cant use: return, sqrt, pow, or any similar math command What to submit: A single .m le with your initials, underscore, and homework (e.g. ijh HW3.m) Autograder: Available 12:00 pm Wednesday Sept. 19 Program Input Enter any numeric value: User will always enter a numeric value, no error checking needed Enter an initial guess: User will always enter a reasonable numeric value, no error checking needed Program Outputs The square root of 0 is simply 0 Show this specic message if the user tries nd the square root of zero Algorithm completed in MMM iterations Replace MMM with the number of while loop iterations used to nd the square root The square root of XXX is YYY Replace XXX with the original value, YYY with the square root (including the imaginary i if needed) Let MATLAB choose the number of decimal places. Assignment Details We will implement the Babylonian Algorithm, which uses a single equation and a starting guess (xold) to calculate square roots: xnew = 1 2xold + S xold S is chosen by the user and is the value you want to nd the square root of. Note that S must always be positive so change the sign of S if given a negative value. xnew represents the algorithms updated prediction of the answer, and xold is the previous predicted answer. The algorithm requires an initial guess of xold which is also chosen by the user (the initial guess). The algorithm runs repeatedly in a while loop until the predicted answer matches the true square root, which is veried through an error equation: error = | xnew xnew S 1| Once the error equation becomes less than or equal to 0.000000001 (also written as 1e-9 in scientic notation), the algorithm is nished! Check out the following table to see a breakdown of the algorithm nding the square root of 125348:

Table 1: Given S = 125348 and xold = 600 Algorithm Iteration xold xnew = 1 2xold + S xold1 600 404.456666667 2 404.456666667 357.186837335 3 357.186837335 354.059011038 4 354.059011038 354.045195125 5 354.045195125 354.045194855 Sample Output The following test cases do not cover all possible scenarios (develop your own!) but should indicate if your code is on the right track: Test Case 1: Enter any numeric value: 25 Enter an initial guess: 6 Algorithm completed in 4 iterations The square root of 25 is 5 Test Case 2: Enter any numeric value: -9 Enter an initial guess: 2 Algorithm completed in 4 iterations The square root of -9 is 3i Test Case 3: Enter any numeric value: 45 Enter an initial guess: 12345 Algorithm completed in 15 iterations The square root of 45 is 6.7082 Test Case 4: Enter any numeric value: 0 The square root of 0 is simply 0 Test Case 5: Enter any numeric value: -1239.04 Enter an initial guess: 99.5 Algorithm completed in 5 iterations The square root of -1239.04 is 35.2i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions