0% found this document useful (0 votes)
8 views29 pages

Matlabnoteschap 03

Uploaded by

Marwa AlFaouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views29 pages

Matlabnoteschap 03

Uploaded by

Marwa AlFaouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 3: Functions and Files

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:

Open MATLAB. Open a new Function File.


The Function File consists of a beginning and end which is outlined on
the left of the function. This function will be used to calculate the
volume of a sphere:
4𝜋 3
𝑉= 𝑟
3
The radius of the sphere will be an input argument, and the volume of the
sphere will be an output variable. Double-click on input args and
replace it with Sphere_Radius. Double-click on output args and
replace it with Sphere_Volume.
Double-click on Untitled and replace it with
Sphere_Volume_Calculator.
Press the Save button. This will automatically name the Function File
appropriately.
Type in the equation for the volume of a sphere as shown below. You
must use the input variable name Sphere_Radius and the output variable
name Sphere_Volume as specified in the Function Statement. Make
sure to Save the Function File whenever you make changes to it.

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.

x = fminbnd(fun,x1,x2) returns a value x that is a local minimizer of the


function that is described in fun in the interval x1< x < x2. fun is a
function_handle.
Problem 3.11:
Create a new Function File that calculates the following in terms of the
Radius:

• Water Heater Height


• Cost of the Water Heater

Input: Radius; Output: Water Heater Cost

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

Cost = 400 2𝜋𝑟ℎ + 600 2𝜋𝑟 2 $


Problem 3.11:
Problem 3.19:
Problem 3.19:
Working with Data Files
xlsread
Read Microsoft Excel spreadsheet file
Syntax
num = xlsread(filename)
Read data from the first worksheet.
filename = 'myExample.xlsx'; A = xlsread(filename)
xlsread returns the numeric data in array A.

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:

You might also like