Powerpoint

advertisement
Functions 1
ENGR 1181
MATLAB 14
User-defined Functions in Real Life
Using available built-in functions in our calculators and MATLAB is great, but often we
encounter situations for which there is no pre-defined function. Like adding a quadratic
equation solver program to our calculators, we can create our own functions in MATLAB to
perform all sorts of calculations.
Today's Learning Objectives
 After today’s class, students will be able to:
• Demonstrate the proper use of functions in
their programs.
• Explain how functions can be used in multiple
places and as an organizational tool.
• Describe purpose and structure of a function
definition line.
What is a function?
 MATLAB has built-in functions that we have been using all
semester such as sum,length,mean, and others.
 We will now learn how to create our own functions; these are
called user-defined functions.
 It is also referred to as a subroutine or a procedure in other
programming languages.
What is a function?
 MATLAB functions can be used to make programs easier to
read and reduce the lines of code.
 Allows multiple people to work on the same project
seamlessly
• Ex: One person's function can be referenced in another
person's script file.
What is a function?
 Just like a built-in function, a user-defined function
can be used within the command window or your
script file.
 In order to do this, the function must be saved in
your active directory.
Opening a function file
Open a function file (not a script file)
Basic function structure
function[output variable(s)] = function_name(input variables)
typical code as compared to a script file
end
**Function definition line
Required if using nested functions, optional otherwise
**Must follow this format or MatLab will think it’s a script file and/or you will get an error
Saving a function file
function[fun,outputs] = eng_fun(math,science,physics)
Function files MUST be saved as the name it is called out as in the function
definition line as highlighted above. Also, it must be saved as a .m file.
For example, this function file would be saved as:
eng_fun.m
Example: One input & output
Let’s calculate how many Oreo® cookies are left in a box
using a function file:
1. Open a new function file
2. Complete the function definition line
Function[oreos_left]=oreos(hrs_of_HW)
3. Save file – oreos.m
Example: One input & output
Now that we have the function file defined, we can carry out the
calculations to determine how many Oreo® cookies we will eat:
eat_yum=ceil(1/2*exp(hrs_of_HW)); %calculates the
number of oreos
% Typical oreos in a package is 30
package=30;
oreos_left= package-eat_yum; %This line calculates the
output
Example: One input & output
Let’s test our file now; be sure to save your function file. Go to
your command window and type this:
oreos(hrs_of_HW)
What happens?
>> oreos(hrs_of_HW)
Undefined function or variable 'hrs_of_HW'.
Example: One input & output
Now try this in the command window:
oreos(3)
What happens?
How about this:
ans =
19
x=3;
box=oreos(x)
Example: Two inputs
& one output
Let’s modify our oreo code to account for stress of the number of
midterms that week:
function[oreos_left]=oreos_rev(hrs_of_HW,exams)
SAVE YOUR FILE!
oreos_rev.m
Modify our equations:
eat_yum=ceil(1/2*exp(hrs_of_HW));
package=30;
bonus_yum=exams*3;
oreos_left= package-eat_yum-bonus_yum;
Example: Two inputs
& one output
Run from the command window where you did 1 hour of
homework and have 2 midterms this week:
HW=1; %Hours of homework
MT=2; %Number of midterms this week
box=oreos_rev(HW,MT)
box = 22
Create a script file
using oreos_rev.m
Script file
clc
clear
hrs=input(‘How many hours of homework have you done today? ’);
midterms=input(‘How many midterms do you have this week? ’);
box=oreos_rev(hrs,midterms);
fprintf(‘\nYou have %i oreos left, you better do more homework!’,box)
Output
How many hours of homework have you done today? 2
How many midterms do you have this week? 1
You have 23 oreos left, you better do some more homework!
Functions with multiple
inputs & outputs
Suppose we want to calculate the stress and deflection of a cantilever
beam like the one in lab, let’s create a function file for this:
𝐹
𝜎=
𝐴
𝑤 𝑡3
𝐼 =
12
𝐹𝐿3
𝛿 =
3𝐸𝐼
Function definition line
function [stress, deflection] = beam_lab(w, t , F, L, E)
Functions with multiple
inputs & outputs
 Write a function file to
calculate stress and
deflection
• Hint: When using more than one
output, you must assign the
function to multiple outputs.
Ex: [s,d]=beam_lab(inputs)
w = .05 meters
t = .01 meters
F = 100 newtons
L = 1 meter
E = 70 x 109 N/m2
What do you get?
 Then write a script file that
calls the function file and
uses these values.
stress = 200,000 N/m2
deflection = 0.1143 m
Functions Example
function v = freefall(t,m,cd)
%freefall: bungee velocity with second-order drag
%v=freefall(t,m,cd) computes the free-fall velocity of an object
with
%second-order drag
%input:
%t=time (s)
%m=mass(kg)
%cd = second-order drag coefficient(kg/m)
%output:
%v=downward velocity (m/s)
g = 9.81; %acceleration due to gravity
v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);
Command Window Output:
>>freefall(12,68.1,.25)
ans =
50.6175
Now try changing the argument values
Important Takeaways
 User-defined functions in MATLAB improve readability and
reduces overall amount of code.
 They can be used to accomplish calculations or perform
subroutines within a program.
 Function files must be saved in the same directory as the
script file that uses them.
Preview of Next Class
 Functions 2
• Having to share many function files along
with your script file can be tedious
• MATLAB offers a way to condense all this into
a single file
What’s Next?
 Review today’s Quiz #14
 Open the in-class activity from the EEIC website
and we will go through it together.
 Then, start working on MAT-14 homework.
 Prepare for the next class by reading about
MATLAB Functions 2.
Download