ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.

Slides:



Advertisements
Similar presentations
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Advertisements

Programming with MATLAB
Chapter 41 Defining Classes and Methods Chapter 4.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
Guide To UNIX Using Linux Third Edition
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
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.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
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.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Functions 1 ENGR 1181 MATLAB 14.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
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.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Input, Output, and Processing
Announcements HW3 grades will be announced this week HW4 is due this week Final exam on August 25, 2010, Wednesday at 09:00 Duration is about 140 minutes.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
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.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
Fundamentals of Python: First Programs
Topics Designing a Program Input, Processing, and Output
MATLAB – More Script Files
1-1 Logic and Syntax A computer program is a solution to a problem.
Lecture: MATLAB Chapter 1 Introduction
Introduction to Programming for Mechanical Engineers
Other Kinds of Arrays Chapter 11
Scripts & Functions Scripts and functions are contained in .m-files
CS190/295 Programming in Python for Life Sciences: Lecture 1
funCTIONs and Data Import/Export
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Using Script Files and Managing Data
Presentation transcript:

ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions Anonymous Functions 2.Use of function main 3.Nested Functions More about Functions / Chapter 6

ENG Script File Limitations  Only one line functions can be included inside a script file  There are two kinds of one line functions o Inline functions o Anonymous functions

ENG Inline functions  Older format, may become obsolete name =inline('math expression as a string') or name =inline('math expression as a string','arg1','arg2') % where order of arguments is specified, default is % alphabetical order For example, MofInertia=inline('b*h^3/12', 'h', 'b') % not alphabetical h=3; b=5; I=MofInertia(h,b); % define function before using it

ENG Inline functions  Any letter except i and j may be used for independent variables  The one line math expression can use any built-in or user-defined functions  Include dot operators if the input variables can be arrays  The expression CANNOT use predefined variables, e.g. a=3; b=5; Royal_Mess=inline('sqrt(a*x.^2+b*y.^2) ')

ENG Anonymous functions  Newer format, name becomes function handle (points to where function is stored in computer) name (arglist) math_expression For example, (h,b) b*h^3/12 h=3; b=5; I=MofInertia(h,b);  The anonymous function needs to be defined before it is used

ENG Anonymous functions  The one line math expression can use any built-in or user-defined functions  Include dot operators when using arrays for input  The math expression CAN use predefined variables, a=3; b=5; (x,y) sqrt(a*x.^2+b*y.^2) However!, the predefined variables must be defined before the anonymous function is defined – NOT when it is used. New values for the predefined variables mean redefining the function!!!

ENG MATLAB will not allow a multi-line function to be included at the end of a script file! Sending a directory of files to a user can be confusing for them and a pain to Wouldn’t it be nice to just use one file? What if you want everything in one file?

ENG Function Main MATLAB, however, does allow the user to place a function inside another function. Thus if you add the line function main to the top of your script file, i.e. a function with no input variables and no output variables, then you can add multi-line user defined functions to the file. This top level still acts like and should be used as a script file! When you do this, MATLAB needs to know when the main routine ends. This is done with an end statement 176

ENG Function Main Example function main % script file commands... end function [output] = name_1(input)... end function [output] = name_2(input)... end 176

ENG Programming Insight You can now use save and run. This will run the top level script file (function main) and call each included function when that line of the script file is reached. ALL functions in a file will only be understood inside that file, not in other files. Because of this, general purpose functions should be as separate files in a user library Include user-defined functions AFTER the end statement for “function main”, not before!

ENG Nested Functions  Functions can be placed inside other functions (before the end statement). These are called Nested functions. Example: Function out = nameA (input) Function out2 = nameB(input2) … end 178

ENG Nested Functions  Nested Functions share variable names and values, just like between script files but with limitations. This can cause problems if you do not want such interactions!  For example, a variable defined in a primary function is recognized and can be redefined by any functions contained within it (before the end statement for the primary function) 178

ENG Nested Functions In Summary:  Place user-defined functions separately, each after the end statement of the previous one if you want the functions to behave independently and not share variable names and storage  Nest functions if you do want interaction (more similar to separate script files – see the book and help menus for more details) 178

ENG ASSIGNMENT : MAT #10 per course homework packet