Each problem is worth 1/4 point.
1. For this question first type the following expression at the Matlab prompt:
>> string = [ 'y = ' ,
_______num2str(number)_okay also accept
___num2str(1.2345)_________________________ , ' * cos(x)'
];
number
____________double array______________________________________
string
____________char array______________________________________
To answer the following questions type the following commands at the Matlab prompt:
>> a = 2 : 3 : 17; >> b = 1:6;
2. Fill in the blanks with the response Matlab gives when you type
the following at the Matlab prompt. If the result is an error then
write ERROR.
>> a
a =
____ 2 5 8
11 14 17 ______
3. >>
length(a)
ans =
____6_____
4. >> size(a) , size(b)
ans =
_____1 6
ans =
_____1 6_____
5. >> 3 a
ans =
_____ERROR_______
6. >> 3 * a
ans =
6
15 24 33 42
51 ______
7. >> 3 .* a
ans =
6 15 24 33 42 51
8. >> a * b
ans =
_________ERROR____________
9. >> a .* b
ans =
2 10 24 44 70 102
10. >> a .^ 2
ans =
4 25 64 121 196 289
11. >> 2 .^ b
ans =
2 4 8 16 32 64
12. >> (a .* b) ./ b
ans =
2 5 8 11 14 17
13. >> (a .* b) ./ a
ans =
1 2 3 4 5 6
Type the following expression at the Matlab prompt then either answer the question or fill in the blanks with the response Matlab returns. If there is an error write ERROR.
>> x = linspace(0,3*pi,100); >> y = [ 1 5 9 13 17 21] ;
14. How many elements does the vector x have?
_____100___________________
15. >> y(3:end)
ans =
9 13 17 21
16. >> y(1:end-1)
ans =
1 5 9 13 17
17. >> x( [1 2])
ans =
______ 0 0.0952
(approx.)
18. >> x(1:2)
ans =
______ 0
0.0952__________(approx.)
19. Fill in the blank below to carve out the last
two values of y (in reverse order) and prepend zero to these
two values. The correct answer would display
z =
0 21 17
>> z
= [ 0 , _____y(end:-1:end-1) OR
y([6,5])___OR y(end), y(end-1)_____ ]
(The
correct answer must use the variable y and subscriping on y)
20. Fill in the blanks below to flip the values of y. The
correct answer would display
flipy =
21 17
13 9 5 1
>> flipy = y( end : ___-1___ : ___1____
)
21. Complete the following Matlab expression to simulate the roll
of one, six-sided die. Note the die values are 1,2,3,4,5,6 and are
integers.
(Hint: use the function rand with proper scaling)
ceil( _____6_____ * rand(1,1) )
22. Write a Matlab expression to display a row vector with 2
random integers in the range from 2 to 5 inclusive.
>>
random =
ceil( ______4________* rand(1,2) + _____1________ )
To answer the following questions, first type,
>> syms
x
23. Write the response Matlab returns after typing the
following,
>> factor(x^3 - 1)
_______(x-1)*(x^2 + x+1)____________________
24. Write the
response Matlab returns after typing the following,
>> simple(sin(x)^2+cos(x)^2)
(Just write
the answer, don't write the intermediate results)
ans = ______________1______________________
25.
Fill in the blank below to simplify the mathematical function
cos(2x) / (cos2(x)-sin2(x)).
(The domain is a subset of the Reals).
>> simple(__cos(2*x) /
(cos(x)^2-sin(x)^2)___________)
26. For the function y = 1/x choose the sample
of x values as [ -2 -1 1 2] type the
following command at the Matlab prompt
>> x = [-2 -1 1
2];
Fill in the blank below
to compute the values for y, (you should type this at the Matlab
prompt for use in Part 8)
>> y = ____1./x OR
x.^-1 but NOT 1/x_____________
That's it, we have now discretized y = 1/x . How good is our discretization? We find out in Part 8.
27. y = 1.234 * sin(x) Fill in the blanks below to discretize this function. Choose the sample of x values as 50 evenly spaced values from 0 to 2*pi. We will use x2 and y2 so that we can keep these separate from the x and y in problem #26.
>> x2 =
_____linspace(0, 2*pi, 50)__________________________
>> y2 = _______1.234
* sin(x2)________________________
28. Type the following comand to plot the discretized parabola we
created in Part 7.
>> plot(x,y,'g') ('g' for
green)
Was the discretization in #26 good? No, since 1/x is a hyperbola
and is undefined for x = 0. Fix the problem by plotting y = 1/x
using 101 points from -2 to 2. The plot should be in red, no
lines connecting the points and each point should be marked with a
*(star) not a . (period).
>> x = ___linspace(-2, 2,
101)_________________
>> y = _____1./x OR
x.^-1________
>> plot( ____x________ ,
_________y___________, _____'r*'_________)
>> grid on
29. We will now plot the discretized function from problem #27.
>> plot(x2,y2)
Now put two plots on one figure
window, type,
>> hold on
>> plot( x2,y2)
>>
grid on
Does plot(x2,y2) give the same results as plot(y2,x2)
______NO__________(YES/NO)?
Since every real number in Matlab number takes up eight
bytes of memory and there is eight bits per byte, this means that
every real number takes up 64 bits. A bit is a 0 or a 1 so there are
two possibilities for each bit and thus 264 different
possible real numbers can be represented in Matlab. That's not
infinite, and since there are an infinite number of real numbers
(actually an infinite number between any two real numbers), Matlab
has lots of holes or gaps in its representation of the real numers.
This problem is called roundoff error. If you take CS/Math 257 you
will learn a lot more about this. But let's do one
example.
Approximate the derivative of cos(x) at
x = pi/2. From Calculus we know the formula for the slope of the
derivative
d/dx(cos(x)) = -sin(x) so plug in x = pi/2 and we
get -1.0 exact. Now from the instructions let's use the Centered
Difference formula to approximate the derivative, however we will use
various values of h, type,
>> format long e
>> h =
10 .^ -(2:14)
>> derivative = (cos(pi/2 + h/2) - cos(pi/2 -
h/2))./h
>> error = abs(derivative + sin(pi/2))
>>
loglog(h,error) % loglog is like plot except it plots the log
of both h and error
30. Which value of h gives us the best
approximation to the derivative, that is, which value of h gives us
the smallest error?
h =
__________1.0e-5_______________________
Because of
roundoff error arbitrarily small values for h won't necessarily give
us better values for the derivative or in any numerical calculation(
like the definite integral). However, in CS101 we will be in the
"denial stage" and will try to ignore roundoff error. If
you can't do this then you are welcome to take a course on Numerical
Analysis like CS/Math 257.
Using the formulas derived
in Part 9 of the instructions,
>> midpoints = (x(1:end-1) + x(2:end))/2 ; >> yprimes = diff(y) ./ diff(x) ;
fill in the blanks below in order to compute and plot the derivative of y = sin(x) on the interval [0, 2*pi] using 10 equally spaced points.
>> x2 = linspace(0,2*pi,10);
>> y2 = sin(x2);
31.
>> midpoints = __(x2(1:end-1) +
x2(2:end))/2_____________ ;
32.
>> yprimes = _____diff(y2) ./ diff(x2)_______ ;
>>
plot(midpoints, yprimes , 'g')
>> hold on
>>
plot(midpoints , cos(midpoints), 'r')
(we know that the derivative of sin(x) is cos(x) so let's compare
symbolice vs. discrete)
33. Type the following assignment statement at the Matlab prompt,
>> v = [ -1 0 2 0 3
0 4 0 5 0 6]
Fill in
the blank in the Matlab expression below that uses subscripting
to extract all non-zero values from v and assign these to the
variable w.
>> w = v ( ____________v ~=0_______________________________)
w =
-1 2 3 4 5 6
34. Fill in the blank in the Matlab expression below that uses subscripting to extract all zero values from v and assign these to the variable w.
>> w = v ( ____________v == 0_____________________________)
w =
0 0 0 0 0
35. Suppose that we are given the discretized values of a function y = f(x) as below. We want to find all the roots of the function f . That is, we want to find all the values r such that 0 = f(r).
Type the following assignment statements at the Matlab prompt,
>>
y = [ 1 0 5 0 -3
0 4 0 5
0 ] ;
>> x = [ .1 .2 .3 .4
.5 .6 .7 .8 .9 1.0 ] ;
Fill in the blank in the Matlab expression below that uses
subscripting to find all the roots of the discretized function. Yes
the answer is [ .2 .4 .6 .8
1.0] but your answer should be a Matlab command that gives you the
correct answer no matter what x and y are, assuming of course they
have the same number of elements.
r =
______________x( y == 0)___________________________________ ;
r =
0.2000
0.4000 0.6000 0.8000
1.0000
36. For the x and y values from problem #35
find the x values that correspond to the smallest value in y. The
answer is .5 since -3 is the smallest value for y. Write Matlab code
to produce this answer. Your answer should work for any values for x
and y. Use subscripting and the Matlab function named min.
result = _________x( y ==min(y))_____________________________________ ;
result =
0.5000
Congratulations, you are done!