GENERAL GUIDELINE:

advertisement
GENERAL GUIDELINE:
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!
Chapter 6
#6: (for/end loops and nested loops)
We should use two for/end loops: one nested inside the other. The basic structure of a for/end
loop is on Page 192-194 of Gilat. There is a useful example on Page 200-202 of using nested loops
to create a matrix. You just have to replace the conditional statements with the formula.
#7: (for/end loops and nested loops)
Similar idea as #6. Note instead of inputting the dimension of the matrix, we use two codes for
each problem, as input causes publishing issues. In other words, you will have for i=1:4, for j=1:4
& for i=1:7, for j=1:7 for the problems.
There are two ways of generate the elements:
1. Notice that a(i,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 (translate this by yourself).
If you want to generate your element this way, your code should include two for/end loops
and one if-elseif-else-end structure.
(𝑖+𝑗−2)!
2. Another way of generating is using a formula: 𝑎(𝑖, 𝑗) = (𝑖−1)!(𝑗−1)! . For example,
(2+3−2)!
3!
𝑎(2,3) = (2−1)!(3−1)! = 1!2! = 3, which is the element of the second row and third column.
If you want to generate your element this way, you will have two for/end loops. In Matlab,
n! is factorial(n).
#29: (loops, conditional statements, relational operators)
(a) Part (a) does not require any programming-it is just like a Chapter 5 problem. You can
follow the steps:
1. Define the values of Dg, vd, ka, ke as given in the problem;
2.
Define the vector t (note: we want our plot to be smooth, so it’s important to have
enough elements, i.e. t=0:0.1:10 is fine, but t=1:1:10 is not acceptable).
3.
Calculate Cp and plot(t, Cp).
(b) I encourage you to use loops and conditional statements for this problem. There is a
quick way to define Cp as a piecewise function. The steps are: 1. Define t as a vector:
t=0:0.1:24. 2. Define Cp using the formula in the problem. 3. Define a new vector which
represent the drug in the body, say C. Then C(1:40)=Cp(1:40);
C(41:80)=Cp(41:80)+Cp(1:40);
and so on. Note that the elements of the vector C changes if you define t as a vector with
different space, say if t=0:0.01:24. Then the elements of C will be: C(1:400)=Cp(1:400) and so
on.
#34: (loops, conditional statements, relational operators)
Question 34 ask you to plot a piecewise function. You can follow the steps listed here:
1. Define theta as a vector.
2. Use if-elseif-else-end structure to define the corresponding vector y . Notice this time you
will need several elseifs. Also, you have to use logical operators. For example:
if theta(k)<=pi/2 y(k)=6*(2*theta(k)-0.5*sin(theta(k)))/pi;
elseif theta(k)<=2*pi/3
y(k)=6; ect.
3. After finish defining y vector, you can use the command plot to graph the function.
#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. E.g., for part(a), define the vectors:
quizgrade= [6 10 6 8 7 8]; midterm= [82 95 89]; final=81. And define the new vectors for
part (b).
2) Calculate average. Quiz average is easiest to use the sum and min commands (Recall p76).
The idea is to use the sum minus min then divide by 5.
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). Another way of
calculating is using one if-else-end structure (maybe this way is easier to understand).
For calculating the average of the midterm grades, you can use the command mean(page
76).
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.
Chapter 7
Chapter 7 of Gilat covers functions. Recall a function is a rule that assigns a unique output to each
input. Note that "inputs" and "outputs" can be several variables (think of it as a single vector
whose components correspond to each variable).
#1: (anonymous functions)
Page231-233 of Gilat. Define the function anonymously in their M-file using the @ notation.
Check the example on p233 (especially noting the element-by-element definition needed when
plotting). Note all plots MUST have titles and axes labels (see Gilat p144, with formatting details
on Page145-148).
For the remaining problems (IMPORTANT):
1. Create separate function files using the "function" command. Refer to Page223-229,
paying particular attention to the example file near the top of Page229 which is used (in
their M-file) midway through the page.
2. 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!
#12
Recall how to do this by hand: 1) create vectors between the points by subtracting(i.e. BA = A-B;
BC = C-B). 2) Use the cos(theta) formula (which was done in Chapter 3, #19). You can follow the
steps listed here:
1. Create the function file: The first line should be th=anglines(A,B,C), the function name
should be anglines.m when you save the function file. This means you have three inputs
(the points A,B,C) in your function and the output is th. Inside your function file, you need
to create two vectors between the given two points by subtracting. And calculate th using
arccos(Chapter 3, #19).
2. Create you script file, define the point A, B, C as given in the problem. Then apply the
function (you need just one line to apply, which is th=anglines(A,B,C)). Check Page229 for
example. Then display the angle th.
#15: (DIRECTION CHANGE!)
1) The function should only have one subfunction, which subtracts to find the vectors.
(Check Page 240-242 about subfunction)
2) If it is 2-dimensional, add a third component of 0.
3) Then use MATLAB's built-in function cross in the main function.
#17
1) Since this function has no output, it should start with"function NAME(INPUT)".
2) It is easiest to plot the circles using parametrized equations. Try to write x and y using
parametric theta. You can calculate the radius r using c and p.
3) Use the hold on command (Page142-143) to plot both on the same axes.
#21
1. You can refer to Section 13.4 in the Stewart text for the formulas to convert Polar
Coordinates to Cartesian and vice versa.
2. NOTE that to convert back to theta, you should use the atan2 command (which avoids
the domain restriction (-pi/2, pi/2) and convert to degrees by *180/pi.
3. You can follow the steps listed here:
1) Define the function file: [r th]=AdVecPol(r1,th1,r2,th2). In your function file,
calculate the radius r and angle th when given r1, th1, r2, th2( This is the hardest
part, I’ll explain this in detail next week).
2) Write a script file to apply the function. Identify r1, th1, r2, th2 in the problem.
Download