Homework 2 FAQ

Q1: When asking us to plot using matlab, should we attach a print out of the Matlab plot to the rest of our hw, or should we just write down the sequence of commands used to create our plots?

Q2: I can make the x vector, but I can't get matlab to compute the equation and store it in a y vector. It says something like:

??? Error using ==> mpower
Matrix must be square.

What do I do???!!!??

Q3: I'm confused - in the homework assignment, do we type the vectors in matlab with the curly braces, or square brackets?

Q4: On 3.1.5 was a=-2 supposed to be x=-2? I only ask because my plot comes out as a horizontal line at about y=2 and i know thats not right. Here is my code for that problem:

x=-100:.1:100;
f=((x.^2)-4)/(x+2);
plot(x,f)
title('Some title')
xlabel('X Axis')
ylabel('Y Axis')

Q5: I don't understand what x=x1,x2 means on problem 3.3.9 or how to integrate into Matlab and make the problem compute correctly. It give me an undefined function or variable error.

Q6: when you ask to plot using subplots for the derivative problems, what command do I use to get the subplots to show? I looked in the help section but couldn't find an example or explanation that really fit what we're doing.

Q7: if i wanted to plot (-100,-1) and then (-1,100) without including -1, how would i do that?


A1: please include the plots as well with the homework. You may have more than one plot per page to save paper, just label in the title the plot, and refer to the appropriate label for a problem. Example: a plot which has the title 'problem 3.2.1 : x vs. y'


A2: remember to use the . operator when performing multiplication or division involving two variables or raising a variable to a power (example: x.^2, x.*y, or x./y). You do not need to use the . operator when multiplying a scalar, or subtracting or adding numbers and vectors (example: 5*x, or x+y, or x+5). See the plotting examples for m file examples of this.


A3: Square brackets. The curly braces were merely being consistent with mathematical notation to the literature. But in matlab, curly braces have a specific meaning. If you used curly braces you would be getting errors. The following is from the matlab help documentation:

{ } Braces are used to form cell arrays. They are similar to
brackets [ ] except that nesting levels are preserved.
{magic(3) 6.9 'hello'} is a cell array with three elements.
{magic(3),6.9,'hello'} is the same thing.
{'This' 'is' 'a';'two' 'row' 'cell'} is a 2-by-3 cell array.
The semicolon ends the first row. {1 {2 3} 4} is a 3 element
cell array where element 2 is itself a cell array.

Braces are also used for content addressing of cell arrays.
They act similar to parentheses in this case except that the
contents of the cell are returned.

Some examples:
X{3} is the contents of the third element of X.
X{3}(4,5) is the (4,5) element of those contents.
X{[1 2 3])} is a comma-separated list of the first three
elements of X. It is the same as X{1},X{2},X{3} and makes sense
inside [] ,{}, or in function input or output lists (see LISTS).

[ ] Brackets are used in forming vectors and matrices.
[6.9 9.64 SQRT(-1)] is a vector with three elements
separated by blanks. [6.9, 9.64, sqrt(-1)] is the same
thing. [1+I 2-I 3] and [1 +I 2 -I 3] are not the same.
The first has three elements and the second has five.
[11 12 13; 21 22 23] is a 2-by-3 matrix. The semicolon
ends the first row.


A4: In response to your questions, 

1) problem 3.1.5 yes it should be x=-2 as in that's where the discontinuity is, and the question is to explain why.  It has to do with the denominator going to zero.  Check your old calc/precalc books for limits and continuity or discontinuity, or look on mathworld, or another similar site (google is our friend:)  

I see in the code you sent the reason it's coming out with one number: don't forget the '.' before the division ('/'), as you want to divide element-wise (as opposed to as a vector operation). 


A5: Just compute this by hand.  You can do this symbolically in matlab using the sym command, but I didn't go over that and don't expect that of you.  If you are interested in computing the answer symbolically in matlab (ie you end up with some combination of x1 and x2), the help documentation about symbolic manipulation is pretty good (help sym).  Once you create a symbolic object you can have matlab simplify equations and perform multiplications symbolically (ie not with a specific number, just with symbols like x1, x2).


A6: See handouts section, example 3 plotting code.  I introduced it in class briefly and discussed it, but I think the subplot command is tricky at first, it's one of those things that makes sense once you do it.


A7: you could create a stepsize which will avoid that point, or you could create two arrays which don't quite include -1, then put them together...x=[-100:-.1, .1:100];