Lab D deals with Chapters 6 and 7 of the... Chapter 6 of Gilat is probably the most difficult lab... =======================================================

advertisement
Lab D deals with Chapters 6 and 7 of the MATLAB textbook.
=======================================================
Chapter 6 of Gilat is probably the most difficult lab since it involves programming (primarily
conditional statements and loops)
#7: (for/end loops and nested loops) You should use 2 for/end loops: one nested inside the other. The
basic structure of a for/end loop is on pp192-194 of Gilat. There is a useful example on pp200-202 of
using nested loops to create a matrix-students just have to replace the conditional statements with the
formula. Regarding the formula, note that A(i,j) is the sum of the first j elements of the previous row
(meaning the : notation from pp44-45 may be useful)
#19: (nested loops and conditional statements) A good tip here is to start with c=1:50, then b=1:c
(since c is the largest side), then a=1:b (to avoid duplicates a,b,c and b,a,c). Once the condition is
satisfied, either display a, b, and c immediately (as a vector) or add a new row to a matrix A consisting
of the triples (by creating a new variable n which indicates which row is to be built next and increasing
it each time the condition is met).
#24: (loops and nested conditional statements) After starting the loop at n=1, use conditional
statements to determine IF sum(1:n) is less than 100 AND greater than 1000. If so, the easiest way I
found to check the digits is to extract the hundreds digit (see "fix" on p15) and test whether the sum 111*hundreds_digit is 0. If all of these are true, break the loop and display the required statements and
values; otherwise, continue the loop to the next value of n. Refer to pp184-186 for if statements
(***pay particular attention to the use of the double-equals (==) for testing equality!), and p179 for the
"AND" syntax (which should also be doubled (&&) within an "if" statement).
=======================================================
Chapter 7 of Gilat covers functions. Note that "inputs" and "outputs" can be several variables
(think of it as a single vector whose components correspond to each variable).
#2: (anonymous functions) pp231-233 of Gilat. Define the function in the M-file using the @
notation. Go over the example on p233 (especially noting the element-by-element definition needed
when plotting). Remember that all plots MUST have titles and axes labels (see Gilat p144, with
formatting details on pp145-148. Also refer to pp156-157 for polar plots).
For the remaining problems, you are required to create separate function files using the
"function" command. Refer to pp223-229, paying particular attention to the example file near
the top of p229 which is used (in their M-file) midway through the page. The name of the
function file must agree with the name given on the first line (in this example, "loan.m")-Matlab
defaults to this name, so don't change it! If you want, you can click on "New" then "Function"
under the home tab and Matlab will give the default format (you just need to replace everything
with the appropriate names and variables)
#12: This is not a direct calculation, so you should begin by listing the steps (subtract A-B and C-B to
get vectors, then use the dot product formula). Remember that you calculated the angle between vectors
in Lab 3 (g5c3p090x20). Also, the method explained in #19 does NOT depend on the components
(meaning you can use it for 2-d OR 3-d vectors!)
#14: The formula for cross product is on p669 of Stewart (you will be using it soon enough!). NOTE
that the formula only works for 3-dimensional vectors, so you must start the function with the condition
IF length(u)=2, u(3)=0 and v(3)=0. Once this condition is tested, you can use the formula.
#19: Since this function has no output, it should start with "function NAME(INPUT)". To graph the
triangle, the easiest thing to do is plot
[A(1) B(1) C(1) A(1)] vs [A(2) B(2) C(2) A(2)] (The x-coordinates and y-coordinates respectively as
you move around the triangle starting at point A).
For the center and radius of the circumscribed circle, there are complicated formulas hidden in the
Wikipedia page for "Circumscribed circle" and the Solutions' Manual uses angle bisectors, but the
easiest thing to do is let MATLAB handle it symbolically! Since all three points are on the circle, make
3 equations: (A(1)-x0)^2 + (A(2)-y0)^2-r^2 (=0) and the corresponding equations with B and C, then
solve these equations for x0, y0, and r! (refer to p363 in Gilat for a review of solving a system of
equations symbolically). Once you find x0, y0, and r, plot the parametrized circle x=x0+r*cos(t),
y=y0+r*sin(t) (remember to "hold on" so you don't lose the triangle!)
#21: You can refer to Section 13.4 in the Stewart text for the formulas to convert Polar Coordinates to
Cartesian, but you can review from 151 (and PHYS 218) that this is what is done when given (for
example) a Force vector with given magnitude at a given angle.
One final note: When you publish your script files, the separate function files do not display. You
need to print your function files as well and attach them to the corresponding published
printouts. This can be done in one of two ways:
1) Since .m files are just text files, you can print them directly using xprint in the calclabs or
transferring the .m file via email, flash drive, or FileZilla to a local machine for printing.
2) You can publish the function files. The output will be an error, but the function will appear in
the pdf file. Print the resulting pdf as usual.
Download