Docsity

Challenges Assignment - Introduction to Scientific Programming | COMP 116, Assignments of Computer Science

Toggle sidebar
bg1
COMP 116
17 Sept 07
Challenges (Functions)
Write functions to do the following in MATLAB. Some are already done as examples.
1. Function spots(m, n) should return a vector of n rows with the number of spots observed
n experiments of throwing m six-sided dice at a time.
function result = spots(m, n)
result = sum(ceil(6*rand(m, n)))';
2. Assume that you've used D = spots(2, 10000); to generate 10K experiments with two
dice. Determine the average number of spots, how often you observed more than the
average, and how often you observed exactly the average. (You'll get different numbers if
you redo D.)
3. Assume that you start this game with $15, pay $6.95 for throwing a pair of dice, and win
in dollars the number of spots showing. How much money do you have after 10000 turns?
4. Suppose that you must stop playing if ever you cannot afford the $6.95 buy-in. Now,
how much money do you have after 10000 turns (if you make it that far)?
5. Function star(n) should return a string of n '*' characters.
function result = star(n)
result(1:n) = '*'; % OR % result = repmat('*', 1, n);
6. Funtion repeatme(i, n) should return a row vector containing n copies of the number i.
7. Function box(r,c) should return an rc matrix of ‘*’ characters surrounding blanks (' '),
provided that r and c are integers between 2 and 80. E.g., box(3,4) is shown at right.
8. Function result = countCaps(string) should count the number of capital letters in the
variable named string. E.g., countCaps('COMP 116 Challenges are…') should return result =
5.
9. Function FtoC(f) should return a vector or matrix of the same size/shape as f with all
values converted from Farenheit to Celsius temperatures (5/9 * (f – 32)).
10. Function tempwarn(f) should return 'scorching' for temperatures over 95, 'cold' for
temperatures under 40, 'hot' for temperatures in the range of 85–95, 'cool' for temperatures in
the range of 40–65, and 'warm' for everything else.
11. Function bills(amount) should return a row vector with the numbers of $100, $50, $20,
$10, $5, and $1 bills to add up to amount, using the smallest total number of bills.
12. Function twoDigitEven(n) should return a list of the positive even integers with two digits
that are less than n.
13. Function nonprime(n) should return a list of the positive numbers from 1:n that are not
prime. (You may use the MATLAB function primes(n), which returns a list of numbers
from 1:n that are prime.)
14. Function oddwithcubeGT(n) should return the first odd integer whose cube is greater
than n.
****
* *
****
pf3

Partial preview of the text

Download Challenges Assignment - Introduction to Scientific Programming | COMP 116 and more Assignments Computer Science in PDF only on Docsity!

COMP 116

17 Sept 07 Challenges (Functions)

Write functions to do the following in MATLAB. Some are already done as examples.

1. Function spots(m, n) should return a vector of n rows with the number of spots observed n experiments of throwing m six-sided dice at a time. function result = spots(m, n) result = sum(ceil(6rand(m, n)))'; 2. Assume that you've used D = spots(2, 10000); to generate 10K experiments with two dice. Determine the average number of spots, how often you observed more than the average, and how often you observed exactly the average. (You'll get different numbers if you redo D.) 3. Assume that you start this game with $15, pay $6.95 for throwing a pair of dice, and win in dollars the number of spots showing. How much money do you have after 10000 turns? 4. Suppose that you must stop playing if ever you cannot afford the $6.95 buy-in. Now, how much money do you have after 10000 turns (if you make it that far)? 5. Function star(n) should return a string of n '' characters. function result = star(n) result(1:n) = ''; % OR % result = repmat('', 1, n); 6. Funtion repeatme(i, n) should return a row vector containing n copies of the number i. 7. Function box(r,c) should return an rc matrix of ‘*’ characters surrounding blanks (' '), provided that r and c are integers between 2 and 80. E.g., box(3,4) is shown at right. 8. Function result = countCaps(string) should count the number of capital letters in the variable named string. E.g., countCaps('COMP 116 Challenges are…') should return result = 5. 9. Function FtoC(f) should return a vector or matrix of the same size/shape as f with all values converted from Farenheit to Celsius temperatures (5/9 * ( f – 32)). 10. Function tempwarn(f) should return 'scorching' for temperatures over 95, 'cold' for temperatures under 40, 'hot' for temperatures in the range of 85–95, 'cool' for temperatures in the range of 40–65, and 'warm' for everything else. 11. Function bills(amount) should return a row vector with the numbers of $100, $50, $20, $10, $5, and $1 bills to add up to amount , using the smallest total number of bills. 12. Function twoDigitEven(n) should return a list of the positive even integers with two digits that are less than n. 13. Function nonprime(n) should return a list of the positive numbers from 1: n that are not prime. (You may use the MATLAB function primes(n), which returns a list of numbers from 1: n that are prime.) 14. Function oddwithcubeGT(n) should return the first odd integer whose cube is greater than n.

Fitting using simultaneous linear equations in MATLAB Here is are example linear systems of the form My=b with more equations than variables, so we can't solve them exactly. MATLAB's left division operator will give us the solution ŷ that minimizes the square of residual, that is, minimizes sum( (Mŷ – b) .^2). The key is to set up the equations we would like to have satisfied, and write them as Matrix * variables = constants , then use MATLAB to determine variables = Matrix \ constants. In the graphics lab here at UNC we build tracking devices for virtual reality (VR) that output x,y,z coordinates as they are moved around a room. Consider the following test for tracker accuracy: attach the tracker to one end of a wand and move it around while keeping the other end of the wand in a small depression on the floor. Thus, the tracker should describe points on a sphere. You get the following measurements, in the units of the tracker: x y z -11.61 -15.55 4. 9.22 -10.07 14. -5.18 -2.47 19. -17.49 -3.79 11. -0.97 17.69 7. -17.63 5.29 10. 1.07 -14.48 12. 5.85 -1.82 19. 1.65 16.01 10. -20.28 -0.87 7. -13.87 -14.42 3. -16.69 1.87 12. -10.89 7.51 15. 17.41 -9.54 7. 13.12 -4.19 15. 4.14 17.04 8. 15.92 3.49 13. -6.15 -17.97 3. 15.21 -12.14 7. -9.66 7.67 16. 1.02 -18.48 4. 7.66 -4.50 18. -5.08 -16.37 9. 20.91 -1.91 6. We can ask questions for different mathematical models of the tracker to find the best fit:

1. If we assume that the tip is at the origin, what is your estimate for the squared length of the wand? In this question, the model is that we have a sphere whose unknown squared radius R satisfies R = x^2 +y^2 +z^2 , where x , y , and z are the measured quantities. 2. Suppose the tracker uses three separate antennae for reading x , y , and z coordinates. We might suspect that these coordinate readings are not calibrated identically. Using the same data, fit a model R = Ax^2 +By^2 +z^2 with unknown parameters A, B, R, where A and B allow x , and y coordinates to be scaled differently than z.