ME313 Homework #5. Matlab- Functions

Similar documents
Matlab- Command Window Operations, Scalars and Arrays

1 Introduction to Matlab

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

The value of f(t) at t = 0 is the first element of the vector and is obtained by

Matlab and Vectors. next page. close. exit. Math 45 Linear Algebra. David Arnold.

AN INTRODUCTION TO MATLAB

ME 1020 Engineering Programming with MATLAB. Chapter 1 In-Class Assignment: 1.1, 1.3, 1.13, Topics Covered:

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

ELEMENTARY MATLAB PROGRAMMING

Repetition Structures Chapter 9

Dr Richard Greenaway

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

MATLAB TUTORIAL WORKSHEET

Desktop Command window

EGR 111 Introduction to MATLAB

Programming 1. Script files. help cd Example:

A Guide to Using Some Basic MATLAB Functions

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab

Computer Vision. Matlab

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Function f. Function f -1

7 Control Structures, Logical Statements

ECE 202 LAB 3 ADVANCED MATLAB

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

EGR 111 Functions and Relational Operators

Matlab Introduction. Scalar Variables and Arithmetic Operators

Introduction to Matlab

Getting To Know Matlab

Lab 4 CSE 7, Spring 2018 This lab is an introduction to using logical and comparison operators in Matlab.

Lecture 4: Complex Numbers Functions, and Data Input

Practice Reading for Loops

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

MATLAB Tutorial III Variables, Files, Advanced Plotting

Array Creation ENGR 1187 MATLAB 2

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

1 Overview of the standard Matlab syntax

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

EEE161 Applied Electromagnetics Laboratory 1

Lab 1 Introduction to MATLAB and Scripts

2 Second Derivatives. As we have seen, a function f (x, y) of two variables has four different partial derivatives: f xx. f yx. f x y.

Introduction to MATLAB

LAB 1 General MATLAB Information 1

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018)

A very brief Matlab introduction

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

MATLAB SUMMARY FOR MATH2070/2970

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

Getting Started With Mathematica

An Introductory Guide to MATLAB

ARRAY VARIABLES (ROW VECTORS)

Step by step set of instructions to accomplish a task or solve a problem

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

Programming in MATLAB

Matlab and Octave: Quick Introduction and Examples 1 Basics

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

University of Alberta

Lab 0a: Introduction to MATLAB

Computer Packet 1 Row Operations + Freemat

Trigonometric Functions of Any Angle

Surfaces and Partial Derivatives

Matlab and Matrices. Math 45 Linear Algebra. David Arnold

Topic 3. Mathematical Operations Matrix & Linear Algebra Operations Element-by-Element(array) Operations

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Chapter 1: An Overview of MATLAB

Dr Richard Greenaway

Boolean Logic & Branching Lab Conditional Tests

3.1 The Inverse Sine, Cosine, and Tangent Functions

AMS 27L LAB #1 Winter 2009

Getting Started With Mathematica

McTutorial: A MATLAB Tutorial

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

A Very Brief Introduction to Matlab

Laboratory 1 Octave Tutorial

To see what directory your work is stored in, and the directory in which Matlab will look for files, type

LECTURE 1. What Is Matlab? Matlab Windows. Help

Introduction to Matlab

Mathworks (company that releases Matlab ) documentation website is:

Introduction to MatLab. Introduction to MatLab K. Craig 1

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Introduction to MATLAB for Engineers, Third Edition

Statistics for Biologists: Practicals

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables

Introduction to MATLAB

1 Introduction to MATLAB

RELATIONAL AND LOGICAL OPERATORS

Transcription:

