Due In Lab the week of October 17th Main topics User Defined

Due: In Lab. the week of October 17th Main topics: User Defined Functions Function .m files Open MATLAB Create a Function file named Lab06 Right click Current Folder title of that window Select New File by moving mouse to that option Click on Function in the pop-up menu Type Lab06 in the highlight field and hit the enter key Double click the Lab06.m entry (that you just created) in the Current Folder window We have seen that the approximation of the squareroot of a positive number x can be defined iteratively: y_0 = x/2 y_n = y_n - 1 + x/y_n - 1/2 Modify the Function file so that it: Takes both x and n as input Calculates the nth approximation of the squareroot of x. Use the loop of your choice, but think which one is best suited for this task Returns this approximation as output Create a Function file named Lab06 Right click Current Folder title of that window Select New File by moving mouse to that option Click on Script in the pop-up mom Type Driver06 in the highlight field and hit the enter key Double click the Driver06.m entry (that you just created) in the Current Folder window Modify the Script file no that it: Uses a User Validation Loop to get a positive number x from the user Uses a User Validation Loup to get a second positive number n from the user Calculates the nth approximation of the squareroot of x, by calling your function Lab06 Displays all user input and the functions output in a reasonable report like format Once you get to this point, you should show your answers to your TA, and get checked off for this lab.

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>>

 Due: In Lab. the week of October 17th Main topics: User Defined Functions Function .m files Open MATLAB Create a Function file named Lab06 Right click Current

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site