Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Lecture 5.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
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:
The Web Warrior Guide to Web Design Technologies
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Introduction to C Programming
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
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.
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.
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.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
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.
Chapter 10 Review: Matrix Algebra
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Introduction to Python
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
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.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
1 Lab 2 of COMP 319 Lab tutor : Shenghua ZHONG Lab 2: Sep. 28, 2011 Data and File in Matlab.
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.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Recap Function M-files Syntax of Function M-Files Comments Multiple Input and Output Functions.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to Programming with RAPTOR
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
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.
Recap Sum and Product Functions Matrix Size Function Variance and Standard Deviation Random Numbers Complex Numbers.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
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 &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
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.
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
Digital Image Processing Introduction to M-function Programming.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Array and Matrix Operations EE 201 Fall  Achieve Comprehension LOL of Array and Matrix Operations. Class Learning Objectives 2.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Matlab Training Session 4: Control, Flow and Functions
Other Kinds of Arrays Chapter 11
JavaScript: Functions.
User Defined Functions
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.
Matlab Basics Tutorial
Presentation transcript:

Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6

Function M-files User-defined functions are stored as M-files and can be accessed by MATLAB if they are in the current folder or on MATLAB’s search path

Syntax of Function M-file Both built-in MATLAB ® functions and user-defined MATLAB functions have the same structure Each consists of a name, user-provided input, and calculated output. For example: the function cos(x) is named cos takes the user input inside the parentheses (in this case, x ) calculates a result The user does not see the calculations performed, but just accepts the answer User-defined functions work the same way Imagine that you have created a function called my_function Using my_function(x) in a program or from the command window will return a result, as long as x is defined and the logic in the function definition works

Continued…. User-defined functions are created in M-fi les. Each must start with a function- definition line that contains: The word function A variable that defines the function output A function name A variable used for the input argument For example: function output = my_function(x) is the first line of the user-defined function called my_function It requires one input argument, which the program will call x, and will calculate one output argument, which the program will call output The function name and the names of the input and output variables are arbitrary and are selected by the programmer Here’s an example of an appropriate fi rst line for a function called calculation : function result = calculation(a) In this case, the function name is calculation, the input argument will be called a in any calculations performed in the function program, and the output will be called result Although any valid MATLAB ® names can be used, it is good programming practice to use meaningful names for all variables and for function names

Continued…. Here’s an example of a very simple MATLAB function that calculates the value of a particular polynomial: function output = poly(x) %This function calculates the value of a third-order %polynomial output = 3*x.^3 + 5*x.^2 - 2*x +1; The function name is poly, the input argument is x, and the output variable is named output Before this function can be used, it must be saved into the current folder The file name must be the same as the function name in order for MATLAB to find it All of the MATLAB naming conventions we learned for naming variables apply to naming user-defined functions. In particular, The function name must start with a letter. It can consist of letters, numbers, and the underscore. Reserved names cannot be used. Any length is allowed, although long names are not good programming practice.

Continued…. Once the M-file has been saved, the function is available for use from the command window, from a script M-fi le, or from another function You cannot execute a function M-file directly from the M-file itself. This makes sense, since the input parameters have not been defined until you call the function from the command window or a script M-file Consider the poly function just created. If, in the command window, we type poly(4) then MATLAB responds with ans =265 If we set a equal to 4 and use a as the input argument, we get the same result: a = 4; poly(a) ans =265 If we define a vector, we get a vector of answers. Thus, y = 1:5; poly(y) gives ans = If, however, you try to execute the function by selecting the save-and-run icon from the function menu bar, the following error message is displayed: ???Input argument “x” is undefined. Error in ==> poly at 3 output = 3*x.^3 + 5*x.^2 - 2*x +1; The value of x must be passed to the function when it is used—either in the command window or from within a script M-file program.

Comments As with any computer program, code should be commented liberally so that it is easy to follow However, in a MATLAB function, the comments on the line immediately following the very first line serve a special role These lines are returned when the help function is queried from the command window Consider, for example, the following function: function results = f(x) %This function converts seconds to minutes results = x./60; Querying the help function from the command window help f returns This function converts seconds to minutes

Functions with Multiple Inputs and Outputs

Continued…. Similarly, a user-defined function could be written to multiply two vectors together: function output = g(x,y) % This function multiplies x and y together % x and y must be the same size matrices a = x.*y; output = a; When x and y are defined in the command window and the function g is called, a vector of output values is returned: x = 1:5; y = 5:9; g(x,y) ans = The comment lines can be used to let users know what kind of input is required and to describe the function In this example, an intermediate calculation ( a ) was performed, but the only output from this function is the variable we’ve named output This output can be a matrix containing a variety of numbers, but it’s still only one variable

Continued…. Such functions can also be created that return more than one output variable Many of the predefined MATLAB functions return more than one result For example: max returns both the maximum value in a matrix and the element number at which the maximum occurs. To achieve the same result in a user-defined function, make the output a matrix of answers instead of a single variable, as in function [dist, vel, accel] = motion(t) % This function calculates the distance, velocity, and % acceleration of a particular car for a given value of t % assuming all 3 parameters are initially 0. accel = 0.5.*t; vel = t.^2/4; dist = t.^3/12; Once saved as motion in the current folder, you can use the function to find values of distance, velocity, and acceleration at specified times: [distance, velocity, acceleration] = motion(10) distance =83.33 velocity =25 acceleration =5 If you call the motion function without specifying all three outputs, only the first output will be returned: motion(10) ans =83.333

Continued…. Remember, all variables in MATLAB are matrices, so it’s important in the preceding example to use the.* operator, which specifies element-by-element multiplication For example: using a vector of time values from 0 to 30 in the motion function time = 0:10:30; [distance, velocity, acceleration] = motion(time) returns three vectors of answers: distance = velocity = acceleration = It’s easier to see the results if the vectors are grouped together, as in results = [time',distance',velocity',acceleration'] which returns results = Because time, distance, velocity, and acceleration were row vectors, the transpose operator was used to convert them into columns