Title: MATLAB Code for Calculating Square Roots
Q4
x = 1:5;
% Create a vector x
y = sqrt(x);
% Create a vector y
T = [x; y];
% Create a 2x5 matrix T, where the first row is x and the second row is y
fprintf('If the number is: %i, its square root is: %f\n', T);
% The fprintf command displays two numbers from T in every line
% When this script file is executed, the display in the Command Window is:
% T
% 1.0000 1.0000
% 2.0000 1.4142
% 3.0000 1.7321
% 4.0000 2.0000
% 5.0000 2.2361
% The 2x5 matrix T
% If the number is: 1, its square root is: 1.000000
% The fprintf command repeats 5 times, using the numbers from the matrix T column after column.
% If the number is: 2, its square root is: 1.414214
% If the number is: 3, its square root is: 1.732051
% If the number is: 4, its square root is: 2.000000
% If the number is: 5, its square root is: 2.236068