Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

% Do a step-by-step trace of each loop and determine what is % printed by the di

ID: 3709956 • Letter: #

Question


% Do a step-by-step trace of each loop and determine what is
% printed by the disp() functions.

% Loop 1
disp('loop 1' );
xx = 10;
while( xx > 3 )
xx = xx - 2;
end
disp(xx); % xx = ______

% Loop 2
disp( 'loop 2' );
total = 1;
for( kk = 2 : 5 )
total = total + kk;
end
disp( [kk, total] ); % TC = _____, kk = _____, total = ______

% Loop 3
disp ('loop 3');
total = 100;
for( kk = 20 : -5 : 1 )
total = total - kk;
end
disp( [kk, total] ); % TC = _______, kk = ______, total = _____

% Loop 4
disp('loop 4');
total = 0;
vect=[20, 5, 12, 8];
for( kk = vect )
total = total + 2*kk;
end
disp( [kk, total] ); % TC = ______, kk = _____, total = ______

% Loop 5
disp( 'loop 5' );
total = 0;
xx = [ 3, 6, 9, 12, 15, 18, 21 ];
for( kk = 1 : 2 : length(xx) )
total = total + xx(kk);
end
disp( [kk, total] ); % TC = ______ kk= _______, total = _______

% Loop 6
disp( 'loop 6' );
total = 0;
xx = [ 2, 3, 5, 7, 11, 13, 17, 19 ];
for( kk = 1 : 2 : length(xx) )
xx(kk+1) = 2*xx(kk);
end
disp( xx ); % xx = [____, ____, ____, ____, ____, ____, ____, ____ ]
% TC = _____________

7.
Complete the function by filling in the blanks to estimate the infinite series,
? ? ?
?
?? ?
?
n
k
n k k 1 1
1
1 lim .
The function accepts one input parameter n, and outputs an estimate for the series.
function [ ____________ ] = estOne( ____________________ )
%
% input: nTimes, the number of times to repeat the running sum.
% output: total, an estimate close to one.
%
% Begin by setting the running sum accumulator to zero.
______________ = 0;
for kt = _____________ : ______________________
__________ = ___________ + __________________________;
end
end

8. Complete the function to implement the infinite series: ? ? ?
?
?? ?
?
n
k
k
n k
x
x
0
2
(2 )!
cosh lim
function [ ____________ ] = coshEst( ___________, ___________ )
%
% inputs: 1. The x value input to cosh(x).
% 2. the number of times, n, to repeat the running sum.
% output: cosh(x).
% use the factorial(2k) to compute (2k)!
%
% Begin by setting the running sum accumulator total to zero:
____________ = ____________ ;
for kt = _________________ : _______________________
% Fill-in the needed steps for completing the calculation.
end
end

Explanation / Answer

Q1:  Do a step-by-step trace of each loop and determine what is printed by the disp() functions.

% Loop 1
disp('loop 1' );
xx = 10;
while( xx > 3 )
xx = xx - 2;
end
disp(xx); % xx =2

Loop will execute and xx will be decremented 10,8,6,4,2.

when 2 comes it exits from while loop and print xx as 2.

--------------------------------------------------------------------------------------------------------------------------------------------------------

% Loop 2
disp( 'loop 2' );
total = 1;
for( kk = 2 : 5 )
total = total + kk;
end
disp( [kk, total] ); % TC = _____, kk = _____, total = ______

kk=5 ,total=15

in this loop there is addition of series from 2 to 5 and store in total variable.

--------------------------------------------------------------------------------------------------------------------------------------------------------

% Loop 3
disp ('loop 3');
total = 100;
for( kk = 20 : -5 : 1 )
total = total - kk;
end
disp( [kk, total] ); % TC = _______, kk = ______, total = _____

kk=5 ,total=50

in this loop there is subtraction of series from 20 to 1 with diff of -5 and store in total variable.

--------------------------------------------------------------------------------------------------------------------------------------------------------

% Loop 4
disp('loop 4');
total = 0;
vect=[20, 5, 12, 8];
for( kk = vect )
total = total + 2*kk;
end
disp( [kk, total] ); % TC = ______, kk = _____, total = ______

kk=8 ,total=90

kk runs for a vector and will stop when last value comes that is 8.

total is sum of 2 times each value of kk in loop

--------------------------------------------------------------------------------------------------------------------------------------------------------

% Loop 5
disp( 'loop 5' );
total = 0;
xx = [ 3, 6, 9, 12, 15, 18, 21 ];
for( kk = 1 : 2 : length(xx) )
total = total + xx(kk);
end
disp( [kk, total] ); % TC = ______ kk= _______, total = _______

kk=7 ,total=48

--------------------------------------------------------------------------------------------------------------------------------------------------------

% Loop 6
disp( 'loop 6' );
total = 0;
xx = [ 2, 3, 5, 7, 11, 13, 17, 19 ];
for( kk = 1 : 2 : length(xx) )
xx(kk+1) = 2*xx(kk);
end
disp( xx ); % xx = [____, ____, ____, ____, ____, ____, ____, ____ ] % TC = _____________

xx= 2 4 5 10 11 22 17 34?

--------------------------------------------------------------------------------------------------------------------------------------------------------