1 ME1 Homework #5 Matlab- Functions Last Updated February 9 2012. Assignment: Read and complete the suggested commands. After completing the exercise, copy the contents of the command window into Word or Adobe. Turn in a hardcopy of the file, and any M-files that you generate. Native functions in Matlab: You may not know it, but you have already used functions in Matlab. Consider the computation of the sine of an angle. At the command prompt, type x=0.25, and y=sin(x). The output should look like >> x=0.25 x = 0.2500 >> y=sin(x) 0.27 If you check with your calculator (and make sure that your calculator uses units of radians for degrees), you should get the same answer. Since the function sin is part of Matlab, it is known as a native function. In general, a function has three parts, visualized as sin From left to right, the three parts consist of an input (in this case x), a function name (in this case named sin), and an output (in this case y). The intent of a function is to compute outputs from inputs using a procedure that is black box in nature. For example, when the function sin was used above, the user does not need to know how the computation was made. This allows for development of modular code. More on this later. The function sin is more powerful that the simple use above. For example, type x=[0.2 0. -0.5], and then y=sin(x). You should obtain the following output >> x=[0.2 0. -0.5] x = 0.2000 0.000-0.5000 >> y=sin(x) 0.1987 0.2955-0.79 x y

2 It is apparent that the input x can be a row vector, and the function sin will respond with a row vector with elements corresponding to the sine of the same elements in the input. Making your own elementary function: A valuable capability of programming languages (and Matlab is a programming language), is the ability to write your own functions. Lets consider a very simple example, which will illustrate some of the rules for using functions. We will write a function that will compute the square of a scalar. Functions are stored in separate M-files. Open the M-file editor, and type in the following lines function y=sq(x) y=x^2 Then save the file as sq.m. There are several element of syntax, even in this simple function. For a function, the first line should always take the form function output=name(input). The purpose of function is to inform Matlab that this is a special type of M-file, i.e., a function instead of a script. output is the name of a variable that will be computed in the function. In this case, the output variable has been named y. name is the name of the function. In parenthesis, input is the name of a variable that will be the input. In this example, the input variable is named x. Later, we will see that there can be multiple inputs and multiple outputs. The job of the function is to compute an output given the input. In this example, this is accomplished by the line y=x^2. In general, there can be many lines in a function, their sole purpose being the computation of output variable(s) given the input variable(s). A second important factoid is that the name of the M-file should be the same as the name of the function. Hence, the function named sq must be stored in an M-file named sq.m. Now toggle to the command window, and type the lines x=2, y=sq(x). You should obtain the following output >> y=sq(x) The first occurrence of y= in the command window is caused by the line y=x^2 in the function sq. The second occurrence of y= in the command window takes

place after the assignment to y in the line y=sq(x) typed into the command window. Input and output variable names in functions are local: The variable names x for input and y for output in the function sq.m are local to the function, i.e., it is not required to use these names at the command window. At the command window, type the lines t=5, z=sq(t). You should obtain the following output >> z=sq(t) 25 z = 25 The function does not care that the input, named x in the function, it called t in the command window. It also does not mind that the output y in the function, computed from x in the function, is assigned to z in the command window. The input(s) and output(s) to functions are called dummy variables, in that they are defined by the placement in the function statement, not by the name. This will be clearer in later examples. All variables in a function are local to the function: Not only are input and output variables local to a function, all variables used in a function are local to the function. Suppose that we make the change to the function sq that it will add 2 to the square of a number. This can be accomplished by the following modification to sq.m function y=sq(x) a=2; y=x^2+a; Now there are two computational lines in the function square. It is evident that the output y will be computed by squaring the input x, and adding a value of 2. In adding a value of 2, an intermediate variable a was created. Furthermore, the command window output for the computational lines has been suppressed. Now type clear all in the command window. This will enable a fresh start. Type the commands t=, z=sq(t). Your output should be >> clear all >> t= t = >> z=sq(t) z = 18

