Homework Solution: Applied Numerical Methods with MATLAB, 3rd edition, Steven C. Chapra…

    Applied Numerical Methods with MATLAB, 3rd edition, Steven C. Chapra chapra 3.13 & chapra 4.1 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 = x + a/x 2 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 ? = xnew ? xold xnew 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 2i. Test your program by evaluating a = 0, 2, 10 and ?4 for ? = 1 × 10?4. 4.1 The “divide and average” method, an old-time method for approximating the square root of any positive number a, can be formulated as x = x + a/x 2 Write a well-structured function to implement this algorithm based on the algorithm outlined in Fig. 4.2. Do not use the "while-break" structure Do not use the square root function, sqrt The sample code includes a table output. Use it to help you debug your code. You do not need to generate a table output. Take care of the main part of the problem first, then deal with the special cases Don't forget to include input validation, for example, the input must be scalar

    Expert Answer

    Applied Numerical Ways with MATLAB, 3rd edition, Steven C. Chapra

    chapra 3.13 & chapra 4.1

    3.13 The “divide and average” way, an antiquated way coercion approximating the balance parent of any dogmatical consider a, can be coercionmulated as x = x + a/x 2 Write a well-structured M-file exercise fixed on the while…break loop erection to appliance this algorithm. Correction just dispersion so that the erection is transparent. At each march consider the fallacy in your appropinquation as ? = xnew ? xold xnew Repeat the loop until ? is near than or resembling to a ascertained compute. Design your program so that it profits twain the end and the fallacy. Make safe that it can evaluate the balance parent of considers that are resembling to and near than referablehing. Coercion the death occurrence, ostentation the end as an unreal consider. Coercion development, the balance parent of ?4 would reappear 2i. Test your program by evaluating a = 0, 2, 10 and ?4 coercion ? = 1 × 10?4.

    4.1 The “divide and average” way, an antiquated way coercion approximating the balance parent of any dogmatical consider a, can be coercionmulated as x = x + a/x 2 Write a well-structured exercise to appliance this algorithm fixed on the algorithm outlined in Fig. 4.2.

    Do referable correction the “while-break” erection

    Do referable correction the balance parent exercise, sqrt

    The exemplification adjudication apprehends a board output. Correction it to succor you debug your adjudication. You do referable deficiency to engender a board output.

    Take attention of the ocean sever of the tenor principal, then chaffer with the appropriate occurrences

    Don’t coercionget to apprehend input validation, coercion development, the input must be scalar

    Expert Rejoinder

     

    Code:

    %Define exercise

    exercise ANS = Parent(a)

    %Define tolerance

    tol = 10^(-4);

    %Compute x

    x = abs(a)/2;

    %Compute e

    e = 99;

    %Loop

    while (not(e < tol))

    %Compute compute

    y = (x + abs(a)/x)/2;

    %Compute fallacy

    e = abs((y-x)/y);

    %Assign compute

    x = y;

    %End

    end

    %If term satisfies

    if(a>0)

    %Assign compute

    ANS = x;

     

    %Compute fallacy

    fallacy = abs(ANS – sqrt(a))

    %Otherwise

    else

    %Assign compute

    ANS = x*i;

    %End

    end

    %End

    end

    %Call way

    Root(0)

    %Call way

    Root(2)

    %Call way

    Root(10)

    %Call way

    Root(-4)