ENGR 17
Practice Test 3
Make a folder Test3 and put all your scripts and function files AND PLOTS inside
1) If you roll a balanced PAIR of dice 300 times, how many times would you expect to
obtain-A) a sum of 8?
B) A sum of either 3,4 or 5?
C) A sum of less than 9?
HINT: The Matlab command to generate 300 throws of a six sided dice is
floor(rand(1,300)*6) +1
Make a histogram of 300 throws of two dice. You may either count the hits manually on
the histogram chart, or, for full credit, use the matlab vector/relational (and other)
commands such as z(z<10) to obtain the answer directly from the command line.
`
>> dice1 = floor(rand(1,300)*6) +1;
>> dice2 = floor(rand(1,300)*6) +1;
>> total = dice1+dice2;
>> hist(total,[2-12])
>> sum( total==8)
ans = 52
>> sum( total>=3 & total<=5)
ans = 81
>> sum( total<9)
ans = 222
60
50
40
30
20
10
0
2
3
4
5
6
7
8
9
10
11
12
2) Make a function file to compute the function foo(x) = x^2 / (sin(x) + 2)
(note that foo(0) = 0, foo() = 2/2 and foo( /2) = 2/12)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% foo.m
function y = foo(x)
y = x.^2 ./ (sin(x) + 2) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3) Use quad to find the area under function foo for x ranging from 0 to 10, in other words,
area = quad('foo',0,10)
area = 167.0325
4) Use a for loop and repeated calls to quad to generate a vector y formed by integrating foo
from 0 to x, in other words,
Start with an x vector of [0:0.1:10]
%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:length(x)
y(k) = quad('foo',0,x(k));
end
plot(x,y)
180
160
140
120
100
80
60
40
20
0
0
1
2
3
4
5
6
7
8
9
10
5) Find the numeric solution to
y' = t2 – y, given y(0) = 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% prob5dot.m
function ydot = prob5dot(t,y)
ydot = t.^2 – y
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Prob5.m
[t,y]=ode45('prob5dot',[0:.1:5],0)
plot(t,y)
18
16
14
12
10
8
6
4
2
0
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
© Copyright 2026 Paperzz