MatLab Exam 3
Nesting Loops i =3 and j =2
(1 x 1) + (1 x 2) + (2 x 1) + (2 x 2) + (3 x 1) + (3 x 2) = 18
Consider the following code: for i = 12:20 disp("In a loop!"); if i ~= 12 continue; else break; end disp("In a loop!"); end How many times does the message "In a loop!" gets printed?
1
Consider the following code: x = 3; y = 1; while (x - y) > 0.001 x = (x + y)/2; y = 2/x; end The loop eventually terminates when x is an approximation of the square root of 2 within 0.001 accuracy. What is the value of x after 2 iterations of the loop?
1.5
Consider the following code: x = 0; for i = -10:-1 for j = -10:-1 x = x + 1; end end What is the value of x after the loop is executed?
100
Consider the following code: x = 0; for i = 1:10 for j = 1:10 if (i + j) == 3x = x + 1; end end end What is the value of x after the loop is executed?
2
What is the result of the following code? % create array of values between -1 and 1 points = linspace(-1, 1, 50); % [-1, ..., 1] x = points;] % [1, ..., -1] y = fliplr(points); % the absolute value of each point in x z = abs(x); scatter3(x, y, z);
3 dimension
Given the following code, what kind of grid the figure window would be divided into by using the subplot? subplot(3, 2, 2);
3*2--> 3 rows, 2 columns grid
Consider the following code: i = 16; while i >= 1 i = i / 2; end How many times does this while-loop iterate?
5
Consider the following code: i = 1; while i < 32 i = i * 2;end How many times does this while-loop iterate?
5
Consider the following code: for i = 10:16 disp("In a loop!"); if i ~= 10 continue; end disp("In a loop!"); end How many times does the message "In a loop!" get printed?
8
what will plot([3,5,6,3,1]) give
It will put the values in the Y axis with the x axis starting from 1 to 5
What will plot([3,2,3,5,1],[4,5,6,7,8])
It will treat the x and y axis normally so the first point is 3,4 the next 2,5
matlab hold on and off command?
Multiple graphs on the same plot are made using the hold on command. The hold off command ensures graphs are created on different plots.
linespace funcion (1,2,3)
The starting point of the range. The ending point of the range. The number of points to generate between the starting and ending points. so the domain is from 1-2 and same with range with 3 points evenly spread out
What is the result of the following code? x = linspace(-pi, pi, 21); y = tan(sin(x)) - sin(tan(x)); plot(x,y,':mo',... 'LineWidth', 2,... 'MarkerSize', 10,... 'MarkerEdgeColor', 'g',... 'MarkerFaceColor'[0.5,0.5,0.5]); xlabel('X'); ylabel('Y'); Color Specifier SpecifierDescription b-blue g-green r-red c-cyan m-magenta k-black Line Segment SpecifierDescription -solid :dotted -.dashdot —dashed none-no line Marker Specifier SpecifierDescription +plus sign o-circle *-asterisk .-point x-cross s-square
What is the result oft he following code?
x = linspace(-360, 360, 50); % define rows and columns rows = 2; columns = 1; y1 = sind(x); subplot(rows, columns, 1); plot(x, y1); y2 = cosd(x); subplot(rows, columns, 2); plot(x, y2);
Which of the following lines of code generates this graph?
x = linspace(-360, 360, 50); % hold call hold on; y1 = sind(x); plot(x, y1); y2 = cosd(x); plot(x, y2); % hold call hold off;
Which of the following lines of code generates this graph?
x = linspace(-180, 180, 50); % define rows and columns rows = 2;* columns = 1; * y1 = sind(x); subplot(rows, columns, 1); plot(x, y1); y2 = cosd(x); subplot(rows, columns, 2); plot(x, y2);
Which of the following lines of code generates this graph? (Choose the answer that best fits the graph)
x = linspace(-10, 10, 50); rows = 2; columns = 1; y1 = x.^2; y2 = 2*x.^2; y3 = abs(x); y4 = 2*abs(x); % subplot 1 subplot(rows, columns, 1); hold on; plot(x, y1); plot(x, y2); hold off; % subplot 2 subplot(rows, columns, 2); hold on; plot(x, y3); plot(x, y4); hold off;
Which of the following plots is generated by this code?
Which of the following is a correctly written for-loop that repeats 6 times?
for i = 7:12 end
What is the resulting plot from the following commands? A = [4, 9, 16, 25, 36, 48]; plot(A);
the points go on the Yaxis
Consider the following code: i = 64; while i >= 0 i = i / 2; end How many times does this while-loop iterate?
this is an infinite loop
What is the result of the following code? figure(1) age=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; weight=[25, 30, 34, 39, 45, 49, 57, 65, 70, 85]; plot(age,weight); xlabel('Age'); ylabel('Weight'); title('Graph for The Average Human Body Growth');
two dimension graph with labels
What is the result of the following code? % [-1, ..., 1] x = linspace(-1, 1, 50); % cubed values of x y = x.^3; scatter(x, y);
two dimension plane
Matlab while statement
while needs to have a single variable such as While x>5 it wont work if While x = 5:x
Consider the following code: x = 0; for i = -5:5 for j = -5:5 x = x + 1; end end Which of the following is an equivalent while-loop version of the above?
x = 0; i = -5; while i <= 5 j = -5; while j <= 5 x = x + 1; j = j + 1; end i = i + 1;end
Which command enables a title for the x-axis
xlabel()