Matlab Question

profilejhh19970724
ENG5142017-18Lab2.pdf

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 1 of 10 2017/18

Question 1

a) Use MATLAB to create a vector from −2 to +2 with an increment of 1, using colon notation. 1. Find the absolute value of each element of the vector.

2. Find the square root of each element of the vector (Hint: Use sqrt function)

b) Find the square root of both −3 and +3.

1. Use the sqrt function.

2. Use the nthroot function, to calculate the square root of the vector. (You should get an error statement for −3).

3. Raise −3 and +3 to the ½ power, using ‘. ^’ or power function How do the results vary?

c) Create a vector 𝑥 from −9 to 12 with an increment of 3.

1. Find the result of 𝑥 divided by 2. 2. Find the remainder of 𝑥 divided by 2.

d) Create a vector 𝑦 from 2.4 to 7.8 with an increment of 0.6. Find the 𝑒𝑦 for each

element of the vector, using the exp function.

e) Using the vector from exercise (d),

1. Find ln⁡(𝑦) (the natural logarithm of 𝑦), using log function.

2. Find 𝑙𝑜𝑔10(𝑦) (the common logarithm of 𝑦), using log10 function. Explain your results.

f) Use the sign function to determine which of the elements in vector 𝑥 are positive.

Question 2 Consider the air-conditioning requirements of the large house. The interior of the house is warmed by waste heat from lighting and electrical appliances, by heat leaking in from the outdoors and by heat generated by the people in the house. An air-conditioner must be able to remove all this thermal energy in order to keep the inside temperature from rising. Suppose there are 20 light bulbs emitting 100 J/s of energy each and four appliances emitting 500 J/s each. Suppose also that heat leaks in from the outside at a rate of 3000 J/s.

a) How much heat must the air-conditioner be able to remove from the home per second?

b) One particular air-conditioning unit can handle 2000 J/s. How many of these units are needed to keep the home at a constant temperature? Use an appropriate rounding function.

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 2 of 10 2017/18

Question 3 a) Use MATLAB’s functions for Discrete Mathematics to complete the following

calculations. 1. Factor the number 322. 2. Find the greatest common denominator of 322 and 6. 3. Is 322 a prime number? 4. Approximate 𝜋 in a fractional form. 5. Find 10! (factorial of 10).

b) Calculate the following using MATLAB’s trigonometric functions. All angles are given

in radians. 1. sin⁡(2𝜃) for 𝜃 = 3𝜋 2. cos⁡(𝜃) for 0 ≤ 𝜃 ≤ 2𝜋; let 𝜃 change in steps of 0.2𝜋 3. 𝑠𝑖𝑛−1(1) 4. 𝑐𝑜𝑠−1(𝑥) for −1 ≤ 𝑥 ≤ 1; let 𝑥 change in steps of 0.2 5. Find the cosine of 45°.

o Convert the angle from degrees to radians and then use the cos function

o Use the cosd function. 6. Find the angle whose sine is 0.5. Is your answer in degrees or radians?

c) Use the MATLAB Data Analysis Functions for maximum and minimum to answer the

questions below. Consider the following matrix:

𝑥 = [

4 90 85 75 2 55 65 75 3 78 82 79 1 84 92 93

]

1. What is the maximum value in each column? 2. In which row does the maximum occur? 3. What is the maximum value in each row? 4. In which column does the maximum occur? 5. What is the maximum value in the entire table?

d) Use the MATLAB Data Analysis Functions for mean and median to answer the

questions below. Consider the matrix from the previous question (c). 1. What is the mean value in each column? 2. What is the median for each column? 3. What is the mean value in each row? 4. What is the median in each row? 5. What is returned when you request the mode? 6. What is the mean for the entire matrix?

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 3 of 10 2017/18

e) Use the MATLAB Data Analysis Functions for sorting values and matrix sizing to

answer the questions below. Consider the matrix from the previous question (c).

1. Use the size function to determine the number of rows and columns in this matrix.

2. Use the sort function to sort each column in ascending order.

3. Use the sort function to sort each column in descending order.

4. Use the sortrows function to sort the matrix so that the first column is in ascending order, but each row still retains its original data. Your matrix should look like this:

𝑥 = [

1 84 92 93 2 55 65 75 3 78 82 79 4 90 85 75

]

f) Use the MATLAB Data Analysis Functions for standard deviation, variance and

random numbers to answer the questions below. Consider the matrix from the previous question (c). 1. Find the standard deviation for each column. 2. Find the variance for each column. 3. Calculate the square root of the variance you found for each column. 4. How to the results from the previous calculation compare against the standard

deviation you found in the first sub-question?

5. Create a 3 × 3 matrix of evenly distributed random numbers. 6. Create a 3 × 3 matrix of normally distributed random numbers. 7. Create a 100 × 5 matrix of evenly distributed random numbers. Be sure to

