Question

In: Advanced Math

The “divide and average” method, an old time method for approximating the square root of any...

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. At each step estimate the error in your approximation as

ε = abs(( Xnew − 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. Test your program by evaluating a = 0, 2, 10 and -4 for ε = 1×10−4. Hint: This is similar to the IterMeth function discussed in class.

Solutions

Expert Solution


%%Matlab code for finding square root of a number
clear all
close all

val=square_root(0,10^-10);
fprintf('root of 0 is ')
disp(val)

val=square_root(2,10^-10);
fprintf('root of 2 is ')
disp(val)

val=square_root(10,10^-10);
fprintf('root of 10 is ')
disp(val)

val=square_root(-4,10^-10);
fprintf('root of -4 is ')
disp(val)

%function for square root of n
function val=square_root(n,es)
    %check for negative number input
    if n<=0
        n=abs(n);
        t=1;
    else
        t=0;
    end
    x=n;
    y=1;
    while abs(x-y)/abs(y)>es
        x=(x+y)/2;
        y=n/x;
    end
  
    if t==0
        val=x;
    else
        val=complex(0,x);
    end
  
end
    %%%%%%%%%%% End of Code %%%%%%%%%%   


Related Solutions

Newtons method for approximating square roots. The next iteration is the average of the previous iteration...
Newtons method for approximating square roots. The next iteration is the average of the previous iteration and the ratio of the number in question to the previous iteration.x_i = ( x_(i-1) + number/x_(i-1) ) / 2 if i is 0 the approximation is half the number. Pre: number & interations are non-negative integers Post: return an approximation of sqrt(number) , the return value is double double newton(size_t number, size_t iterations){}
What is the concept of the square root of time rule?
What is the concept of the square root of time rule?
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.data spread; population standard deviationStandard deviation; data spreadpopulation standard deviation; variancevariance; standard deviation
write a function to determine the square root of a number. The square root of a...
write a function to determine the square root of a number. The square root of a number can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for the next guess and LG stands for the last guess. The loop should repeat until the difference between NG and LG is less than 0.00001. Use an initial guess of 1.0. Write a driver program to test your square root function. I WANT THIS PROGRAM...
The standard deviation is equal to the square root of the average squared deviations about the...
The standard deviation is equal to the square root of the average squared deviations about the mean. More succintly, it is equal to the square root of the variance. So one way to calculate the standard deviation of a frequency distribution is to calculate the variance. Complete the table below as the first step in calculating the variance: (10 points) X f X−x (X−x)2 96 1 94 1 92 1 87 1 85 1 84 1 83 1 82 1...
Graph the square root of the following function. State the domain and range of the square root of the function.
Graph the square root of the following function. State the domain and range of the square root of the function.y = -2x + 4
Square-Root Implementation & Performance Comparisons Square root operation is considered difficult to implement in hardware, in...
Square-Root Implementation & Performance Comparisons Square root operation is considered difficult to implement in hardware, in this project, you must Write and test a MIPS assembly language program to implement three algorithms of an 8-bit integer square root. Background: In mathematics, a square root (√) of a number x is a number r such that r 2 = x, or, in other words, a number r whose square (the result of multiplying the number by itself, or r × r)...
show the root sum square method for describing the total uncertainty of a quantity, which depends...
show the root sum square method for describing the total uncertainty of a quantity, which depends on a set of parameters called Xi (i=1,n) explain the roll of the sensitivity coefficient of each parameters. This is experimental fluid dynamics question, please explain nicely
Calclute the root mean square speed of Ne atoms at temperture at whic their average kinetics...
Calclute the root mean square speed of Ne atoms at temperture at whic their average kinetics energy is 5.22kj/mol?
Java please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT