Midterm 1 with solutions

Transcription

Midterm 1 with solutions
Name: ____________________________________________
UCI ID#: _______________
Midterm – MAE10 Spring 2015
Section 1: Short Answer. (2 points each)
What is the command to clear the variables stored in memory?
clear
What is the command to find information on the variables stored in memory?
whos
Consider the following matrix: A = [ 1 2 3 ; 4 5 6 ]; If B = numel(A) and C = size(A),
What is the value of B?
6
What is the value of C?
[ 2 3]
Section 2: Identify ALL the programming errors in the following commands (3 points each)
1.
2.
3.
4.
A = [ 0:3 ; 2:5 ];
D=[1 2; 1 2];
2DA=2*D;
2DA is an invalid variable name
a = 10; b = a*2; c = b*3;
disp(a,b,c);
invalid arguments for disp
city = 'Irvine'
switch Irvine
case {'city'}
disp(city)
end
for i=1:0.5:2;
A(i)=i.^2;
disp('A(i)');
end
5.
Numb(1) = [ 1 2 ];
Numb(2) = [ 2 3 ];
disp([Numb(1) Numb(2)])
6.
hi = 'bye';
bye = 'hi';
x = 7;
if(bye = 'hi' | x > 8)
disp(bye)
end
Irvine is not a defined variable
index for array A(i) must be integer or logical
mismatch in the number of elements
mismatch in the number of elements
two ‘=’ signs required in logical expressions
Name: ____________________________________________
UCI ID#: _______________
Section 3: State what is the exact output that will be produced by each of the following programs.
Assume that there are no errors. If you are given specific formatting instructions, use underscores to
indicate blank spaces. In addition, carriage returns (newline) should be clear. (4 points each)
1.
for i = 1:3
for j = i:3
if(i<j)
disp([ num2str(i) ' is greater than ' num2str(j) ])
end
end
end
1 is greater than 2
1 is greater than 3
2 is greater than 3
2.
x = 2;
while(x<=5)
x=x+2;
disp('Hello')
end
Hello
Hello
3.
for i=1:3
for j=3:i
disp('This is an endless loop')
end
disp(['not quite ' num2str(i)])
end
not quite 1
not quite 2
This is an endless loop
not quite 3
4.
Matt = 'Name';
name = 'Matt';
switch name
case Matt
disp('no quotes')
case 'Matt'
disp('quotes')
end
quotes
Name: ____________________________________________
5.
A=[7:-2:1];
B(2,1:4)=2;
disp(A(3)+B(8))
5
6.
for i=1:2:5
C(i)=i;
disp(C)
end
1
1
0
3
1
0
3
0
5
UCI ID#: _______________
Name: ____________________________________________
7.
time=[0:0.5:2];
dist=3*time;
fprintf('%5.2f %5.2f\n', time, dist)
8.
time=[0:0.5:2];
vel=time/2;
fprintf('%5.2f %5.2f\n',[time ; vel ])
9.
x = [1 2 3; 4 5 6; 7 8 9];
fprintf('%i%i%i',x)
fprintf('\nHello %6.4f or %g\n',x(1,1),x(3,3))
fprintf('Here is a free point \n')
disp('But only if you get it right')
10.
invalid3_variable2_name1 = 'fprintf?';
fprintf('Studied %s',invalid3_variable2_name1)
num1 = 4.56;
num2 = 6.54;
fprintf('num2=%4.1f and num1=%1.4f\n',num1,num1)
fprintf('Now the value is: %12.3e \n',-(num1+num2))
7.
0
1
2
1
4
.
.
.
.
.
0
0
0
5
5
0
0
0
0
0
0
1
0
3
6
.
.
.
.
.
5
5
0
0
0
0
0
0
0
0
8.
0
0
1
1
2
.
.
.
.
.
0
5
0
5
0
0
0
0
0
0
0
0
0
0
1
.
.
.
.
.
0
2
5
7
0
0
5
0
5
0
4 7
e l
e r
u t
2
l
e
5 8 3 6
o
1 .
i s
o n l y
9
0 0
a
i
0 0
o r
9
f r e e
p o i n t
f
y o u
g e t
i t
i
t
f p
v a
r
l
9.
10.
1
H
H
B
UCI ID#: _______________
S t u d
N o w
e d
h e
r i g h t
i n t f ? n u m 2 =
4 . 6
a n d
n u m 1 =
u e
i s :
- 1 . 1 1 0 e + 0 0 1
4 . 5 6 0 0
Name: ____________________________________________
UCI ID#: _______________
Section 4: Write a code to complete the following tasks. You do not have to write the output of
the code.
(4.1)
Write a Matlab/Octave program that calculates the chapeau (‘hat’) function.
Chapeau function is as follows:
For 0 ≤ x ≤ 1,
Chapeau(x) = 1;
For x<0 or x>1,
Chapeau(x) = 0;
The program should request x as input, and display the value of Chapeau(x) (8 pts).
x = input('Enter x to calculate chapeau(x): ');
if(0<=x && x<=1)
fprintf('Chapeau(%6.2f)= %6.2f\n',x,1);
else
fprintf('Chapeau(%6.2f)= %6.2f\n',x,0);
end
Name: ____________________________________________
UCI ID#: _______________
(4.2) The Taylor Series Expansion of ln(1+x) is as follows:
where the i-th term is:
Write a Matlab/Octave program that requests inputs x and N from the user, with N being the
total number of elements in the taylor expansion to be computed. The function should output
the value of ln(1+x) if x is within the valid range -1< x ≤1. If x is outside that range, the program
should display the warning message: ‘Out of bounds!’ (12 pts).
x=input('Enter the value of x to calculate ln(x+1): ');
N=input('Enter the number of elements to be computed in the
Taylor Series Expansion: ');
if(-1<x && x<=1)
fun=0;
fprintf('%3s %8s\n','i','ln(1-x)')
for i=1:N
fun=fun+(-1)^(i+1)*x^i/i;
fprintf('%3i %8.3f\n',i,fun)
end
fprintf('ln(1-%8.3f)= %8.3f\n',x,fun)
else
disp('Out of bounds!')
end
Name: ____________________________________________
UCI ID#: _______________
(4.3) Write a program that asks the user to enter a number, N, that is smaller than 100. The
program should calculate the sum of the multiples of N that are smaller than 100. The program
should output a table in a file called table.txt that includes two columns: first column shows
the multiples of N and the second column shows the cumulative sum. The last line in the file
should say “END OF FILE”. (12 pts).
For example, below is the table for N=16:
i
Sum
-----16
16
32
48
48
96
64
160
80
240
96
336
END OF FILE
N=input('Enter a number smaller than 100 ');
Sum=0;
ID=fopen('table.txt','w');
fprintf(ID,'i
Sum \n');
fprintf(ID,'------\n');
for i = N:N:99
Sum=Sum+i;
fprintf(ID,'%3i
end
%6i\n',i,Sum);
fprintf(ID,'END OF FILE');
fclose(ID);