The following commands should do the trick for you:
tut1 = [1 2 3 4 5;6 7 8 9 10; 0 9 0 5 3] tut2 = [15 14 13 12 11; 10 9 8 7 6; 5 4 3 2 1]
The command size(tut1) and size(tut2) should both yield something like:
ANS =
3. 5.
a1 = zeros(10) a2 = zeros(10);
The first command will display the entire 10x10 matrix of zeros that it creates. The second will work silently. The difference is in the semicolon at the end of the command.
The vector [1 3 5 7 9] is created.
The following MATLAB command should do the trick:
evens=[6:2:24]
abcwhich should show you the contents of the matrix abc.
The matrix abc should consist of the entries:
3. 6. 9.
15. 12. 9.
18. 15. 24.
fdj+3 fdj-6 fdj/2.in each case the indicated operation should be performed on each entry of the matrix fdj.
The matrix fdj+3 is
4. 5. 6. 8. 7. 6. 9. 8. 11.The matrix fdj-6 is
-5. -4. -3. -1. -2. -3. 0. -1. 2.The matrix fdj/2 is
0.5 1.0 1.5 2.5 2.0 1.5 3.0 2.5 4.0
abc = 1:10; def = 5:14; ghi = 3*abc + def
The first creates the vector abc = [1 2 3 4 5 6 7 8 9 10]
The second creates the vector def = [5 6 7 8 9 10 11 12 13 14]
The third creates the vector ghi = [ 8. 12. 16. 20. 24. 28. 32. 36. 40. 44.]
abc = [1 2 3 4;5 6 7 8]; def = [4 3 2 1;0 -1 -3 3]; abc + def
The matrices abc and def are both 2x4 matrices, so they can be added together. The componentwise addition results in the matrix
ANS =
5. 5. 5. 5.
5. 5. 4. 11.
Here is an example:
A = <1 2;3 4;5 6>;
B = <2 3;4 5;6 7>;
C = <1 1;2 2;-1 -1>;
A.*B + C
ANS =
3. 7.
14. 22.
29. 41.
abc = [1 2 3 4]; def = [2 5 4 3]; abc * def
The operation requested is a traditional matrix operation, which would require the vector def to be a column vector in this situation. (NOTE: It would also work if abc were a column vector while def remains a row vector. Try it and see what happens.)
abc = [1 2 3 4]; def = [2 5 4 3]; abc * def'
The apostrophe causes MATLAB to take the transpose of the vector def causing it to be tranformed into a column vector.
A*ones(1,123)'which multiplies the vector A with the column (after the transpose) vector made up of 123 ones. The result of this product is the total of the 123 entries in A.
To explore this technique create a row vector with 10 entries, and use the idea above to direct MATLAB to compute the sum of the entries.
a=[1 2 3 4 5 6 7 6 5 4]; a*ones(1,10)' ANS = 43.
[1:100]*ones(1,100)'
ANS =
5050.
[2:2:1001]*ones(1,500)'
ANS =
250500.
There are dozens of these. I am sure you found some.
The commands for the first plot are:
x = -5:1:5; y = x ./ (1 + x.^2); plot(x,y)
The commands for the second plot are:
x = -5:0.1:5; y = x ./ (1 + x.^2); plot(x,y)
You should see an imporovement in the resolution with the increased number of grid points but you may also notice a significant increase in the time taken to produce the graph.
The plot below should create the baseball diamond along with the foul ball lines extending into the outfield. Other possible enhancements could include small squares for the bases, detailed marking of the baselines, running coach stations, the batter's box and pitchers mound, as well as curves depicting the separation of the dirt and grass in the infield, etc.
foulx = [ -150 0 150]; fouly = [ 150 0 150]; baselinex = [0 40 0 40 0]; baseliney = [0 40 80 40 0]; plot(foulx,fouly,baselinex,baseliney)
theta = 0 : 0.05 : 2*pi;
hold on
axis('square')
plot(cos(theta),sin(theta))
This set of commands creates a circle of radius one centered at the origin. The ideas here are barrowed from the notion of polar coordinates in calculus.
When you are finished using the image, type hold off. To see the effect of the axis('square') statement, enter the MATLAB command axis('normal') and redraw the graph.
You will notice that the circle appears to be stretched. This is because the x and y graphs are on different scales. The axis('square') command forces the graph to use the same scales on both axes.
theta = 0 : 0.1 : 2*pi; r = sin(3*t); plot(r .* cos(theta), r .* sin(theta))Note the componentwise multiplication used in this problem, indicated by the dot-asterisk (.*) notation.
You should see a three petaled rose-like figure appear in the plot.
As an optional exercise (I'm a mathematician after all) guess what happens if the sin(3*t) term is changed to sin(4*t). Then test your guess by creating the graph.
You will see an eight (surprise!!) petaled rose-like figure. You may have to increase the resolution (decrease the stepsize 0.1) to get a good picture of the figure.
You are on your own for this one.
The MATLAB commands for this task are:
x=[-10:0.2:10]; y=[-10:0.2:10]; [X Y] = meshgrid(x,y); Z = cos(2*(X+Y)); surf(X,Y,Z)
ones(1,6)*(a^10*p)
This statement sums up the populations in each age class, giving us the total population over all age groups.
This is a trick question, the answer is the expression above.
The idea here is to divide the population in each age class by the total population. This quotient is a vector giving the proportion of the total population in each age class. The MATLAB command to do this, assuming p is the vector containing the current age class structure, is:
proportion=p./(ones(1,6)*p)If you want to do this some years down the road, say 10 years, then you can replace each occurence of the vector p with (a^10*p).
To investigate this question we must update the entries in the A matrix to reflect the hypothetical introduction of the new drug. Only three entries inthe matrix A need be changed, so rather than recreate A from scratch we will make the three individual changes.
The first change is that the birth rate for two year olds must be changed to 2.3. Since the birth rate for two year olds is found in the first row, third column of the matrix A, this change can be effected by the MATLAB command:
A(1,3)=2.3;
Make the remaining two changes in A, changes which reflected the lowered survival rates for age classes 3 and 4.
Begin with the same initial population (all 10's) as before. Does the population seem healthier (whatever that means) under the drug regime? How does the drug effect the age distribution of the population?
If fecundity rates remain unchanged, what survival rates are needed for our critter population to remain stable? What proportion of a stable population falls into each age class?
It is fun to see how populations evolve over time. Toward this end it is possible to create a three dimensional graph showing the evolution of a Leslie population over time by creating a matrix whose columns are "snapshots" of the population at various times under study.
Suppose P is the initial population (all 10's in our example). We will use the matrix TimePop to be the matrix whose columns will contain the population at given points in time. Since the first population we are interested in is the initial population, we will set the first column of TimePop to the initial population using the MATLAB command
TimePop=P;We will use a second matrix called PP to hold the most recent population computed. Thus, PP should start off holding the initial population
PP=P;The task now facing us is to generate the population vector for the next time period, add this column vector to the matrix TimePop and then do the same again, moving ahead yet another time step. This can be achieved by executing the following MATLAB commands repeatedly:
PP = A * PP; TimePop = [TimePop PP];The first command moves the population vector PP forward one time step. The second command adds a new column to the TimePop matrix. This new column contains the most recently computed population.
Using the up-arrow key, repeat the line above 15 to 20 times, creating a TimePop matrix with many population snapshots.
The evolution of the critter population over time can be viewed by creating a three dimensional surface plot of the array TimePop. To create this surface plot enter the following MATLAB commands and note the plot which is generated. Pay special attention to the creation of labels and the colorbar.
surf(TimePop)
view(0,90)
colormap(jet)
colorbar
xlabel('Year')
ylabel('Age Class')
The colon (:) notation we discussed earlier is a convenient way to create large arrays where the entries are evenly spaced. A second use of the notation is to extract parts of existing matrices. In particular, a single colon is a reference to every row or every column in a matrix, depending on its position.
For example, the notation A(2,:) refers to every column of the second row of the matrix A, or, said another way, A(2,:) is the entire second row of A.
Similarly, B(:,5) refers to the entire fifth column of the matrix B.
plot(TimePop(3,:))
Using calculators it is difficult to efficiently deal with Leslie models consisting of more than about ten age classes. Using MATLAB, however, it is relatively easy to deal with populations with dozens of age classes. Human populations are generally modeled with 101 age classes, allowing for ages between 0 and 100. It is also much easier to move far into the future, spotting long term trends.
In this section we discuss modifications to the Leslie Population Model which describe a slightly different situation than that modeled by Leslie. This section introduces no new MATLAB skills and is present for those who want to undertake the challenge of creating a new model. I will describe the mathematics of the new model, but will leave it entirely up to you to implement the model in MATLAB. A knowledge of traditional matrix multplication, the non-componentwise variety, will be helpful.
In the Leslie model only two things can happen to individuals in an age class; they can die or they can move onto the next age class. In the model presented here we allow for a third option, remaining alive and in the same age class.
Given that it is possible to remain in the same class for several time steps, it is probably best to leave off the word age when describing these classes.
As far as I know, this version of the Leslie model has no well-known name, so we get to name the model for the purposes of this tutorial. Feeling singularly uncreative at the moment, I choose to call it Model X.
Model X requires three sets of parameters. Denote the fecundity of class i with the symbol f(i), the proportion of class i moving on to the next class at each iteration with the symbol s(i) and the proportion of each class which survives but remains behind with the symbol r(i).
Proceeding in a fashion reminiscent of the Leslie model development, if we create a model with classes numbered from zero to N, and let P(i) denote the number of individuals in class i, then we can derive the following equations which give the values of the various P(i) for the next time step in terms of the P(i) values for the current time step.
Considering, first, the values of P(i) in the next time step
for classes numbered from one to N we see:
Next period's P(i+1) = s(i)*P(i) + r(i+1)*P(i+1)
This equation tells us that next year's class i+1 is made up
of those individuals from this year's class i who move up a
class as well as those individuals from this year's class i+1
who survive but do not advance in class. (a.k.a. flunking?) This
equation holds for values of i from zero to N-1
inclusive.
A careful look at this situation reveals that we have not yet specified the population of class zero in next year's population. Recall that members of class zero are either true newborns or previous members of class zero who do not advance for some reason.
The mathematical expression which gives the number of class zero
residents in next year's population is given by:
Next period's P(0)=
(r(0)+f(0))*P(0) + f(1)*P(1) + f(2)*P(2) + ... + f(N)*P(N)
Pay special attention to the first term of this equation. It does not
follow the pattern of the other terms.
Model X has a structure similar to the Leslie model. In fact, if P is a column vector whose N+1 entries are the populations of each class at some point in time, then there is an (N+1)x(N+1) matrix A so that the population next year can be determined by calculating the traditional matrix product
A*P