NOTE: I hope you have examined the solution for midterm one Midterm 1 Solutions
I plan to pose these questions in the same manner as our first midterm. So you will be asked to give the contents of the MATLAB Workspace and some of the problems, that are still evolving, will involve user-defined functions.
% assume the input value is in centimeters and this MATLAB code
% converts to the units specified by the variable, units
x = 2.7;
units = 'm';
disp([' for input ' num2str(x) ' in centimeters, this fragment converts to '])
switch units % convert x to centimeters
case {'inch','in'}
y = x * 2.54
case {'feet','ft'}
y = x * 2.54 * 12
case {'meter','m'}
y = x/100
case {'millimeter','mm'}
y = x*10
case {'centimeter','cm'}
y = x
otherwise
disp(['Unknown Units: ' units])
y = nan;
end
disp( units )
Give the output:
With the same source fragment above, what output will be computed for the case of
x = 1025; units = 'ft';
v = ['Character strings having more than'
'one row must have the same number '
'of columns just like arrays! ']
% assume the input value is in centimeters and this MATLAB code
% converts to the units specified by the variable, units
x = 2.7;
units = 'm';
disp([' for input ' num2str(x) ' in centimeters, this fragment converts to '])
switch units % convert x to centimeters
case {'inch','in'}
y = x * 2.54
case {'feet','ft'}
y = x * 2.54 * 12
case {'meter','m'}
y = x/100
case {'millimeter','mm'}
y = x*10
case {'centimeter','cm'}
y = x
otherwise
disp(['Unknown Units: ' units])
y = nan;
end
disp( units )
x = linspace(0,10,100); % create data
y = sin(x);
z = (y>=0) .* y;
z = z + 0.5 * (y<0);
z = (x <=8) .* z;
plot(x,z),...
xlabel('x'),ylabel('z = f(x)'), title('A Discontinuous Signal')
% set up data
x = 0:10; y = [x; x.^2];
fid = fopen('exp.txt','w') % open a file for writes
fprintf(fid,' %g %g \n ',y)