DOC PREVIEW
Duke CPS 111 - Homework 4

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CPS111- Homework 4Due on February 12, 2008Questions may continue on the back. Please write clearly. What I cannot read, I will not grade. Typed homework ispreferable. A good compromise is to type the words and write the math by hand.1. This problem is partly a way to explore state trajectories, and partly a programming exercise. Do your work next to acomputer running Matlab.As usual, programming tips are typeset in a different font and with a wider margin, as for this paragraph, so expert program-mers can skip this text. However, make sure you read all other text carefully. Short, regular homewor k sentences are oftensandwiched between long blocks of programming tips.Write a Matlab function with the following headerfunction trajectory(F, x0, N, fig)that plots in figure fig the first N steps, n = 0, . . . , N − 1, of the free evolution of the dynamic systemx(0) = x0x(n + 1) = F x(n)starting from state x0. Your code should check that the input argument F is a 2 × 2 matrix, and abort execution with an errormessage otherwise. Also check that x0 is a column vector with two entries.To check that a matrix A is, say, 2 × 3 you put an if statement in your code. Every if must have a matching end keywordafter the code that is to be executed if the clause in the if is true:if any(size(A) ˜= [2 3])error(’A must be 2 by 3’)endThe clause is anything after the if keyword and before a new line, a comma, or a semicolon. So in the code above theclause isany(size(A) ˜= [2 3])Let us analyze this clause. The Matlab function size takes a matrix, in this case A, and returns its size, that is, the numberof rows and columns of A, in a 1 × 2 matrix. As an example, say that the matrix A you pass when you call your functionis 3 × 3. Then size returns the row vector [3 3] (try this in Matlab, with some random matrices you create). The string˜= means “not equal to”. The result of comparing two matrices of equal size to each other with the ˜= operator is anothermatrix of the same size. Each entry of this result is 1 if the result of applying the operator to the corresponding entries inthe two input vectors results in a true statement, and 0 otherwise. In the example, the comparison[3 3] ˜= [2 3]results into the vector [1 0], because 3 (the first entry of [3 3]) is not equal to 2 (the first entry of [2 3]), hence the 1 inthe result (the “not equal” is true), but 3 (the second entry of [3 3]) is equal to 3 (the second entry of [2 3]), hence the 0in the result. Again, try to type[3 3] ˜= [2 3]directly to the Matlab prompt, followed by the return key, and check that you get [1 0], and that you understand why. Makethis into a habit: try everything in Matlab. This is perhaps the greatest advantage of using an interpreted language likeMatlab, rather than a compiled one, like, say, C: in an interpreted language, you can type something to the interpreter andget the result back immediately.The Matlab function any then takes the vector [1 0] resulting from the comparison and returns 1 if any entry of the vectoris nonzero, that is, if any of the comparisons is true. Otherwise, any returns 0 (so it returns 0 if and only if all entries of itsargument are equal to 0). In summary the if clauseCPS111 — Duke — February 4, 2008any(size(A) ˜= [2 3])returns 1 if and only if at least one of the dimensions of the matrix A is wrong. In that case, the code of the if statement,that is, anything between the clause and the matching end is executed.The Matlab function error then prints whatever message you specified in its argument (you need the parentheses and thesingle quotes as above) and aborts execution of the function.If the argument fig is omitted when the function trajectory is called, then your code should use the default value of 1for fig.Matlab has a built-in variable nargin that contains the number of input arguments to the current function, that is, the numberof arguments with which the function is being called. For instance, if you calltrajectory([1 0; 0 1], [2; 1])then Matlab does not complain about the fact that your definition of trajectory expects four arguments rather than two.Inside the function, the variable nargin is automatically set to be equal to 2. So you need to check the value of this variablein an if statement in your function definition, and set the value of fig if needed.Your plot should have a dot at every state point x(n) on the trajectory, and a line segment connecting consecutive points.This will give you plots in the style of Figure 2 in Chapter 3 of the supplementary class notes. However, do not put numbersnext to the dots, and there is no need to display the state matrix.For efficiency, pre-allocate a 2 × N array of zeros with the Matlab function zeros, to contain the entire trajectory. Let’s saythat the array is called x. The first column of x will eventually contain the two values in x(0), the second will contain thosein x(1), and so forth. This array is filled inside a for loopfor n = 1:N% Your code hereendthat fills column x(:, n) of x appropriately. Make sure that x(:, 1) is also given a proper value somewhere. After theloop, plot the second row of x, that is, x(2, :) versus the first row x(1, :) with the Matlab plot function. Do helpplot to find out the details. Plot this function twice: one with dots (third argument to plot set to ’.’), and one withline segments (third argument set to ’-’). To prevent Matlab from erasing the first plot as it draws the second, insert theinstructionhold onbetween the two plot instructions. You should not assume that the figure in which you plot is clean. For instance, it mayhave the hold switch turned on from a previous run of your code. To reset the figure before you plot on it, use the Matlabclf command.Draw two solid line segments to show the two coordinate axes.To plot these two line segments, you need to know the minimum and maximum x1and x2coordinates, so you know howlong to make the segments. After plotting the state trajectory, you can store into a variable, say, xlim, the smallest andgreatest horizontal coordinates of the plot as follows:xlim = get(gca, ’XLim’);Of course, there is also a ’YLim’. The built-in variable gca points to the current graphic axes. If you just type get(gca),you can see the list of property-value pairs associated to the current axes (try this). Any value in this list can be readindividually by a get instruction, and written to by a set instruction. Axes are contained in a figure


View Full Document

Duke CPS 111 - Homework 4

Download Homework 4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework 4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?