An interactive introduction to MATLAB

(Jeff_L) #1

54 decision making


Exercise 7: Decision making


Write your own script files to solve the following problems:


  1. Write a script file that asks the user for the input of a number and
    returns the natural logarithm of the number if the number is positive,
    and displays an error message otherwise.

  2. The cost per kilometre for a rental car is £0.50 for the first 100 kilometres,
    £0.30 for the next 200 kilometres and £0.20 for all kilometres in excess
    of 300 kilometres. Write a function that determines the total cost for a
    given number of kilometres.

  3. Write a function to evaluatef(x,y)for any two user specified valuesx
    andy. The functionf(x,y)is defined as:


f(x,y) =












x+y x> 0 andy> 0
x+y^2 x> 0 andy < 0
x^2 +y x < 0andy> 0
x^2 +y^2 x < 0andy < 0


  1. The energy loss due to fluid flow through a pipe can be calculated using
    the following equations:


hL=f

(

L

D

)(

V^2

2

)

, V=

Q

A, A=

πD^2
4 , Re=

DVρ
μ ,

where:

hL=energy loss per mass of fluid flowing (J/kg)
f=friction factor (dimensionless)
L=pipe length (m)
D=pipe diameter (m)
V=average fluid velocity (m/s)
Q=volumetric flow rate (m^3 /s)
A=pipe cross-sectional area (m^2 )
Re=Reynolds number (dimensionless)
ρ=fluid density (kg/m^3 )
μ=fluid viscosity (kg/ms)
Free download pdf