Functions 1 ENGR 1181 MATLAB 14.

Slides:



Advertisements
Similar presentations
Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane.
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
2D Plots 1 ENGR 1181 MATLAB 12.
Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Program Design ENGR 1181 MATLAB 11. MATLAB Program Design in Real Life Complex manufacturing processes require careful logic mapping to determine what.
Spring 2004ENGR 111A1 MATLAB – Palm Chapter 3, Part 2 Function Files Class 6.1 Chapter 3: Section 2.
Functions.
ENGG 1801 Engineering Computing MATLAB Lecture 2: Background for Week 4 Tutorial Simple Programming in MATLAB.
Insight Through Computing 11. User-Defined Functions Input parameters Local Variables Output Values.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
Visual Basic Chapter 1 Mr. Wangler.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
WJEC Applied ICT Spreadsheet Skills 1.Introduction to Financial Modelling Definition A model is a program which has been developed to copy the way.
CS 122 Engineering Computation Lab Lab 2 Dan De Sousa and Tim Cheeseman Department of Computer Science Drexel University April 2009 ©By the author. All.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
For Loops 1 ENGR 1181 MATLAB 8. For Loops and Looped Programming in Real Life Looping within programs has long been a useful tool for completing mundane.
Conditional Statements ENGR 1187 MATLAB 7. Conditional Statements in Real Life A Fluorescence Activated Cell Sorter (FACS) sorts cells based on predetermined.
MATLAB FUNDAMENTALS: MATRIX/ARRAY FUNCTIONS THE COLON MATRIX/ARRAY MANIPULATION INPUT/OUTPUT HP 100 – MATLAB Wednesday, 9/3/2014
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Functions CS 103. Review A function is a set of code we can execute on command to perform a specific task When we call a function, we can pass arguments.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Synthesis ENGR 1181 MATLAB 11. Topics  No new material  Covers topics that will be on the Midterm 2 Exam MATLAB 01 – Program Design MATLAB 02 – Introduction.
Outline Comparison of Excel and R R Coding Example – RStudio Environment – Getting Help – Enter Data – Calculate Mean – Basic Plots – Save a Coding Script.
Engr 0012 (04-1) LecNotes script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how”
Winter Semester 2014/2015 College of Engineering.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Introduction to MATLAB 7 MATLAB Programming for Engineer Hassan Migdadi Spring 2013.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.
2D Plots 2 ENGR 1181 MATLAB 13. Plotting in the Real World 2D plots generated by MATLAB can be used in a variety of fields, including the one shown here,
While Loops ENGR 1181 MATLAB 10.
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2012.
Array Operations ENGR 1181 MATLAB 4.
Intermediate 2 Computing Unit 2 - Software Development.
Teamwork and Problem Solving: Activities ENGR 1181 Class 2 – Part 2.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Laboratory 2. LABORATORY OBJECTIVES This lab work is intended to give the students more insights on the Matlab environment and how to work more efficiently.
FILE MANAGEMENT Computer Basics 1.3. FILE EXTENSIONS.txt.pdf.jpg.bmp.png.zip.wav.mp3.doc.docx.xls.xlsx.ppt.pptx.accdb.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
MATLAB – More Script Files
Python Lesson 12 Mr. Kalmes.
Chapter 6 Work and Machines
ENGG 1801 Engineering Computing
Python Lesson 12 Mr. Kalmes.
Lecture 1: Introduction
Welcome to the Fundamentals of Mathematics for Engineers Lab
Scripts In Matlab.
Redundant code repositories
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

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: Open a new function file 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: 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 file using oreos_rev.m 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) Then write a script file that calls the function file and uses these values. w = .05 meters t = .01 meters F = 100 newtons L = 1 meter E = 70 x 109 N/m2 What do you get? stress = 200,000 N/m2 deflection = 0.1143 m

Functions Example Now try changing the argument values 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.