The function responds as expected, the input and output variables x and y are local. However, at the command prompt, type a, followed by whos. You will obtain the following ouput in the command window >> a??? Undefined function or variable 'a'. t 1x1 8 double z 1x1 8 double It is apparent that the variable a does not exist. It only existed locally within the function sq. This is a very important feature of a programming language. It allows for modularity. If variables were not local, then assignments in one module of code would affect the values of variables of the same name in another module. Multiple inputs and outputs: As you might expect, it is possible for a function to have multiple inputs and multiple outputs. Open the M-file editor, and type in the following code for a function named sq2 function [z1,z2]=sq2(x,y) a=2; z1=x^2+a; z2=y^2-a; Make sure and save the function in the file named sq2.m. Multiple inputs and outputs have been implemented in the first line function [z1,z2]=sq2(x,y) There are two inputs, x and y. They are included in the parenthesis, separated by a comma. This is often called the input list, or argument list. Since variable names in the function are local, the interpretation of the meaning of the inputs and outputs are determined by their order in each list, not by their name. The outputs are placed in square brackets, separated by a comma. In the code statements, note that the outputs z1 and z2 are computed from the inputs and the variable a defined in the code.

5 Now, go to the command window, and type the commands clear all, w=5, v=6, and [q1,q2]=sq2(w,v), followed by whos. The output at the command window should look like >> w=5 w = 5 >> v=6 v = 6 >> [q1,q2]=sq2(w,v) q1 = 27 q2 = q1 1x1 8 double q2 1x1 8 double v 1x1 8 double w 1x1 8 double At the command window, the computation performed by the function sq2 were assigned to the variables q1 and q2. The variables x,y,a,z1,z2 do not exist in the command window, they existed locally in the function sq2. Input and output variables may be arrays: As you may further expect, input and output variables may be scalars and arrays. In fact the inputs and outputs can also be other kinds of data structures, which will be discussed in class. Using the M-file editor, open a new M-file and type in the following code function [z1,z2]=arrayexample(x,y) a=[1 ; 2 ; ] ; z1=x*a; z2=*y; Make sure and save the function in an M-file named arrayexample.m. Now go to the command window, and type clear all, w=[ 2 1;-9 8 7;0 2 ], v=, [q1,q2]=arrayexample(w,v), and then whos. You should get the following output at the command window

6 >> clear all >> w=[ 2 1;-9 8 7;0 2 ] w = 2 1-9 8 7 0 2 >> v= v = >> [q1,q2]=arrayexample(w,v) q1 = 10 28 16 q2 = 9 q1 x1 2 double q2 1x1 8 double v 1x1 8 double w x 72 double The function computes the outputs q1 and q2 as defined by the code in the function arrayexample. Again, the variables x,y,a,z1,z2 are local to the function, and do not exist in the command window. Note that the intention of the function is to return an output z1 that is a x1 column vector, and an output z2 that is a scalar. This is exactly what happened, as observed by the dimension of the output variables q1 and q2 in the command window. You must be aware of required variable types for inputs and outputs: On must pay careful attention to the requirements of the input and output lists for functions. In the previous example, the dimensions of the first and second arguments x and y were x and 1x1 (scalar) respectively. If you do not follow the intended requirements, you may get incorrect results or an error in the execution of the function. For example, suppose you repeat the command window sequence before, only you mix up the order of the input list for the function arrayexample. To do this, type clear all, w=[ 2 1;-9 8 7;0 2 ], v=, [q1,q2]=arrayexample(v,w), and then whos (only difference is v,w instead of intended w,v in the function call). You should obtain the following result in the command window

7 >> clear all >> w=[ 2 1;-9 8 7;0 2 ] w = 2 1-9 8 7 0 2 >> v= v = >> [q1,q2]=arrayexample(v,w) q1 = 6 9 q2 = 9 6-27 2 21 0 6 12 q1 x1 2 double q2 x 72 double v 1x1 8 double w x 72 double Now the results of the computation in the function are not what were intended. The output q1 is a scalar x1, and the output q2 is a x matrix. In this case, only the output of the code was incorrect. Many times, the code in the function assumes particular dimensions for the input variables for a valid matrix operation. If an input does not have the assumed dimension, and error will occur and execution will halt.