Sie sind auf Seite 1von 22

Name: Last Name, First Name M.I. Schedule: Time/Day.

Course & Year: Course/Year. Instructor: Engr. Darrel D. Pasalo


Date: Click here to enter a date.

EXPERIMENT NO. 2
0Arrays/Matrices, Plotting, User-Defined Functions and
User-Controlled Input/Output

I. OBJECTIVES
A. Manipulate and define arrays and matrices in Octave
B. Create, edit and label two and three-dimensional plots
C. Divide the plotting window into subplots
D. Create and use your own functions with both single and multiple inputs and outputs
E. Create a program that prompt the user for input and display and format the output

II. DISCUSSION
Defining Arrays and Matrices

Special Matrices
Manipulating Matrices

Two-Dimensional Plots
Control of Plot Appearance
Figure Control and Annotation
User-Defined Function

Format Control
Input/Output
Comparison Operators

Logical Operators
Control Structures
III. EXERCISES
A. Matrices/Arrays
1. Defining a Matrix
Create Octave variables to represent the following matrices, and use them in the exercises
that follow:

1. Assign to the variable x1 the value in the second column of matrix a. This
is sometimes represented in mathematics textbooks as element a1,2 and
could be expressed as x1 = a1,2.
2. Assign to the variable x2 the third column of matrix b.
3. Assign to the variable x3 the third row of matrix b.
4. Assign to the variable x4 the values in matrix b along the diagonal (i.e.,
elements b1,1, b2,2, and b3,3).
5. Assign to the variable x5 the first three values in matrix a as the first row
and all the values in matrix b as the second through the fourth row.
6. Assign to the variable x6 the values in matrix c as the first column, the
values in matrix b as columns 2, 3, and 4, and the values in matrix a as the
last row.
7. Assign to the variable x7 the value of element 8 in matrix b, using the single-
index-number identification scheme.
8. Convert matrix b to a column vector named x8.
2. Problems with two variables

1. The area of rectangle is length times width. Find the areas of rectangles with
lengths of 1, 3 and 5 cm and with widths of 2, 4, 6 and 8 cm. (You should have 12
answers)

2. The volume of a circular cylinder is, volume = pi*r^2*h. Find the volume of
cylindrical containers with radii from 0 to 12 m and heights from 10 to 20 m.
Increment the radius dimension by 3 m and the height by 2 m as you span the two
ranges.
3. Consider the following table

a. Create a column vector named times going from 0 to 24 in 2 hour increments.


b. Create a matrix named thermocouple by typing in the data from the table above.
c. Combine the times vector you created in part (a) with the data from the thermocouple to
create a matrix corresponding to the table in this problem.
d. Recall that both the max and min functions can return not only the maximum values in a
column, but also the element number where those values occur. Use this capability to
determine the values of times at which the maxima and minima occur in each column.

B. Plotting
1. 2D-Plotting
Second Order Unit Step Response
Case 1: Critically Damped

Case 2: Overdamped

Case 3. Underdamped

Using the following functions where:

a. Plot Case 1 if = 1. Change the color of the output to blue and make it solid.
b. Plot Case 2 if = 2. Change the color of the output to green and make it solid.
c. Plot Case 3 if = 0.1 and Change the color of the output to
black and make it solid
d. Plot the three cases in a single window using subplot. Make it a 2 x 2 subplot with all
three cases combined in the fourth plot. Maintain the colors of each plot from a, b and
c. Name the plot Critically Damped, Overdamped, Underdamped and Effects of
Damping Ratio respectively. Label the y axis as y(t) and the y axis as x. Plot all cases
from t = 0 to t = 20 with 2000 points.

a. Case1
b. Case 2
c. Case 3

d. Subplot
2. Polar Coordinate Plotting
Plot the following equation in polar coordinate system:
a. r = 2 + 4sinƟ - limacon
b. r = 5cos(9Ɵ) - rose
c. r = 18cos(2Ɵ)
2
- lemniscate of Bernoulli
3. Logarithmic Plotting
The total transistor count
integrated circuits produced
over the last 35 years is shown
in the table to the right. Create
a semilog plot (with the y-axis
scaled logarithmically) of the
actual data, using circles only
to indicate the data points (no
lines). Include a second line
representing the predicted
values using Moore’s Law,
based on the 1971 count as the
starting point. Add a legend to
your plot.
Note: The y-axis should be
labeled “Transistor Count’
while the x-axis is ‘Year’
Hint: “Moore’s Law states that
the no. of transistor in a dense
IC doubles about every two
years”
4. 3D Plotting
Create x and y vectors from -5 to +5 with a spacing of 0.5. Use the mesh grid function to map x
and y onto two new two-dimensional matrices called X and Y. Use your new matrices to calculate
vector Z. with magnitude:

a. Use the mesh plotting function to create a three-dimensional plot of Z.


b. Use the surf plotting function to create a three –dimensional plot of Z. Compare the results
you obtain with a single input (Z) with those obtained with inputs for all three dimension (X,
Y, Z)
c. Modify your surface plot with interpolated shading. Try using different colormaps.
d. Generate a countour plot of Z.
e. Generate a combination surface and countour plot of Z.
C. Function
1. Create a function called “polygon” that draws a polygon with any number of sides. Your
function should require a single input: the number of sides desired. It should not return any
value to the command window but should draw the requested polygon in polar coordinates.
2. Write a function m-file called polyadd.m that adds two polynomials (of not necessarily the
same degree). The input should be the two vectors of coefficients and the output should be
a new vector of coefficients representing their sum.
3. Write a function m-file called div5.m that takes as input a real number and checks to see if it
is divisible by 5. An appropriate message indicating the result should be the output.

III. QUESTIONS
1. Explain any differences between the answers that Octave gives when you type in A*A, A^2
and A.^2 if A is any matrix.
Click here to enter text.

2. What is the command that isolates the submatrix that consists of the 2 nd to 3rd rows of a
matrix?

Click here to enter text.

3. Give the atleast two ways of making two or more graphs appear in just one plot
Click here to enter text.
IV. CONCLUSION

Click here to enter text.

Das könnte Ihnen auch gefallen