ENGR 1181 MATLAB 15: Functions 2

Similar documents
Chapter 6: User defined functions and function files

ENGR 1181 MATLAB 09: For Loops 2

MATLAB - Lecture # 4

ENGR 1181 MATLAB 05: Input and Output

MATLAB Introduction To Engineering for ECE Topics Covered: 1. Creating Script Files (.m files) 2. Using the Real Time Debugger

User Defined Functions

ENGR 1181 MATLAB 4: Array Operations

Full file at

MATLAB - FUNCTIONS. Functions can accept more than one input arguments and may return more than one output arguments.

This course is designed for anyone who needs to learn how to write programs in Python.

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Chapter 3: Programming with MATLAB

ENGR 105: Introduction to Scientific Computing. Dr. Graham. E. Wabiszewski

Lesson 1. Introduction to Programming OBJECTIVES

Matlab Tutorial: Basics

Object Orientation Fourth Story. Bok, Jong Soon

ENGR 1181 MATLAB 02: Array Creation

The authorindex Package

Computational Methods of Scientific Programming

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

ABOUT WEB TECHNOLOGY COURSE SCOPE:

Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Expr Language Reference

y = f (x) f(x) MATLAB MATLAB - Lecture # 7 Functions and Function Files / Chapter Function File

Introduction to MatLab. Introduction to MatLab K. Craig 1

Business Insight Authoring

Appendix A. The Preprocessor

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

Web Site Development with HTML/JavaScrip

2 Sets. 2.1 Notation. last edited January 26, 2016

Preprocessor Directives

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

JavaScript CS 4640 Programming Languages for Web Applications

CONDITIONAL STATEMENTS AND FLOW CONTROL

Two Key JDK 10 Features

CSE Theory of Computing Fall 2017 Project 3: K-tape Turing Machine

Class #15: Experiment Introduction to Matlab

IT6503 WEB PROGRAMMING. Unit-I

ECE 102 Engineering Computation

22-Functions Part 1 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

Shorthand for values: variables

2. INTRODUCTORY EXCEL

PTN-202: Advanced Python Programming Course Description. Course Outline

Copyright. Trademarks Attachmate Corporation. All rights reserved. USA Patents Pending. WRQ ReflectionVisual Basic User Guide

Embarcadero PowerSQL 1.1 Evaluation Guide. Published: July 14, 2008

Lecture Notes on Programming Languages

Eau Claire Area School District Curriculum Map FORM A

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

The Perl Debugger. Avoiding Bugs with Warnings and Strict. Daniel Allen. Abstract

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

Introduction to MATLAB programming: Fundamentals

6 Appendix B: Quick Guide to MATLAB R

Version 7.0. for WINDOWS NT 4.0 for WINDOWS 98 for WINDOWS 95. Programming with Reflection: Visual Basic User Guide.

Question Points Score Total 100

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

Parsing Combinators: Introduction & Tutorial

Terms you ll need to understand:

Lesson 1: Writing Your First JavaScript

Creating tables of contents

CSE 341, Autumn 2015, Ruby Introduction Summary

Solve the matrix equation AX B for X by using A.(1-3) Use the Inverse Matrix Calculator Link to check your work

Microsoft Windows PowerShell v2 For Administrators

Implement a virtual private database with fine-grained access control. Write code to interface with external C and Java applications.

My Favorite bash Tips and Tricks

EducAide Software. Instructions for producing an Acces-compatible database module (program version 3.41 or higher)

Desktop Command window

A Guide to Using Some Basic MATLAB Functions

CGS 3066: Spring 2015 JavaScript Reference

Improve your SAS applications by using functionality built in the MS Windows API, such as accessing external DLLs, OCXs and OLE servers.

User Defined Functions

A GUIDE TO WRITING TECHNICAL REPORTS

Introduction to MATLAB

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python

Getting started. Have you been installed correctly? If you need more screen space. Closing Sidebar. Map Sidebar User Guide: EMIS Web

5. Single-row function

Oracle Database: Program with PL/SQL

Chapter 4: Writing and Designing a Complete Program. Programming Logic and Design, Third Edition Introductory

HW 4 HINTS. 1. Chapter 6

biosignal acquisition tool-kit for advanced research applications MATLAB compatibility with biosignalsplux Python TM API

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Introduction to Programming Languages and Compilers. CS164 11:00-12:30 TT 10 Evans. UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164)

