Due In Lab the week of October 17th Main topics User Defined
Solution
The MATLAB function Lab06.m ( save this function as Lab06.m)
function [ y ] = Lab06( x,n ) % The function Lab06 to compute approximate value of sqrt(x)
y = x/2; % The initial guess of iteration
for i = 1:n % n times loop to compute the nth approximation
y = (y+x/y)/2; % The approximation formula
end % End of the loop
end % End of the program
The Driver06.m MATLAB script file
% Begining of the program Driver06.m
clear all; % Clearing the Workspace
% Disciption of the program to the user
fprintf(\'This is program to compute square root of \"x\" approximatly using \"n\" iterations\ \');
x = -1; % initilization of the x varialbe as a negative value
n = -1; % initilization of approximation index as a negative value
while x<0 % This loop will continue until the user enters a positive value
x = input(\'Enter a positive x: \'); % Getting the value of x
end % End of the loop
while n<0 % Tis loop will continue until a user enters a positive val
n = input(\'Enter a positive number n: \');% taking the value of n from user
end % End of the loop
y = Lab06(x,n); % Computing the square root of x by calling the function Lab06.m
% Printing the result to the screen for the user
fprintf(\'The approximate square root of %f is %f using %d iterations\',x,y,fix(n));
% End of the program Driver06.m
SAMPLE OUTPUT1
>> Driver06
This is program to compute square root of \"x\" approximatly using \"n\" iterations
Enter a positive x: 100
Enter a positive number n: 5
The approximate square root of 100.000000 is 10.000046 using 5 iterations>>
SAMPLE OUTPUT 2
>> Driver06
This is program to compute square root of \"x\" approximatly using \"n\" iterations
Enter a positive x: 100
Enter a positive number n: 50
The approximate square root of 100.000000 is 10.000000 using 50 iterations>>
