home

Scientific Computing (Psychology 9040a)

Fall, 2021

Square Root


Write a MATLAB function called sqrtN() to estimate the square root of any number using Newton’s method:

Write your function so that the number N whose square root is to be taken, and the number of iterations to use M, are both input parameters of the function. The output of the function should be the estimate of the square root of N. You can choose the initial guess of the value of the square root on your own, use whatever you like. I suggest the number 10.0 as an initial guess, as in the Wikipedia example.

Write a MATLAB script that uses your sqrtN() function to find the square root of N=612 using M=5 iterations. Try it again using M=10 iterations.

Your script should look like this:

N = 612;
M = 5;
x = sqrtN(N, M);
fprintf('The square root of %.3f using %d iterations is %.12f\n', N, M, x);
M = 10;
x = sqrtN(N, M);
fprintf('The square root of %.3f using %d iterations is %.12f\n', N, M, x);

and should produce output like the following:

The square root of 612.000 using 5 iterations is 24.738633753766
The square root of 612.000 using 10 iterations is 24.738633753706