MATLAB FunctionsENGR 1181Presentation by Cody Allison
Today’s Topics What is a function? Basic function structure Function Examples
What is a function? MatLab has built-in functions that we have been
using all semester such as sum, length, mean, and many 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: 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 fileIn order to do this, the function must be saved in your active directory.
Opening a function fileOpen 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
Required if using nested functions, optional otherwise
**Function definition line
**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 & outputLet’s calculate how many oreos are left in a box using a function file:1. Open a new function file2. 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 oreos 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:x=3;
box=oreos(x)
ans = 19
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)
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;
SAVE YOUR FILE!
oreos_rev.m
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 fileusing oreos_rev.m
clcclearhrs=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)
Script file
How many hours of homework have you done today? 2How many midterms do you have this week? 1
You have 23 oreos left, you better do some more homework!
Output
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:
𝛿=𝐹 𝐿33𝐸𝐼
𝐼=𝑤𝑡312
𝜎=𝐹𝐴
function [stress, deflection] = beam_lab(w, t , F, L, E)Function definition line
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)
Then write a script file that calls the function file and uses these values.
w = .05 meterst = .01 metersF = 100 newtonsL = 1 meterE = 70 x 109 N/m2
What do you get?
stress = 200,000 N/m2
deflection = 0.1143 m
Questions?
Functions Examplefunction 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 gravityv=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