Вы находитесь на странице: 1из 2

700018 - Engineering Computing Workbook


Week 10 Execise
Learning Objectives

User Defined Function

Question 1:

Write a function called getResult which takes an argument named m (m can be a

vector or a single value) and calculate the results for the following expression and
return it:
n = 0.5 * m^2 + 2*m - 1
Use the function to calculate the results for m=2 and m=1:5 vector.

Question 2:

Create a user-defined function called even_or_odd which takes a number. If the

number is an odd number you should display the message Odd number, if it is
even number you should display Even number. (hint: You can use rem(x,y) to
determine the remainder.)

Question 3:

Create a user-defined function called DisplayNumbers to display the number from 1 to

n. Then test your result to display from 1 to 10. (hint: use a for loop.)

Question 4:

A rocket is launched vertically. At time t=0, the rockets engine shuts down. At that
time, the rocket has reached an altitude of 500m and is rising at a velocity of 125m/s.
gravity then takes over. The height of the rocket as a function of time is
9.8 2
() = t + 125t + 500 for t > 0
(a) Create a function called calcHeight that accepts time as an input and returns
the height of the rocket.

(b) Plot height (Use the function you created in part a to calculate height.) versus
time for times from 0 to 30 seconds. Use an increment of 0.5 second in your
time vector.
Question 5:

This problem requires you to generate temperature-conversion tables. Use the

following equations, which describe the relationships between temperatures in
degrees Fahrenheit (TR), degrees Celsium (TC), kelvins (TK), and degrees Rankine
(TR), respectively:
TF = TR 459.67 R

TF = T + 32
5 C

TR = T
5 K
You will need to rearrange these expressions to solve some of the problems.

(a) Create a function called F_to_K that converts temperatures in Fahrenheit to Kelvin.

Use your function to generate a conversion table for values from 0F to 200F.

(b) Create a function called C_to_R that converts temperatures in Celsius to Rankine.

Use your function to generate a conversion table from 0C to 200C. Print 25

lines in the table. (Use the linspace function to create your input vector.)
(c) Create a function called C_to_F that converts temperatures in Celsius to

Fahrenheit. Use your function to generate a conversion table from 0C to 100C.

Choose an appropriate spacing.

Question 6:

(a) Write a user-defined MATLAB function called Grade that takes test score as an
argument and return letter grade for that score based on the grading scale given
in Table 1.
Score Grade
100 or above X
85-100 HD
75-84 D
60-74 C
50-59 P
0-49 F
Below 0 X
Table1: Grading Scale
(b) Use the user-defined function you wrote for part a to calculate grade for:
Test score =85
Test score=62