Function M-File Numerical Computing with. MATLAB for Scientists and Engineers.

Slides:



Advertisements
Similar presentations
Introduction To MATLAB Prof. Muhammad Saeed Mathematical Modeling and Simulation.
Advertisements

Lecture 5.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MATLAB Functions – Part II Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September 2013.
Introduction to Matlab & Data Analysis
Programming with MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Functions.
General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
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.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
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.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
A Brief introduction to MatLAB ($50 - $99 for students)
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Functions CS 103 March 3, Review A function is a set of code we can execute on command to perform a specific task A function is a set of code we.
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.
Recap Function M-files Syntax of Function M-Files Comments Multiple Input and Output Functions.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Scientific Computing Introduction to Matlab Programming.
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.
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.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
ME 123 Computer Applications I Lecture 16: More Matlab Programming: Secant Method, Review 4/4/03.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
Matlab Training Session 4: Control, Flow and Functions
Introduction to Programming for Mechanical Engineers
Scripts & Functions Scripts and functions are contained in .m-files
User Defined Functions
2. Primary and Subfunctions
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Using Script Files and Managing Data
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

Function M-File Numerical Computing with. MATLAB for Scientists and Engineers

You will be able to Write a function m-file, Tell the differences between local and global variables, Use sub-functions and nested functions. 2

Anatomy of Function 3 mmempty.m function d = mmempty(a,b) %MMEMPTY Substitute Value if EMpty % MMEMPTY(A,B) returns A if A is not empty, % otherwise B is returned. % if isempty(a) d = b; else d = a; end function d = mmempty(a,b) %MMEMPTY Substitute Value if EMpty % MMEMPTY(A,B) returns A if A is not empty, % otherwise B is returned. % if isempty(a) d = b; else d = a; end keyword H1 Line (lookfor) input arguments output arguments function name = file name 1~63 lower case alphanumeric characters function name = file name 1~63 lower case alphanumeric characters Help Message Function Body

Demo: Simple Function Write a function m-file 'func1.m' for the following expression. Run the function in the command window. 4 func1.m >> func1(4) >> a = 0:0.1:5; >> plot(a, func1(a)) >> a = 0:0.1:5; >> plot(a, func1(a))

Input – Output Arguments Various forms of function definitions 5 function [mpay, tpay] = loan(amount, rate, years) function [A] = RectArea(a, b) function A = RectArea(a, b) function [V, S] = SphereVolArea(r) function trajectory(v, h, g)

Exercise 1: Polar Function Write a function m-file for Use the function to calculate: Use the function to plot (polar plot) for 6

Solution 1 Function m-file Commands and Screenshot 7

Local and Global Variables All variables in a function m-file is local. Local variables are not shared between function files / the command window. Use global keyword for global variables. 8 global SPEED_OF_LIGHT;

Script and Function M-Files ItemScript m-fileFunction m-file extension.m 1 st lineanyfunction definition variable recognized in command window local to the function usagebatch jobfunction, procedure I / Othrough variablesthrough arguments file nameanythe function name 9

Anonymous Function Simple one-line user-defined function Examples Usage 10 name (arglist) expr cube (x) x^3 ex3r (x, y) sqrt(x.^2+y.^2) y = cube(23.4) z = ex3r( [1 2], [2 0])

Exercise 2: Set of Parabolic Curves Define an anonymous function for where a and b are scalar values while x can be a vector. Use the anonymous function to plot a set of parabolic curves for -5<x<5 with a=-b= 1, 1.5, 2,

Solution 2 Script and Screenshot 12 parabolas.m

Sub-functions 1/2 A function m-file may contain several function definitions. The 1 st function is the primary function and the others are sub-functions, which are available only to the primary function. 13 f1.m funciton y = f1( a, b, c ) % f1 is the main funciton z = f2(a); w = f3(b,c); y = z.* w; function k = f2( x)... funciton y = f1( a, b, c ) % f1 is the main funciton z = f2(a); w = f3(b,c); y = z.* w; function k = f2( x)... Why sub-functions? Divide and conquer. Why sub-functions? Divide and conquer.

Sub-functions 2/2 Variables in each function are private to the function. Only the primary function may be called from outside. Each function can call each other in the function m-file. 14

Exercise 3 Determinant of 3x3 Matrix Write a function m-file, det3x3.m, for calculating the determinant of 3x3 matrix using the following formula. Use d = det3x3(A), where A is a 3x3 matrix. Use a sub-function that calculates the 2x2 determinant. Calculate the determinant of the matrices on the right. 15

Solution 3 Function m-file 16 det3x3

Solution 3 Commands and Screenshot 17

Variable Number of Arguments 0+ Input Arguments / 0+ Output Arguments Can be called with fewer arguments nargin : # of actual input arguments nargout : # of actual output arguments 18