This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 5 pts this
problem's
only
meant to
be an
exercise
for the
course
project so
unless
they
wrote
complete
nonsense
, just give
the 5 pts
and
proceed Assigned: October 16 Fall 2009 Due: October 23 2 GUI 6. (a) Create the following function Ichal.m. Note that Ichal has no output arguments.
It has 3 input arguments. The code assumes that the 3rd input argument is a handle
to a graphics object. The code increments, by 1, the value of application data named NumberTimesPressed that is stored (hidden) in that graphic object.
begin code function Ichal(Esrc,EData,PBH) N = getappdata(PBH,’NumberTimesPressed’); setappdata(PBH,’NumberTimesPressed’,N+1);
end code The next piece of code 0 creates a pushbutton,
0 sets the callback function of the pushbutton to be the function Ichal o and creates application data (with setappdata) within the pushbutton.
begin code 1 close all
2 F = figure;
3 Fpos = get(F,’position’); 4 set(F,’position’,[Fpos(1:2) 500 50]);
5 str = [’Push Me’]; 6 pr = uicontrol(’style’,’pushb’,’pos’,[10 5 8O 30],’string’,str);
7 set(pr,’callback’,{QIchal pr}) s setappdata(pr,’NumberTimesPressed’,0);
end code Run that code, and then experiment with the pushbutton. In order to see how many times the button has been pressed, “ask" the pushbutton for the appdata variable NumberTimesPressed using the following command.
begin code getappdata(pr,’NumberTimesPressed’)
end code 1 
Now, push the button a few times, run the previous command, and check the answer. Reset the counter with
begin code setappdata(pr,’NumberTimesPressed’,0);
end code (b) In this part, modify the previous code to create two pushbuttons. Start with the following
code: begin code % create a new figure
% handles to both pushbuttons 1 F = figure;
2 pr = zeros(2,1);
3 for i=1:2 4 istr = int2str(i); 14 of 28 Assigned: October 16 Fall 2009 Due: October 23 5 str = [’Push Me (’ istr ’)’]; s pr(i) = uicontrol( ’style’ , ’pushb’ , . . . 7 ’pos’ , [10+(i—1)*1oo 5+(i—1)*40 80 30],...
8 ’string’ ,str) ; 9 set (pr(i) , ’ callback’ ,{QIchal pr(i)}) ; 10 setappdata(pr(i) , ’NumberTimesPressed’ ,0) ; 11 end end code Now, experiment with the buttons, and determine how the code works. For example, push
the buttons a few times (different number for each button) and see how many times each
has been pressed by executing begin code 1 getappdata (pr ( 1) , ’ NumberTimesPressed ’)
2 getappdata(pr(2) , ’ NumberTimesPressed’)
3 whos end code There are three things to notice 0 There is no variable in the Matlab workspace named NumberTimesPressed. 0 Each pushbutton uses its appdata, namely a variable identified by NumberTimesPressed,
to store the value of it's counter. o The appdata variables associated with one pushbutton are independent of the appdata
of the other pushbutton. Answer the following: i. Comment on what the commands in lines 68, 9, and 10 do in the previous piece of
code. ii. Why don't we have to write two individual callback functions for the two pushbuttons
in this case? Solution: 0 — Lines 6—8 create a uicontrol object, with pushbutton style at a specified
position of specified size, puts the string str on the button, and gives the
handle for this push button.  Line 9 sets the callback function for the push button. — Line 10 initializes the variable NumberTimesPressed in the appdata of the
pushbutton, using setappdata. c We didn't write two callback functions for these pushbuttons because the action
taken when these pushbuttons are clicked are same, i.e., the counter is incre
mented by 1. These counters (i.e., the variables which keep track of how many
times each pushbutton has been clicked) are different but the action is the same.
The handle of the pushbutton is embedded in the callback itself, so the callback
code has direct access to the pushbutton that has instigated the call. 15 of 28 Fall 2009 Due: October 23 Assigned: October 16 By passing the right pushbutton handle to the callback function, we distinguished
between two push buttoons. If you look at the callback function, the handle for
the push button is an input argument. So, we pass the handle to the function.
Then, in the callback function, the variable NumberTimesPressed in the handle
is incremented. Recall from the lecture that for a cell array callback at the time
when the event which generates the callback occcurs (button press) MATLAB
creates two variables: the source of the event and data associated with the
event. These are automatically first two variables in the declaration line of the
callback function. Varaibles after the second one in the call array callback are
the varaibles or parameters the user passes to the callback function. At this
point time event data is often empty. It seems to a place holder for some other
purposes. But it should still be included in the declaration line. 16 of 28 Assigned: October 16 Fall 2009 Due: October 23 3 Recursion 7. Suppose that 11 students are forming a new student group. Because they all want to gain leadership experience, the all agree to elect the club president using a random methodology. One selfish
student proposes the following algorithm: 0 The students gather in a circle. 0 Starting at a particular point and proceding in the clockwise direction, the students are
numbered 1 through 11. 0 Starting at position number 1 and continuing clockwise, every other student is eliminated. 0 Each eliminated student is removed from the circle and the counting continues until just one
person is left. Of course, this algorithm is not random at all, and the selfish student knows where to stand
in order to be elected. To better understand this devious election process, we will consider the
specific case of 10 students. First, the students line up in a circle and are assigned numbers based
on their position, as shown in Figure 6. Figure 6: Initial arrangement of students. Then, every other student is eliminated in clockwise order, as shown in Figure 7. 10 2 Figure 7: Students eliminated in first round are higlighted with boxes. Since the first round of elimination ended by eliminating number 10, we begin the next round with
number 1 again (see Figure 8). Figure 8: The second round of elimination begins with student 1 again. 17 of 28 Assigned: October 16 Fall 2009 Due: October 23 Note that the second round ended by eliminating number 7, so number 1 should be the next
student eliminated. This happened because there were an odd number of students in round 2.
Thus, for round three we must start with student number 9 (see Figure 9). Figure 9: Round three must start with student number 9. Again, round three had an odd number of students, so we must shift everyone and start round
four with student number 5 (see Figure 10). Figure 10: The final round must start with number 5. Finally, we see that for a group a 10 students, the cheating student will be elected president if he stands in position number 5. For a general arrangement of n students, we can find the position
of the winner using the following rules: 0 If there is only one student left, he must be the winner. This is the “base case". 0 The person standing in the winning slot at round 1 will also be standing in the winning slot
for each successive round. 0 If a round has an odd number of participants, we must begin the next round where we left
off. In our example above, we used this rule to explain why round three begain with number
9 and round four begain with number 5. Write a function called elect that solves the problem described above using recursion. The input
to this function will be a rowvector of distinct integers. The output will be the label of the elected
student. If the rowvector only has one element, we've clearly reached our base case. If there
is more than one element, we eliminate every other element. Then, if the length of the input
was odd, we must shift the last remaining student to the first position before recursively calling
elect again. Display your function in your solutions using the type command and then test your
function by including the following lines of code in your script file: begin code e1ect(1:7)
e1ect(1:10)
e1ect(1:57) end code 18 of 28 Assigned: October 16 Fall 2009 Due: October 23 Solution: In words, we solve the election problem by eliminating every other person and
then recursively calling elect on the smaller array. However, if the number of remaining
participants was odd, we move the last entry to the beginning of the array to ensure that the next round will begin with that student. Here is one implementation of elect.m:
begin code 1 function winner = elect(positions)
2 n = length(positions);
3 if n ==
4 winner = positions;
5 else
6 remaining = positions(1:2:end);
7 if rem(n,2)==
8 winner = elect(remaining);
9 else
10 winner = elect([remaining(end),remaining(1:end—1)]);
11 end
12 end
end code
To verify that it works, we try the following inputs:
begin code
1 >> elect(1z7)
2 ans =
3 7
4 >> elect(1:10)
5 ans =
6 5
7 >> elect(1z57)
8 ans =
9 51 end code 8. Suppose that a and b are positive integers. If d is a divisor of a and b, then there exist integers u and 1) such that a = ud and b = vd. Review the help documentation for the built—in MATLAB
function rem. If b 7é 0, and rem(a,b) returns 7', then 7' < b and there is an integer q such that
a = qb + 7'. We can use substitution to write a and b in terms of d as follows: a=ud, b=vd => ud=q(’ud)+7' => r=(u—qv)d. Since u, v, and q are all integers, (u — qv) must also be an integer, so d is a divisor of r as well.
We can use this information to find the greatest common divisor of a and b using recursion. The
first hint is to consider what happens when rem(a,b) returns zero. When this happens, a = db
for some integer d, so b is a divisor of a. Since b is the greatest possible divisor of b, we know
that b is the greatest common divisor of a and b. Now, for the second hint, assume that a > b. In the preceding discussion, we saw that gcd(a, b) = 19 of 28 Assigned: October 16 Fall 2009 Due: October 23 gcd(b,rem(a, b)). We know that rem(a, b) < b, so we can keep calling rem(a, b) and switching the roles of a and b until we reach our base case (rem(a, b) =2 0). Using the hints above, write a recursive function gcd that takes two inputs, a and b. Both inputs
must be positive integers and we must ensure that a >= b. (We can check this in our code and
switch the two inputs if a<b.) The output of this function is the greatest common divisor of a and b. In your solutions, display your function using type and execute the following commands to verify
that it works: begin code 1 gcd(15,9) 2 gcd(91 ,7) 3 gcd(483,51) 4 gcd(10395,209) end code PWN @403“ 10 11 12 13 14 poo @4030 10 11 Solution: Here is one implementation of gcd.m:
begin code function g = gcd(a,b) % Ensure a>=b if b>a
tmp = b,
b = a;
a = tmp;
end if rem(a,b) == 0
g=bs
else
g = gcd(b,rem(a,b));
end
end code To verify that it works, we try the following inputs: begin code >> gcd(15,9) ans =
3
>> gcd(91,7)
ans =
7
>> gcd(483,51)
ans =
3
>> gcd(10395,209)
ans = 20 of 28 Assigned: October 16 Fall 2009 Due: October 23 12 11
end code not 9. At the beginning of time, monks at a Hindu monastery were given the task of completing the assigned Towers of Hanoi problem. In their temple are three towers. On one rests a stack of golden disks,
each disk slightly smaller in diameter than the one beneath it. The other two towers are empty.
The monks must move all the golden disks from the first tower to the third. The second tower
may be used to hold disks temporarily. The golden disks may only be moved one at a time. Due
to the fragility of gold, 3 larger disk cannot be allowed to rest on top of a smaller disk. When the
monks complete their task, the world will end. Tower 1 Tower 2 Tower 3 (3) Suppose that S(N) is the minimum number of steps needed to move N disks from one tower
to another (as illustrated in Figure 11.) Now, consider N+ 1 disks instead of N disks. ln Stan: Finish:
(S(N) steps later). Figure 11: Moving disks from one tower to another. Configuration 1 of Figure 12, disk N+ 1 is shown in gray at the bottom of the initial stack.
We have agreed that S(N) is the minimum number of steps to get from Configuration 1
to Configuration 2. It then takes one step to get from Configuration 2 to Configuration 3.
Finally, it takes S(N) steps to get from Configuration 3 to Configuration 4. The number of steps taken to get from Configuration 1 to Configuration 4 is equal to
S(N+ 1). However, we have kept track of this number of steps in terms of S(N). It follows
that we have a relationship between S(N + 1) and S(N). o What is the relationship between S(N + 1) and S(N)? (b) Write a recursive Matlab function towersteps that accepts N (the number of disks) as
input, and that returns S (the number of steps to transfer N desks between towers) as
output. If N = 1, then obviously S should be set equal to 1. If N is greater than 1, use the 21 of 28 ...
View
Full Document
 Summer '08
 Patzek

Click to edit the document details