type lifeordeath function r = lifeordeathx1, y1, x0, y0 % This function computes the number of illumatons % that arrive at the point x1,y1, assuming that the % death ray strikes 1 meter
Trang 1Error using ==> factorial
N must be a matrix of non-negative integers.
Here are two ways around this difficulty
Trang 4−1 −0.5
0 0.5
1
−1 0
Trang 5Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate RCOND = 4.189521e-18.
Trang 10U2 =
0.1240 0.4574 - 0.2848i 0.4574 + 0.2848i -0.2231 0.2831 + 0.2848i 0.2831 - 0.2848i
Ditto yet again (Note that we suppressed the output of the first command, and
took the maximum of the absolute values of the entries in each column of A3*U3
- U3*R3, in order to keep the data from scrolling off the screen.)
Trang 112 2 1/2] 1/2 d - 1/2 a + 1/2 (d - 2 d a + a + 4 b c) ]
Trang 13Since R n is the diagonal matrix with entries 1, 1, 1/2 n , we know that R ∞ is the
diagonal matrix with entries 1, 1, 0 Therefore M ∞ = UR ∞ U −1 So
Minf = U*diag([1, 1, 0])*inv(U)
Trang 14With the suggested alternate model, only the first three columns of the table are
rel-evant, the transition matrix M becomes M = [1 1/2 0; 0 1/2 1; 0 0 0] You can compute that the eventual population distribution is [1 0 0], and is inde-
pendent of the initial population
14.
The following commands create a JPEG file of the French flag The set command
has no effect on the JPEG file; we include it only to turn off the axis ticks in the figurewindow
image(A); axis equal tight
set(gca, ’XTick’, []), set(gca, ’YTick’, [])
imwrite(A, ’tricolore.jpg’)
Trang 15We now redefine the color of the leftmost third of the array and create a JPEG file ofthe Italian flag.
A(:, 1:100, 3) = zeros(200, 100);
A(:, 1:100, 2) = ones(200, 100);
image(A); axis equal tight
set(gca, ’XTick’, []), set(gca, ’YTick’, [])
imwrite(A, ’italia.jpg’)
Of course you will have to run the commands above yourself to see the flags in color.Then you can run the commands below to see a movie transforming the French flaginto the Italian flag
Trang 16Below is a series of commands that places Picard at the center of the arena, fires thedeath ray 100 times, and then determines the health of Picard It uses the followingfunction M-file lifeordeath.m, which computes the fate of the Captain after asingle shot
type lifeordeath
function r = lifeordeath(x1, y1, x0, y0)
% This function computes the number of illumatons
% that arrive at the point (x1,y1), assuming that the
% death ray strikes 1 meter above the point (x0,y0).
% If that number exceeds 50, a "1" is returned in the
% variable "r"; otherwise a "0" is returned for "r".
Trang 17dosage = 10000/(4*pi*((x1 - x0)ˆ2 + (y1 - y0)ˆ2 + 1));
The Captain is dead!
In fact if you run this sequence of commands multiple times, you will see the Captaindie far more often than he lives
Trang 18So let’s do a Monte Carlo simulation to see what his odds are:
x1 = 25; y1 = 25;
disp([’The chances of Picard surviving are ’,
num2str(simulation(x1, y1)/100)])
The chances of Picard surviving are 0.13
We ran this simulation a few times and saw survival chances ranging from 8% to 19%
(d)
x1 = 37.5; y1 = 25;
disp([’The chances of Picard surviving are ’,
num2str(simulation(x1, y1)/100)])
The chances of Picard surviving are 0.15
This time the numbers were between 10% and 24% Let’s keep moving him towardthe periphery
(e)
x1 = 50; y1 = 25;
disp([’The chances of Picard surviving are ’,
num2str(simulation(x1, y1)/100)])
The chances of Picard surviving are 0.46
The numbers now hover between 32% and 47% upon multiple runnings of this nario; so finally, suppose that he cowers in the corner
sce-x1 = 50; y1 = 50;
disp([’The chances of Picard surviving are ’,
num2str(simulation(x1, y1)/100)])
The chances of Picard surviving are 0.63
We saw numbers between 54% and 68% They say a brave man dies but a singletime, but a coward dies a thousand deaths But the person who said that probablynever encountered a Cardassian Long live Picard!
Trang 19(a)
Consider the status of the bank account on the last day of each month At the end of
the first month, the account has M + M × J = M(1 + J) dollars Then at the end
of the second month the account contains [M (1 + J )](1 + J ) = M (1 + J )2dollars
Similarly, at the end of n months, the account will hold M (1 + J ) ndollars So ourformula is
T = M (1 + J ) n
(b)
Now we take M = 0 and S dollars deposited monthly At the end of the first month the account has S + S × J = S(1 + J) dollars The next day, S dollars are added to
that sum, and then at the end of the second month the account contains [S(1 + J ) +
S](1 + J ) = S[(1 + J )2+ (1 + J)] dollars Similarly, at the end of n months, the account will hold S[(1 + J ) n + (1 + J) n−1 + · · · + (1 + J)] dollars Summing the geometric series, the amount T in the account after n months equals
Trang 22The results are all the same; you wind up with $72,795 regardless of where you enter
in the cycle, because the product
1≤j≤5 (1 + rates(j)) is independent of the order in
which you place the factors If you put the $50,000 in a bank account paying 8%, youget
analysis is more subtle here Set S = 10,000 At the end of one year, the account contains S(1 + r1); then at the end of the second year [S(1 + r1) + S](1 + r2), where
we have written r j for rates(j) So at the end of five years, the amount in the
account will be the product of S and the number
If you enter at a different year in the business cycle the terms get cycled appropriately
So now we can compute
Trang 23Not surprisingly, all the amounts are less than what one obtains by investing the inal $50,000 all at once But in this model it matters where you enter the businesscycle It’s clearly best to start your investment program when a recession is in forceand end in a boom Incidentally, the bank model yields in this case
We can use the expression (rand < 0.338) to compute whether Tony gets a hit
or not during a single at bat, based on a random number chosen between 0 and 1
If the random number is less than 0.338, the expression evaluates to 1 and Tony is
credited with a hit; otherwise, the expression evaluates to 0 and Tony is retired by theopposition
We could simulate a year in Tony’s career by evaluating this expression 500 times in aloop More simply, we can put 500 random numbers into this expression at once andsum the results, dividing by 500 to get his batting average for the year The following
function does just this, allowing more generally for n at bats in the year, although we
shall use only 500
Now let’s write a function M-file that simulates a 20-year career As with the number
of at bats in a year, we’ll allow for a varying-length career
type career
Trang 24function ave = career(n, k)
% This function file computes the batting average for each
% year in a k-year career, assuming n at bats in each year.
% Then it lists the average for each of the years in the
% career, and finally computes a lifetime average.
disp([’Best avg: ’, num2str(max(Y), 4)])
disp([’Worst average: ’, num2str(min(Y), 4)])
disp([’Lifetime avg: ’, num2str(ave, 4)])
Next we run the simulation
Trang 25The average for the five different 20-year careers is:
(ave1 + ave2 + ave3 + ave4 + ave5)/5
Our solution and its output is below First we set n to 500 in order to save typing in
the following lines and make it easier to change this value later Then we set up a zero
matrix A of the appropriate sizes and begin a loop that successively defines each row
of the matrix Finally, we extract the maximum value from the list of eigenvalues of
as x = r cos t, y = r sin t Then we clear any previous figure that might exist and
prepare to create the figure in several steps Let’s say that the red circle will haveradius 1; then the first black ring should have inner radius 2 and outer radius 3, andthus the tenth black ring should have inner radius 20 and outer radius 21 We startdrawing from the outside in because the idea is to fill the largest circle in black, thenfill the next largest circle in white leaving only a ring of black, then fill the next largest
circle in black leaving a ring of white, etc The if statement tests true when r is odd
and false when it is even We stop the alternation of black and white at a radius of 2
in order to make the last circle red instead of black, then we adjust the axes to makethe circles appear round
Trang 27Error using ==> mylcm
Arguments must be positive integers.
mylcm(’a’, ’b’, ’c’)
Error using ==> mylcm
Arguments must be positive integers.
ylabel ’Number of occurrences’
title([’Letter frequencies in ’ file])
set(gca, ’XLim’, [0 27], ’XTick’, 1:26, ’XTickLabel’, letters’)
Trang 28Here is the output when we run this M-file on itself.
We let w, x, y, and z denote the number of residences canvassed in the four cities
Gotham, Metropolis, Oz, and River City, respectively Then the linear inequalitiesspecified by the given data are as follows:
The quantity to be maximized is as follows:
• Voter support: 0.6w + 0.6x + 0.5y + 0.3z.
b = [50000; 40000; 18000; 0; 0; -10000; 0; 0; 0; 0];
simlp(f, A, b)
Trang 30We let w, x, y, and z denote the numbers of hours that Joe spends with the
quarter-back, the running backs, the receivers, and the linemen, respectively Then the linearinequalities specified by the given data are as follows:
The quantity to be maximized is:
• Personal satisfaction: 0.2w + 0.4x + 0.3y + 0.6z.
Trang 32That’s the voltage; the current is therefore
Error using ==> fzero
Function values at interval endpoints must be finite and real.
The problem is that the values of the exponential are too big at the right-hand endpoint
of the test interval We have to specify an interval big enough to catch the solution,but small enough to prevent the exponential from blowing up too drastically at theright-hand endpoint This will be the case even more dramatically in part (e) below