Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 8 worksheet In this worksheet we practice writing and understanding functions. 1. What are the outputs of the following? (Some give errors.) a. ??? is x^2 in the command window we evaluate myfun(10); b. ??? is x+2 in the command window we evaluate clear y = 3; myfun(y) c. ??? is x+2 in the command window we evaluate clear x = 3; z = myfun(x); d. ??? is x+2 in the command window we evaluate clear x = 3; myfun(y) e. ??? is y-10 in the command window we evaluate myfun(5); 2. Using lookfor, find the built-in function in Matlab to determine if an integer is prime. (Hard: Can you write a function that does this yourself?) 3. Using the function you found in the previous question, write a function primepi that takes as input x and as output returns the number of primes that are less than or equal to x. For example, primepi(7) should return 4, because 2,3,5,7 is a complete list of the prime numbers less than or equal to 7. 4. Plot primepi(x) using x = 10, 20, 30, ..., 1990, 2000. 5. (Challenge: Why is plotting primepi(x) this way so slow? Can you think of a faster way to plot the same thing? We will talk about computational complexity later.) 6. Write a function countarray.m that takes two inputs: an array A and a number a and as output returns the number of times that a occurs in A. For example, if A is [1 2 3; 2 5 2; 1 2 6], then countarray(A,2) is 4 and countarray(A,6) is 1. 7. Recall our “funnel sequences” which all started at 1000, then randomly chose an integer -1000 to 1000, call it x2, then randomly chose an integer -x2 to x2, and so on until reaching 0. Write a function that takes as input an integer n and as output returns a vector which is a funnel sequence starting at n (not starting at 1000).