Matlabnoteschap 03
Matlabnoteschap 03
Topics Covered:
• Built-In Functions
• Mathematical Functions
• User-Defined Functions
• Function Files
• Anonymous Functions
• Function Functions
• Function Handles
• Working with Data Files
Built-In Functions:
Built-In Functions:
Built-In Functions:
Problem 3.5:
Create a vector for x over the range 1 ≤ 𝑥 ≤ 10𝜋. Use a MATLAB script
file to plot both cosh−1 (𝑥) and ln 𝑥 + 𝑥 2 − 1 to show that they are
the same function.
Problem 3.7:
When a belt is wrapped around a cylinder, the relation between the belt
forces on each side of the cylinder is
𝐹1 = 𝐹2 𝑒 𝜇𝛽
where 𝛽 is the angle of wrap of the belt in radians and µ is the friction
coefficient. Create a vector for 𝛽 over the range 10 ≤ 𝛽 ≤ 720°. Use a
MATLAB script file to plot the force 𝐹1 over the range of 𝛽 for µ = 0.3
and 𝐹2 = 100 N. (Hint: Be careful with 𝛽!)
User-Defined Functions:
4𝜋 3
𝑉= 𝑟
3
Open a new MATLAB Script File. This will be the Calling Script File.
Save it into the same folder location as the Function File that you just
created. Use the radius value and submit it as an input to the Function
File. Notice that the input argument name (r) does not have to match the
name in the function file (Sphere_Radius). The names of variables
inside the Function File are called Local Variables (Local to the
Function). Run this script file and check the result by hand.
Anonymous Functions are used to create a simple function within a
script file. We will create an Anonymous Function to calculate the
surface area of a sphere as shown below. Again, the input argument name
in the calling statement does not have to be the same as that in the
defining statement. Run the script file again and check the result by hand.
Problem 3.9:
Create a Function File that accepts temperature in degrees Fahrenheit
(°F) and computes the corresponding value in degrees Celsius (°C). The
relation between the two is
5
𝑇°C = 𝑇°F − 32
9
Create a Calling Script File to plot the temperature in °C versus the
temperature in °F over the range 0 ≤ 𝑇 ≤ 250°F.
Function Functions:
Functions that act on other functions are called Function Functions. The
@function is called a Function Handle, which is used to reference a
User-Defined Function.
Search Documentation: function handle
function_handle (@)
Handle used in calling functions indirectly
Syntax
handle = @functionname
handle = @(arglist)anonymous_function
Description
handle = @functionname returns a handle to the specified MATLAB
function.
A function handle is a MATLAB value that provides a means of calling a
function indirectly. You can pass function handles in calls to other
functions (often called function functions).
At the time you create a function handle, the function you specify must
be on the MATLAB path.
Problem 3.11:
2 2
2𝜋 3
𝐴cyl = 2𝜋𝑟ℎ; 𝐴hemi = 2𝜋𝑟 ; 𝑉cyl = 𝜋𝑟 ℎ; 𝑉hemi= 𝑟
3
2
2𝜋 3
𝑉 = 𝑉cyl + 𝑉hemi = 𝜋𝑟 ℎ + 𝑟 = 600 m3
3
1 2𝜋 3
ℎ = 2 600 − 𝑟
𝜋𝑟 3
$ $
Cost = 400 2 𝐴cyl + 600 2 𝐴hemi
m m
Cost = 400 2𝜋𝑟ℎ + 600 2𝜋𝑟 2 $
Problem 3.11:
Search Documentation: fminbnd
fminbnd
Find minimum of single-variable function on fixed interval
Syntax
x = fminbnd(fun,x1,x2)
Description
fminbnd finds the minimum of a function of one variable within a fixed
interval.
Test your Function File using the Calling File for a radius of
r = 1.0 m.
1 2𝜋 3 Cost = WH_Calculator(1.0)
ℎ = 2 600 − 𝑟
𝜋𝑟 3
h = 190.3193
cost = 4.8209e+05
Cost = 400 2𝜋𝑟ℎ + 600 2𝜋𝑟 2 $
Problem 3.11:
Use the Calling Script File to plot the cost of the water heater. Make
sure the Calling Script File is in the same folder as the Function File.
Problem 3.11:
Calculate the minimum cost of the water heater using the Built-In
Function fminbnd. Then compute the corresponding height of the water
heater.
1 2𝜋 3
ℎ = 2 600 − 𝑟
𝜋𝑟 3
load
Load data from MAT-file into workspace
Syntax
S = load(filename, '-ascii')
Forces load to treat the file as an ASCII file, regardless of the extension.
Problem 3.23:
Use Microsoft Excel to create a file containing the following data. Then
use the load function to load the file into MATLAB, and use the mean
function to compute the mean value of each column.
Problem 3.23: