USING MATLAB 1 Type in the following program. % find all powers of two less than
ID: 3675109 • Letter: U
Question
USING MATLAB
1 Type in the following program.
% find all powers of two less than N clear all N=1250; v=1; num=1; i=1; while num < N num = 2^i; v = [v; num]; % build vector i = i +1; end v % display v
Explain what the assignment statement, v = [v; num], does. Give all variable values, num, i and v for each iteration of the while loop. What is the dimension of v after this program has executed?
b) Modify the program of part (a) so that a user of the modified program is instructed to enter a value for N. Execute the program only if an N greater than two is entered. If N is not greater than two, use the disp function to inform the user that the program will be terminated. Use an ifelse structure around the present while loop. For example, you could start with:
N = input(‘Enter a value for N: ‘) if N > 2 v=1; num=1; i=1; while num < N . . . end else disp(‘N is not greater than 2, program terminated.’) end
Test your program for N greater than 2 and equal to 2, and describe results.
c) Now consider the following MATLAB program segment.
clear all x = input('enter Y or N: ','s') ans = (x == 'Y') | (x == 'y')
Look up the input command using MATLAB help, and discuss what the input command does when ‘s’ is also included in the input statement.
Explain what this program segment does, and specifically, explain what is being compared with the == operations.
Explanation / Answer
a) v = [v; num], statement take the value of num and add to the vector v. it also increases the size of vector by 1
Iterations of the loop
i: 1
num: 2
v:
1
2
i: 2
num: 4
v:
1
2
4
i: 3
num: 8
v:
1
2
4
8
i: 4
num: 16
v:
1
2
4
8
16
i: 5
num: 32
v:
1
2
4
8
16
32
i: 6
num: 64
v:
1
2
4
8
16
32
64
i: 7
num: 128
v:
1
2
4
8
16
32
64
128
i: 8
num: 256
v:
1
2
4
8
16
32
64
128
256
i: 9
num: 512
v:
1
2
4
8
16
32
64
128
256
512
i: 10
num: 1024
v:
1
2
4
8
16
32
64
128
256
512
1024
i: 11
num: 2048
v:
1
2
4
8
16
32
64
128
256
512
1024
2048
after the loop has ended v contains 2 elements , so its dimension is 12.
b)
clear all;
N = input('Enter the value of N');
if N>2
v=1;
num=1; i=1;
while num < N
fprintf('Loop: %d ', i);
num = 2^i;
v = [v; num]; % build vector
i = i +1;
fprintf('num: %d ', num);
disp('v:');
disp(v);
end
else
disp('N is not greater than 2, program terminated.');
end
when we run the program
Enter the value of N : 3 // all the elements of v will be printed
Enter the value of N : 1
N is not greater than 2, program terminated.
c)
x = input('enter Y or N: ','s')
returns the entered text as a string, without evaluating the input as an expression. and stores it in x.
== will compares the string stored in x with 'Y' and 'y' . if it is equal to Y or y, the statement will be true and ans will hold the value 1.
else ans will hold the value 0.
this program just compared the entered ext x with Y and y, if it is equal to any one of those , ans is assigned value 1, else ans is assigned value 0.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.