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.
for k = 1:25
if d(k) <= 30
velocity(k) = 0.425 + 0.00175*d(k)^2;
else
velocity(k) = 0.625 + 0.12*d(k) - 0.0025*d(k)^2;
end
end
% 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 )
Gives the output:
for input 2.7 in centimeters, this fragment converts to y = 0.0270 m
With the same source fragment above, what output will be computed for the case of
x = 1025; units = 'ft';The output would be:
for input 1025 in centimeters, this fragment converts to y = 31242 ft
title = Data for 45 cows in a pasture
msg = You're right!
ans = You
ans = right
v = ['Character strings having more than'
'one row must have the same number '
'of columns just like arrays! ']
v = Character strings having more than one row must have the same number of columns just like arrays!
lawyers = Cockran Shapiro Clark Darden
ans =
4 7
% 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 '])
tic
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
toc
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)
0 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100