Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

MCD4140 Computing for Engineers

Self Study Exercise 2

Task 1

Write an M-File that performs the following in order from 1 to 7:

1)  Clear the Command Window

2)  Clear all variables

3)  Close all Figure windows

4)  Ask the user to enter an angle in degrees

5)  Convert the angle to radians

6)  Pause until a key is pressed

7)  Display the angle in radians

Task 2

A cylinder with a base radius 12cm and height 40cm contains 24cm deep of water. What is the new water level in the cylinder if 162 lead shots of radius 1cm are dropped into it? Assume the size of the cylinder and the lead shots are  fixed. Write  an  M-file  to  prompt  user  for  current water  level  and  the number of lead shots dropped. Your program should print the new water level in the command window. You should work out a  test case on paper and use it to check your answer.

Task 3

Develop an M-file to compute the velocity of a free-falling bungee jumper at a specific  time.  Your  program  should  prompt  the  user  for  the  mass  of  the bungee jumper  (m,  kg),  drag  coefficient  (c,  kg/m)  and  time  (s).  Use  the equation given below for this problem (use acceleration of gravity, g = 9.81 ms-2):

c 

[ms1]

Test Case: If m = 68.1 kg, c = 0.25, t = 12, then v(12) = 115.1647 ms-1

Task 4

Based  on  past  records  and  data,  it  is  known  that  a  project  can  be completed by 7 workers in 4 days, if every worker is working 6 hours per day. Write an M-file to calculate the number of days required to complete the project based on number of workers (n) and their working hours (t). Develop your interactive program with the help of the pseudo code below. Test Case: If 3 workers working 8 hours per day, then 7 days is required to complete the project.

Pseudo Code:

1.  Get number of worker from user, n.

2.  Get number of working hours per worker from user, t.

3.  Calculate total time required to complete the project by 7 workers.

4.  Calculate total time required to complete the project by 1 worker.

5.  Calculate total time required to complete the project by n workers.

6.  Calculate number of days required by n workers.

7.  Print the output in command window.

Task 5

I =

P = VI cosθ

Q = VI sinθ

S = VI

PF = cosθ

 

 

 

Load

Figure above shows a sinusoidal alternating current (AC) voltage source with voltage V supplying a load of impedance Z/θ Ω . From simple circuit theory,  the  rms  current  I  (Amperes,  A),  the  real  power  P  (Watts, W), reactive power Q (Volt-Amperes-Reactive, VAR), the apparent power S (volt-amperes, VA), and the power factor PF supplied to the load are given by the equations above. The power factor has no units associated with it.

Given the rms voltage of the power source and the magnitude and angle of the impedance Z, write a program that calculates the rms current I, the real power P, the reactive power Q, the apparent power S, and the power factor PF of the load.

Test Case: If V = 120V, Z = 5Ω , θ = 30° , then I = 24A, P = 2494W, Q = 1440 VAR, S = 2880VA, PF = 0.86603

Task 6

Write a function called MyParabola that takes a single number x as input and returns x2 + 4x + 1. Make sure that your function still works when x is a vector.

Task 7

Given the function you created in Task 1, what value will be in the variable result  after running the following code?

x  =  [1  2  3];

result  =  MyParabola(MyParabola(x))  +  MyParabola(5);

Task 8

An object thrown vertically with a speed v0 reaches a height h at time t, where

1     2

h = v t −    gt

Write and test a function that computes the time t required to reach a        specified height h, for a given value of vo . The function’s inputs should be hvo , and g. Test your function for the case where h = 100 m, vo = 50 m/s, and g = 9.81 m/s2 . Interpret both answers.

Task 9

Write a function to calculate the cross product of two vectors V1 and V2 . V1 × V2  = (Vy1Vz 2 Vy 2Vz1)i + (Vz1Vx 2 Vz 2Vx1)j + (Vx1Vy 2 Vx2Vy1)k

where V1  = Vx1i +Vy1j +Vz1k  and V2  = Vx 2i +Vy 2j + Vz 2k . Note that this function will return a real array as its result. Use the function to calculate the cross product of the two vectors V1 = [-2, 4, 0.5] and V2 = [0.5, 3, 2].

Task 10

Vector can be defined by a magnitude and a direction (polar coordinate system), or by the components of the vector projected along the axes of a rectangular coordinate system. The two  representations are equivalent, V = V ∠θ = Vx i + Vy j  .   We   can   convert   back   and   forth   between   the representations.

(a) Write a MATLAB function to convert from polar coordinate system to rectangular coordinate system using the equations below.

Vx  = V cosθ

Vy  = V sinθ

Test your MATLAB function with the following vectors:

P  = 5∠ − 36.87°

2

3

(b)  Write  a  MATLAB  function  to  convert  from  rectangular  coordinate

system to polar coordinate system using the equations below.

V =  V 2 + V 2

θ = tan 1

Test your MATLAB function with the following vectors:

R1  = 3i − 4j

R2  = 5i + 5j

R3  = −5i +12j

Task 11

Are the commands below correct?  If yes, what is the contents of res1, res2, res3 and res4? If not, explain why it is not correct.

str1 = 'This is a test! ';

str2 = 'This line, too.';

res1 = [str1 str2];

res2 = [str1; str2];

res3 = str1([2 6 15]);

mark = 98;

str3 = 'My target for ENG1060 is';

res4 = [str3 mark];

Task 12

Write a program that accepts an input string from the user and determines how many times a user specified character appears within the string.

Task 13

Write  command  lines to  read from  a file  named  magic.txt” .  Read the contents of the file according to the specifications below and store in an array named ‘result’ .

(a) Read all the numbers in the file and arrange them in a single column array.

(b) Read the first 7 numbers in the file and arrange the numbers in one row seven columns format.

(c) Read the first 10 numbers in the file and arrange the numbers in two rows five columns format.

Task 14

Write an m-file that prompt for user to enter full name and student ID. The full name should be stored in a character array / string. The student id is to be entered as a double array of 8 elements. The output of the m-file must be in the following format. Use disp() only to display the output.

Enter your full name (small letters with no spaces): 'maxsmith'

Enter your student ID (as an 1x8 array): [1 8 2 2 1 1 7 3]

Your name is : maxsmith

Your id is :

1     8     2     2     1     1     7     3

Task 15

Manning’s equation can be used to compute the velocity of water in a rectangular open channel:

2

S  (    BH     3

where  U  is  the  water  velocity  [m/s],  S  is  the  channel  slope,  n  is  the roughness coefficient, B is the channel width [m] and H is the depth of water [m]. The following data is available for five different channels:

n

S

B[m]

H [m]

U [m/s]

0.035

0.0001

10

2.0

 

0.020

0.0002

8

1.0

 

0.015

0.0010

20

1.5

 

0.030

0.0007

24

3.0

 

0.022

0.0003

15

2.5

 

Write  an  M-file that computes the velocity for  each of these channels. Enter  these  values  into  a  matrix  where  each  column  represents  a parameter and each row represents a channel. Have the M-file display the input data along with the computed velocity in tabular form where velocity is the fifth column. Include headings in the table to label the columns.