Midterm 2B with solutions

Name: ____________________________________________
UCI ID#: _______________
MIDTERM 2B – MAE10 Spring 2015
Section 1: Provide a short answer to each of the following questions (2 pts each, no partial
credit).
What should be the name of the function stored in function file testfun.m?
testfun
What is the minimum number of inputs required in a user-defined function?
0
What is the maximum number of outputs allowed in a user-defined function?
No maximum
What values are stored in x, y:
[x,y] = size([1:2:7; 2, 4, 6, 8; -1:3:9]);
x=3
What values are stored in y:
y = numel([1:2:7; 2, 4, 6, 8; -1:3:9])
y=12
y=4
Section 2: Identify any and all errors that would prevent the code from executing in the
following MATLAB/Octave statements. Warnings and "bad programming" are
acceptable. Those would not prevent the code from running. If you believe there are
no errors, write Ok. (3 pts each, no partial credit)
1.
2.
3.
4.
5.
D=[1 2; 1 2];
D(3,2)=2*D(2);
a = 10;
a(2)=a;
a(2)=a(2,1);
one=1; two=2;
three = two + one;
for = 2*one + two;
class = ‘MAE10’
switch class
case {MAE10}
disp(class)
end
for x=0:0.5:10;
f(x)=x.^2-2*x+1;
disp(‘f(x)’);
end
Ok
a(2,1) is not defined
for is a key word
MAE10 is not defined
x is not an integer
Name: ____________________________________________
UCI ID#: _______________
These problems contain a function program and the “main”
program that calls the function, each stored in a different
m-file. Identify all errors in the “main” or function
programs, if any exist.
6.
In the “main” program:
x = [3, 4, 5, 6 ; 7, 8, 9, 10];
h = pig( x(3,:) ) * pig( x(:,3) )
x does not have 3 rows
In pig.m:
function [ x ] = pig(y)
x = -10;
x = x + mean(y);
results = 10;
end
7.
In the “main” program:
x = [3, 4, 5, 6 ; 7, 8, 9, 10];
h = dog(x(1,:)) + b
b is not defined
In dog.m:
function [ x ] = pig(b)
x = -10;
x = x + mean(b);
b = (10-x);
results = 10;
end
8.
function name mismatches file name
In the “main” program:
x = [3, 4, 5, 5];
y = [6, 7];
[a] = cheese(x, y);
In cheese.m:
function [ results ] = cheese(y, x)
results = y + x
y and x have different dimensions
return
end
Name: ____________________________________________
UCI ID#: _______________
Section 3: Write the output displayed by the following codes (3 pts each)
1.
a = 0; b = 2; c = 3;
d = a == b | b > c;
e = c < b & c < a;
if (~d)
disp('d is true')
elseif(~e)
disp('e is true')
else
disp('none are true')
end
2.
k=3;
while (k<8)
k=k+4;
for i = k:8;
kpi = k+i;
disp([k,i,kpi])
end
end
3.
for i=2:-2:-2
j = 2 - i;
for k=3:j
A(j,k)=i;
end
end
disp(A);
Consider these values and functions:
x=3; y=1;
z = @(y,x) 2*y-x;
function [y1] = f1(x,y)
y1=x-y;
end
function [y2] = f2(x,y)
y2=2*f1(x,y)-1
end
Name: ____________________________________________
4.
for i=2:-1:0
for j=i:2
disp(f1(i,j));
end
end
5.
f2(f1(x,y),y)
6.
z(f2(f1(x,y),y),y)
Section 3 Solutions
3.1
d is true
3.2
7
7
7 14
8 15
3.3
0
0
0
0
0 0 0
0 0 0
0 0 0
0 -2 -2
3.4
0
0
-1
0
-1
-2
3.5
1
3.6
1
UCI ID#: _______________
Name: ____________________________________________
UCI ID#: _______________
Section 4: Write the exact output displayed by the following codes (3 pts each)
A=[1:0.5:2.50];
B=[1; 2; 3; 4];
C=[1 2 ; 3 4];
E=['MAE10','great','course'];
F=char('Tim','Tom','Pat');
fprintf('A=%5.2f B=%5.2f\n', [A ; B']')
fprintf('C=%5.2f%5.0f\n', C)
fprintf('How was %s? \n%s, of %s!\n', E(1:5),E(6:10),E(11:end))
fprintf('F=%3s\n', F(1:3:9), F(2:3:9), F(3:3:9))
4.1
A= 1.00 B= 1.50
A= 2.00 B= 2.50
A= 1.00 B= 2.00
A= 3.00 B= 4.00
4.2
C= 1.00 3
C= 2.00 4
4.3
How was MAE10?
great, of course!
4.4
F=Tim
F=Tom
F=Pat
Name: ____________________________________________
UCI ID#: _______________
Section 5: Write a program to complete the following tasks. You do not have to write the
output of the code.
(5.1)
Write a program that calculates the sum of all integers that are smaller than 100 and
that are multiple of 2 and 9. Output the results in a file called table.txt. First column
should have the multiples of 2 and 9, and the second should have the cumulative sum.
You don’t need to display headers of the table (8 pts).
(5.2)
Write a function that returns the logical variable TRUE (which is 1) if its three integer
arguments form a Pythagorean triple. Note that a Pythagorean triple consists of three
positive integers a, b, and c, such that a2 + b2 = c2. Do not assume that the arguments
are entered in increasing order. Example: f(3,4,5) should return TRUE; f(13,12,5) should
return TRUE; f(1,2,3) should return FALSE (0) (8 pts).
(5.3)
Write a function that approximates the square root of a real number, A. The algorithm
is as follows. First choose an initial guess xi=G. Then refine the guess using the
following relation:
𝑥𝑛𝑒𝑤 =
1 𝐴
( + 𝑥𝑖 )
2 𝑥𝑖
If xnew is sufficiently close to xi, stop. Otherwise, set xi = xnew and continue refining the
guess. The function should also contain as input the initial guess, G. Your function
should be accurate to five decimal places (10 pts).
Name: ____________________________________________
UCI ID#: _______________
Section 5 Solutions
Problem 5.1
clc; clear;
id=fopen('table.txt');
Sum=0;
for i=3*7:3*7:99;
Sum=Sum+i;
fprintf(id,'%3i %5i\n',i,Sum);
fprintf('%3i %5i\n',i,Sum);
end
fclose(id);
Problem 5.2
function pyth=prob5_2(a,b,c)
pyth = a^2+b^2==c^2 | a^2+c^2==b^2 | c^2+b^2==a^2;
Problem 5.3
function res=prob5_3b(A,G)
error = 1000;
xi = G;
while error > 0.00001
xnew = 1/2*(A/xi+xi);
error=abs(xnew-xi);
fprintf('%10.5f \n',xnew);
xi=xnew;
end
res=xnew;