suppress the output. 8. Find the maximum, the standard deviation, the variance and the mean for each

column in the matrix you created in the previous sub-question. 9. Create a 100 × 5 matrix of normally distributed random numbers. Be sure to

suppress the output. 10. Find the maximum, the standard deviation, the variance and the mean for each

column in the matrix you created in the previous sub-question. 11. Explain why your results for the two separate calculations above are different.

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 4 of 10 2017/18

Question 4 The displacement of the oscillating spring shown in the Figure 1 can be described by

𝑥 = 𝐴𝑐𝑜𝑠(𝜔𝑡) where, x = displacement (m) at time t A = maximum displacement (m) ω = angular frequency (rad/sec), which depends on the spring constant and the mass attached to the spring t = time (s).

a) Find the displacement x for times from 0 to 10 seconds when the maximum displacement A is 4cm and the angular frequency is 0.6 rad/sec. Present your results in a table of displacement and time values.

b) The acceleration of the spring is given by

𝑎 = −𝐴𝜔2 cos(𝜔𝑡)⁡ [m/s2] Find the acceleration from times 0 to 10 seconds, using the values from question (a). Add an extra input in the previous table for the calculated acceleration.

Figure 1. An oscillating spring.

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 5 of 10 2017/18

Question 5 The range of an object shot at an angle 𝜃 in radians with respect to the x-axis and an initial velocity 𝑣0 in m/s (see Figure 2) is given by

𝑅𝑎𝑛𝑔𝑒 = 𝑣0 2

𝑔 sin(2𝜃)⁡⁡⁡⁡[m]

for 0 ≤ 𝜃 ≤ 𝜋/2 and neglecting air resistance. Use 𝑔 = 9.81⁡m/s2 and an initial velocity 𝑣0 of 100 m/s. Show that the maximum range is obtained at approximately 𝜃 = 𝜋/4 by computing the rage in increments of π/100 between 0 ≤ 𝜃 ≤ 𝜋/2. You won’t be able to find the exact angle that results in the maximum range, because your calculations are at evenly spaced angles of π/100 radians.

Figure 2. The range depends on the launch angle and the launch velocity.

Question 6 Consider the circuit shown in the Figure 3, which includes the following:

 A sinusoidally varying voltage source, V [V]

 An inductor, with an inductance, L [H]

 A capacitor, with a capacitance, C [F]

 A resistor, with a resistance R [Ω] We can find the current, I [A], in the circuit by using Ohm’s law (generalized for alternating currents),

𝑉 = 𝐼𝑍𝑇 where ZT [1/Ω] is the total impedance in the circuit (impedance is the AC corollary to resistance). Assume that the impedance for each component is as follows:

𝑍𝐿 = 0 + 5𝑗⁡[Ω] 𝑍𝐶 = 0 − 15𝑗⁡[Ω]

𝑅 = 𝑍𝑅 = 5 + 0𝑗⁡[Ω] 𝑍𝑇 = 𝑍𝐶 + 𝑍𝐿 + 𝑅

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 6 of 10 2017/18

and that the applied voltage is

𝑉 = 10 + 0𝑗⁡[V] Find the current, I, in the circuit. You should expect a complex number as a result. Enter the

complex values of impedance into your calculations using the complex function.

Figure 3. A simple circuit illustrating a sinusoidally varying voltage source, V

Question 7 Answer the questions below using MATLAB matrix manipulation rules.

a) Create MATLAB variables to represent the following matrices and use them in the sub- questions below:

𝑎 = [12 17 3 6]⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡𝑏 = [ 5 8 3 1 2 3 2 4 6

] ⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡⁡𝑐 = [ 22 17 4 ]

1. Assign to the variable x1 the value in the second column of matrix a. This is sometimes represented as element 𝑎1,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 value in matrix b along the diagonal (i.e. elements 𝑏1,1, 𝑏2,2, 𝑏3,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.

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 7 of 10 2017/18

b) Use the meshgrid MATLAB command to answer the following questions. 1. The area of a rectangle is length times width (area = length x width). Find the

areas of rectangles with lengths of 1, 3 and 5cm and with widths of 2, 4, 6 and 8cm. (You should have 12 answers).

2. The volume of a circular cylinder is, 𝑣𝑜𝑙𝑢𝑚𝑒 = 𝜋𝑟2ℎ [m3]. Find the volume of cylindrical containers with radii from 0 to 12m and heights from 10 to 20m. Increment the radius dimension by 3m and the height by 2m as you span the two ranges using the colon notation.

c) Use the MATLAB commands for special matrices to answer the following questions. 1. Create a 3 × 3 matrix of zeros. 2. Create a 3 × 4 matrix of zeros. 3. Create a 3 × 3 matrix of ones. 4. Create a 5⁡× 3 matrix of ones. 5. Create a 4⁡× 6 matrix in which all the elements have a value of pi.

