SPRING 2016 MATH 151 LAB C HINTS 1. Important General Guidelines

advertisement
SPRING 2016 MATH 151 LAB C HINTS
DUE: APRIL 15, 2016
1. Important General Guidelines
(1) When running commands inside loops and conditional statements, in general ALL
output should be suppressed by placing a semicolon at the end of the line. This is
ESPECIALLY important when creating large vectors/matrices (such as #34 with
n = 100)! Otherwise, your published pdf file will be HUGE and may even put your
lab account over memory quota.
(2) When publishing your script files, the separate function files do not display. You
need to print the function files as well and attach them to the corresponding published printouts. This can be done in one of two ways:
(a) Since .m files are just text files, you can print them directly using xprint in
the calclabs or transferring the .m file to a local machine for printing.
(b) 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.
2. Chapter #6
• Chapter 6 #6: (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-you just have to replace the conditional statements with the formula.
• Chapter 6 #7: (for/end loops and nested loops) Similar idea here. Note that
a(i, 1) = 1 and a(1, j) = 1, and for i, j > 1, a(i, j) = the sum of the element to the
left and the element above it.
• Chapter 6 #29: (loops, conditional statements, relational operators) Part (a) does
not require any programming-it is just like a Chapter 5 problem. Part (b) CAN
use loops and conditional statements (for an example, look at p210, but replace
the switch statements with if statements), but it is much easier to use a function
(an anonymous function is recommended) and relational operators as explained on
pp176 − 177. So you basically define the vector t, then the function Cp (t), then let
y = Cp (t) + Cp (t − 4). (t > 4)...etc. (Note that if t < 4, the remaining terms are
multiplied by 0. Also pay attention to the element-by-element multiplication).
• Chapter 6 #34: (loops, conditional statements, relational operators) Graphing another piecewise function as above, only this time if you use the relational operators,
1
2
DUE: APRIL 15, 2016
you also have to use logical operators. As an example, the condition 0 ≤ θ ≤ π/2
can be written as (0 <= theta & theta <= pi/2).
• Chapter 6 #35: (conditional statements) There are three parts to this problem:
(1) Define the grades. Again, do not worry about input since that causes publishing issues. Just define the vectors in the script file,
(2) Calculate average. Quiz average is easiest to use the sum and min commands
(recall p76). The final grade can be easily calculated by using Qavg*0.30 +
Midavg*0.20 + Final*0.20 + max(Midavg, Final)*0.30 (making the
higher one 50% as required),
(3) Assign letter grades: This is where the conditional statements come in. Remember you can assign strings to variables, so Grade=“A” is perfectly valid.
3. Chapter #7
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).
• Chapter 7 #1: (anonymous functions) pp231 − 233 of Gilat. Define the function
anonymously in the M-file using the @ notation. Refer the example on p233 (especially noting the element-by-element definition needed when plotting). Note that
all plots should have titles and axes labels (see Gilat p144, with formatting details
on pp145 − 148).
For the remaining problems, you are 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. Also note that the
name of the function file must agree with the name given on the first line (in this example,
”chp7one.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).
• Chapter 7 #12: You can do this by hand: 1) create vectors a and b between the
points by subtracting. 2) Use the cos(θ) formula (which was done in Chapter 3,
#19.)
• Chapter 7 #15: (DIRECTION CHANGE!) The function should only have one
subfunction, which subtracts to find the vectors and, if 2-d, add a third component
of 0. Then use MATLAB’s built-in function cross in the main function.
• Chapter 7 #17: Since this function has no output, it should start with “function
NAME(INPUT)”. It is easiest to plot the circles using parametrized equations.
Use the hold on command (pp142 − 143) to plot both on the same axes.
• Chapter 7 #21: You can refer to Section 13.4 in the Stewart text for the formulas to
convert Polar Coordinates to Cartesian and vice versa. Note that to convert back
to θ, you should use the atan2 command (which avoids the domain restriction
(−π/2, π/2) and convert to degrees by ∗180/π.
Download