MATLAB Program to find out Square root by solving Difference equation
  • Home
  • /
  • MATLAB Program to find out Square root by solving Difference equation

MATLAB Program to find out Square root by solving Difference equation


Consider the causal non-linear discrete-time system characterized by the following difference equation:


                                  2y[n] = y[n-1] + x[n]/y[n-1]


If we use as input x[n] to this system (algorithm) a step function of amplitude A (i.e. x[n] = A u[n]), then y[n] will converge after several iterations to the square root of A.

·    Write a MATLAB program that implements the above recursion to compute the square root of 81,

49, 16 and 3.

·    How many iterations does it take to converge to the true value starting at y[-1] = 0.2?

 


MATLAB Code
function [yn,iter] = squareroot2(A,yminus1)
yn=0.5*((yminus1)+A/yminus1);%running the first iteration outside the loop
yminus1 = yn;%taking the initial value for next iteration
iter=1;
while yn~=sqrt(A)%loop stops when square root found
yn=0.5*((yminus1)+A/yminus1);
yminus1 = yn;
iter=iter+1;
end
end
 
Output (MATLAB)
[sqrt2,iterations]=squareroot2(81,0.2)
sqrt2 =  9
iterations =  10
 
[sqrt2,iterations]=squareroot2(49,0.2)
sqrt2 =  7
iterations =  10
 
[sqrt2,iterations]=squareroot2(16,0.2)
sqrt2 =  4
iterations =  9
 
[sqrt2,iterations]=squareroot2(3,0.2)
sqrt2 =  1.7321
iterations =  8