6. Use the diag function to create a matrix whose diagonal has values of 1, 2, 3. 7. Create a 10 × 10 magic matrix.

Question 8 A barometer (see Figure 5) is used to measure atmospheric pressure and is filled with a high- density fluid. In the past, mercury was used but because of its toxic properties it has been replaced with a variety of other fluids. The pressure, 𝑃, measured in Pa by a barometer is the height of the fluid column, ℎ in m, times the density of the liquid, 𝜌 in kg/m3, times the acceleration due to gravity, 𝑔 in m/s2, or

𝑃 = ℎ𝜌𝑔⁡[Pa] This equation could be solved for the height:

ℎ = 𝑃

𝜌𝑔 ⁡[m]

Find the height to which the liquid column will rise for pressures from 0 to 100kPa for two different barometers. Assume that the first uses mercury, with a density of 13.56 g/cm3 (13560 kg/m3) and the second uses water, with a density of 1.0 g/cm3 (1000 kg/m3). The acceleration due to gravity is 9.81 m/s2. Before you start calculating, be sure to check the units in this calculation. The metric measurement of pressure is a Pascal (Pa), equal to 1 kg/m s2. Your answer should be a two-dimensional matrix.

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 8 of 10 2017/18

Figure 4. Barometer.

For the Questions 9-11, as you create functions, be sure to comment them appropriately. Remember that, although some problems could be solved without a function, the objective of these questions is to learn to write and use functions. Each of these functions must be created in its own M-file and then called from the command window or a script M-file program.

Question 9 a) Create MATLAB functions with one input argument, x, returning a result evaluated by

the following mathematical functions (make sure you select meaningful function names) and test them. To test your functions you’ll need to call them from the command window.

1. 𝑦(𝑥) = 𝑥2 2. 𝑦(𝑥) = 𝑒1/𝑥 3. 𝑦(𝑥) = sin⁡(𝑥2)

b) Create MATLAB functions for the following unit conversions (you may need to consult

a textbook or the Internet for the appropriate conversion factors). Be sure to test your functions. 1. Inches to feet 2. Calories to joules 3. Watts to BTU/hr 4. Meters to miles 5. Miles per hour (mph) to ft/s

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 9 of 10 2017/18

c) Assuming that the matrix dimensions agree, create and test MATLAB functions to evaluate the following simple mathematical functions with a single or multiple input vectors and a single or multiple output vectors. Make sure you add appropriate comments to your code. 1. 𝑧(𝑥, 𝑦) = 𝑥 + 𝑦 2. 𝑧(𝑝, 𝑡) = 𝑝/sin⁡(𝑡)

3. 𝑓(𝑥) = √5𝑥2 + 2 4. 𝑓(𝑥) = ln⁡(𝑥) 5. 𝑓(𝑥, 𝑦) = 𝑥 − 𝑦 6. 𝑓(𝑥, 𝑦) = 𝑦𝑒𝑥

Question 10 The distance to the horizon increases as you climb a mountain (or a hill). The expression

𝑑 = √2𝑟ℎ + ℎ2⁡⁡[m] where d = distance to the horizon [m] r = radius of the Earth [m] h = height of the hill [m] can be used to calculate that distance. The distance depends on how high the hill is and on the radius of the Earth (or another planetary body).

a) Create a function M-file called distance to find the distance to the horizon. Your function should accept two vector inputs – radius and height – and should return the distance to the horizon. The result should be calculated for all combinations of radii

and heights. To do so, use the meshgrid function to create the 2D array of input values.

b) Create a MATLAB program that uses your distance function to find the distance in km to the horizon, both on the earth and on Mars, for hills from 0 to 3 km. Remember to use consistent units in your calculations. Note that

 Earth’s diameter = 12756 km

 Mars’ diameter = 6787 km Report your results in a table. Each column should represent a different planet and each row a different hill height.

ENG514 Lab 2 by Dr. Antigoni Barouni Built-In and User-Defined Functions; Matrices

Page 10 of 10 2017/18

Question 11 Perhaps the most famous equation in physics is

𝐸 = 𝑚𝑐2⁡⁡⁡[J] Which relates energy 𝐸 to mass 𝑚 in kg. The speed of light in a vacuum, 𝑐, is the property that links the two together. The speed of light in a vacuum is 2.9979 × 108𝑚/𝑠.

a) Create a function called energy to find the energy corresponding to a given mass in

kilograms. Your result will be in Joules, since 1⁡𝑘𝑔 m2

s2 = 1⁡J.

b) Use your function to find the energy corresponding to masses from 1 kg to 106 kg. use

the logspace function to create an appropriate mass vector. Use appropriate comments throughout the function you create.