GSP - G&M codes extension to ACSPL+

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Introduction to Engineering gii

Python Scripting for Computational Science

Semester 2, 2018: Lab 1

CSE/Math 456 and CSE/Math 550 Matlab Tutorial and Demo

CA-MetaCOBOL + Online Programming Language Guide. Release 1.1 R203M+11DRP

The Language for Specifying Lexical Analyzer

ENGR 105: Introduction to Scientific Computing. Dr. Graham. E. Wabiszewski

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

8 Software Package Administration

A Document Class and a Package for handling multi-files projects

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

SOEE1160: Computers and Programming in Geosciences Semester /08. Dr. Sebastian Rost

Transcription:

ENGR 1181 MATLAB 15: Functions 2 Learning Objectives 1. Integrate user-written functions into the same file with the main program 2. Identify good function conventions, (e.g., the importance of placing functions at the Topics Students will read Chapter 6 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading. Students will learn more advanced techniques and syntax for creating and utilizing functions in MATLAB. This material contains the following: 1. Functions in Script Files 2. Using function main 3. Nested Functions 1. Functions in Script Files Previously, we discussed creating user-defined function files, that similar to a built-in function, is a stand alone script file that is accessed from within an executable MATLAB program. There are also ways to imbed function files directly inside the same script file. While there are certain limitations and constraints, this allows a script file to access a function without saving additional files. This reduces issues when sharing programs with others, or moving files, because there is only one file to keep track of rather than having multiple files. Limitations of Functions in Script Files Only one line functions can be included inside a script file There are two kinds of one line functions Inline functions Anonymous functions 1

Inline Functions This is an older format that may soon become obsolete. It is briefly covered for breadth of knowledge, but it is recommed to focus on Anonymous Functions for this class. The general format of an inline function is as follows: name =inline('math expression as a string') or name =inline('math expression as a string','arg1','arg2') - where order of arguments is specified; the default is alphabetical order The following is an example of an inline function: MofInertia=inline('b*h^3/12', 'h', 'b') % not alphabetical h=3; b=5; I=MofInertia(h,b); % define function before using it Some important notes about inline functions: First, define the function, then enter the values for input, and then call the function including the proper variable. Any letter except i and j may be used for indepent 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 (variables are defined AFTER defining the inline functions). A wrong approach is: o a=3; b=5; o Royal_Mess=inline('sqrt(a*x.^2+b*y.^2) ') Anonymous Functions This is a newer format for a function and is more readily accepted where the name becomes the function handle. This means that it points to where the function is stored in the computer. The general format of an anonymous function is as follows: name = @ (arglist) math_expression The following is an example of an anonymous function: MofInertia=@ (h,b) b*h^3/12 h=3; b=5; I=MofInertia(h,b); % again, function is defined before being used 2

Some important notes about anonymous functions: First, define the function, then enter the values for input, and then call the function including the proper variable. 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; Royal_Mess=@ (x,y) sqrt(a*x.^2+b*y.^2) NOTE: The predefined variables must be defined before the anonymous function is defined and NOT when it is used. New values for the predefined variables mean redefining the function! 2. Using function main Typically, only one-line functions can be included within a script file. MATLAB will allow a multiline function to be included at the of a script file if function main is used properly. Sing a directory of files to a user can be confusing for them and a pain to e-mail. Using function main streamlines this and allows users to save, use, and share just one file! Insert function main in the top line 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! It is imperative that MATLAB knows when the routine s, so it also requires an statement. One drawback of function main is that a function created inside that script file is not recognized by other script files. It must be manually copied into another script file or a separate function file made from it. 3

Function Main Example function main % script file commands function [output] = name_1(input) function [output] = name_2(input) Function Programming Insight Once function main and are placed at the beginning and the of the script, respectively, the script file can now use saved and ran. 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 functions are only understood inside of their respective file, general purpose functions should be as separate files in a user library. Include user-defined functions AFTER the statement for function main, not before! 4

3. Nested Functions Functions can be placed inside other functions (before the statement). These are called nested functions and follow the same basic layout as nested loops. An example of nested functions: Function out = namea (input) Function out2 = nameb(input2) 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 statement for the primary function). Place user-defined functions separately, each after the statement of the previous one if you want the functions to behave indepently and not share variable names and storage. Nest functions if you do want interaction, which is more similar to separate script files see the book and help menus for more details. 5