ECE 102 Engineering Computation

Size: px
Start display at page:

Download "ECE 102 Engineering Computation"

Transcription

1 ECE 102 Engineering Computation Phillip Wong MATLAB Function Files Nested Functions Subfunctions Inline Functions Anonymous Functions

2 Function Files A basic function file contains one or more function definitions that are stored in an M-file (text file). Properties of a function file: It can be invoked by any script or other function file that resides in the same directory or is in the current search path. It does not share its variable workspace with scripts or other function files, unless global variables are used. 1

3 Example: Write a MATLAB program that asks the user for a positive integer N, sums up the integers from 0 to N, and displays the result. Input: positive number N Output: summed value from 0 to N Algorithm: 1. Get N 2. R = N 3. Display R 2

4 R k = (k-1)+k Start Process: Prompt user R 0 = 0 R 1 = 0+1 = R 0 +1 Input N R 2 = = R 1 +2 R = 0, k = 0 R 3 = = R 2 +3 : k <= N? T R = R + k R k = R k-1 +k F k = k + 1 Display R End 3

5 % Non-modular program ver. 1 while true N=input('Enter N: '); if N > 0 break; R = 0; k = 0; while k <= N R = R + k; k = k + 1; fprintf('sum = %d\n', R) % Non-modular program ver. 2 while true N=input('Enter N: '); if N > 0 break R = 0; for k = 0:N R = R + k; fprintf('sum = %d\n', R) 4

6 ms_main.m (functions in three, separate M-files) mysum.m % Main script N = prompt_n; Nsum = mysum(n); fprintf('sum = %d\n', Nsum) prompt_n.m function N = prompt_n % Ask user for N and validate function R = mysum(n) % mysum adds # s from 0 to N % N is the max value to sum R = 0; for k = 0:N R = R + k; % function while true N=input('Enter N: '); if N > 0 break % function 5

7 Nested Functions A nested function is a function that is defined within the body of another function. Properties of a nested function: It can only be invoked by its parent function or by other nested functions belonging to that parent. Where it is defined within the parent function does not matter. It shares the variable workspace of its parent. 6

8 Example: Nested function function myfun a = 3; fprintf('1: a = %d\n', a) function zap (x) w = 7; fprintf('zap: x = %d\n', x) fprintf('zap: a = %d\n', a) fprintf('zap: w = %d\n', w) a = x; % function zap Output: 1: a = 3 zap: x = 99 zap: a = 3 zap: w = 7 3: a = 99 4: w = 7 zap(99) fprintf('3: a = %d\n', a) fprintf('4: w = %d\n', w) % function myfun 7

9 Subfunctions A function file can contain one primary function andpossibly multiple subfunctions. The primary function has the same name as the M-file. A subfunction is defined at the same level as the primary function. It is not nested within the primary. Properties of a subfunction: It can only be invoked by the primary function or by other subfunctions within the same M-file. It does not share the workspace of the primary function or other subfunctions. 8

10 Example: Bad subfunction version of nested example function myfun a = 3; fprintf('1: a = %d\n', a) zap(99) fprintf('3: a = %d\n', a) fprintf('4: w = %d\n', w) % primary function function zap (x) w = 7; fprintf('zap: x = %d\n', x) fprintf('zap: a = %d\n', a) fprintf('zap: w = %d\n', w) a = x; % subfunction This will fail with an error message because this w is local to myfun and has not been defined yet. This will fail with an error message because a is local to myfun and is not known inside zap. 9

11 Example: Subfunction Write a MATLAB program that calculates and plots the following piece-wise math function: f ( x) = 1 cos( x) log( x 3π + 1) 1 0 x < 0 x < 3π x 3π Version 1: Single script no functions 2: Functions in separate M-files 3: All functions in a single M-file 10

12 fpw_script.m (single script no functions) % Initialize & calculate x = [-10:0.1:20]; y = []; for k = 1:length(x) u = x(k); if u < 0 y(k) = 1; elseif 0 <= u && u < 3*pi y(k) = cos(u); else y(k) = log10(u - 3*pi + 1) - 1; % Show our results on a graph plot(x, y) axis([-10, 20, -2, 2]) xlabel('x') ylabel('y') title('piece-wise Function') 11

13 fpw_main.m (functions in separate M-files) piecewise.m (this contains two functions) % Main script % Initialize & calculate x = [-10:0.1:20]; % Domain y = piecewise(x); % Range % Show our results draw_graph(x, y) draw_graph.m function draw_graph ( x, y ) plot(x, y) axis([-10, 20, -2, 2]) xlabel('x') ylabel('y') title('piece-wise Function') % function function y = piecewise ( x ) for k = 1:length(x) u = x(k); if u < 0 y(k) = 1; elseif 0<= u && u < 3*pi y(k) = cos(u); else y(k) = fun_eqn(u); % primary function % =========================== function y = fun_eqn ( x ) y = log10(x - 3*pi + 1) - 1; % subfunction 12

14 fpw.m (all functions in a single M-file) function fpw x = [-10:0.1:20]; % Domain y = piecewise(x); % Range draw_graph(x, y) % primary function % =========================== function draw_graph ( x, y ) plot(x, y) axis([-10, 20, -2, 2]) xlabel('x') ylabel('y') title('piece-wise Function') % subfunction function y = piecewise ( x ) for k = 1:length(x) u = x(k); if u < 0 y(k) = 1; elseif 0<= u && u < 3*pi y(k) = cos(u); else y(k) = fun_eqn(u); % subfunction % =========================== function y = fun_eqn ( x ) y = log10(x - 3*pi + 1) - 1; % subfunction 13

15 Variable Classes in Functions Functions can work with three classes of variables: Local variable Defined inside a function Known only inside the function where it is defined Changing its value will not affect a variable with the same name outside the function When the function s, its value becomes undefined 14

16 Global variable Defined in a script or primary function using global keyword Example: global var Declared in a function using global keyword Changing its value inside a function will alter the value outside the function Persistent variable Defined in a function using persistent keyword Example: persistent var When the function s, the variable still exists and its value is retained between calls 15

17 Example: Write a program to control an LED that is connected to the LabJack's DAC0. Version 1: Single script no functions 2: Organized into functions single M-file 3: Load ljud_constants in the subfunction 4: Use global variables in the subfunction 16

18 VERSION 1: Single script no functions % test1 % This program flashes an LED that is connected to the LabJack's DAC0. ljud_loaddriver % Loads LabJack UD Function Library ljud_constants % Loads actual LabJack constants % Open LabJack and reset pin assignments [Error ljhandle] = ljud_openlabjack(lj_dtu3,lj_ctusb,'1',1); Error = ljud_eput (ljhandle, LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0); % Output 2V, 2.5V, 3V, 3.5V for 1.5 sec each voltage = [ ]; dtime = 1.5; % Sets DAC0 to a sequence of voltage values for k = 1:length(voltage) Error = ljud_eput(ljhandle,lj_ioput_dac,0,voltage(k),0); pause(dtime) Error = ljud_eput(ljhandle,lj_ioput_dac,0,0,0); % Reset DAC0 to 0 volts 17

19 VERSION 2: Organized into functions stored in single M-file function test2 ljud_loaddriver % Loads LabJack UD Function Library ljud_constants % Loads actual LabJack constants % Open LabJack and reset pin assignments [Error ljhandle] = ljud_openlabjack(lj_dtu3,lj_ctusb,'1',1); Error = ljud_eput (ljhandle, LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0); % Output 2V, 2.5V, 3V, 3.5V for 1.5 sec each set_dac(ljhandle,[ ],1.5) % primary function function set_dac (ljhandle, voltage, dtime) % Sets DAC0 to a sequence of voltage values. for k = 1:length(voltage) Error = ljud_eput(ljhandle,lj_ioput_dac,0,voltage(k),0); pause(dtime) This will fail with an error message because LJ_ioPUT_DAC is unknown in set_dac s workspace. Error = ljud_eput(ljhandle,lj_ioput_dac,0,0,0); % Reset DAC0 to 0 volts % subfunction 18

20 VERSION 3: Load ljud_constants in the subfunction function test3 ljud_loaddriver % Loads LabJack UD Function Library ljud_constants % Loads actual LabJack constants % Open LabJack and reset pin assignments [Error ljhandle] = ljud_openlabjack(lj_dtu3,lj_ctusb,'1',1); Error = ljud_eput (ljhandle, LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0); % Output 2V, 2.5V, 3V, 3.5V for 1.5 sec each set_dac(ljhandle,[ ],1.5) % primary function function set_dac (ljhandle, voltage, dtime) % Sets DAC0 to a sequence of voltage values. ljud_constants % Loads actual LabJack constants for k = 1:length(voltage) Error = ljud_eput(ljhandle,lj_ioput_dac,0,voltage(k),0); pause(dtime) This works but is inefficient because all the constants are loaded into set_dac s workspace. Error = ljud_eput(ljhandle,lj_ioput_dac,0,0,0); % Reset DAC0 to 0 volts % subfunction 19

21 VERSION 4: Use global variables in the subfunction function test4 ljud_loaddriver % Loads LabJack UD Function Library global LJ_ioPUT_DAC % Defines this as global so other functions can access it ljud_constants % Loads actual LabJack constants % Open LabJack and reset pin assignments [Error ljhandle] = ljud_openlabjack(lj_dtu3,lj_ctusb,'1',1); Error = ljud_eput (ljhandle, LJ_ioPIN_CONFIGURATION_RESET, 0, 0, 0); % Output 2V, 2.5V, 3V, 3.5V for 1.5 sec each set_dac(ljhandle,[ ],1.5) % primary function function set_dac (ljhandle, voltage, dtime) % Sets DAC0 to a sequence of voltage values. global LJ_ioPUT_DAC % Declares this global value to access for k = 1:length(voltage) Error = ljud_eput(ljhandle,lj_ioput_dac,0,voltage(k),0); pause(dtime) This gives access to the only constant needed by set_dac. Error = ljud_eput(ljhandle,lj_ioput_dac,0,0,0); % Reset DAC0 to 0 volts % subfunction 20

22 Inline Function An inline functionis a quick way to evaluate a math equation at the command line or in a script. Syntax: fin = inline('equation') finis the name assigned to the inline function. equation is a string. Arguments can be passed to the inline function. 21

23 Example: F = inline('x^2 + 3*x 5'); F(3) 13 g = inline('x.*exp(-x.^2/5)'); g(0.5) g([0.1,0.5]) fun = inline('x/(x+y)'); z = 4 * fun(1,2) fun(g(0.5)/f(3), z)

24 Anonymous Function An anonymous functionis a more robust and modern version of the inline function. The function is anonymous because it is not named. Instead it returns a function handle. Syntax: fan list) math expression fan contains the function handle. input list contains the input arguments. 23

25 Example: F x^2 + 3*x 5; F(3) 13 g x.*exp(-x.^2/5); g(0.5) g([0.1,0.5]) fun x/(x+y); z = 4 * fun(1,2) fun(g(0.5)/f(3), z)

Chapter 3: Programming with MATLAB

Chapter 3: Programming with MATLAB Chapter 3: Programming with MATLAB Choi Hae Jin Chapter Objectives q Learning how to create well-documented M-files in the edit window and invoke them from the command window. q Understanding how script

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 Functions Recall that an algorithm is a feasible solution to the specific problem. 1 A function is a piece of computer code that accepts

More information

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices Introduction to Interactive Calculations Matlab is interactive, no need to declare variables >> 2+3*4/2 >> V = 50 >> V + 2 >> V Ans = 52 >> a=5e-3; b=1; a+b Most elementary functions and constants are

More information

Scientific Computing with MATLAB

Scientific Computing with MATLAB Scientific Computing with MATLAB Dra. K.-Y. Daisy Fan Department of Computer Science Cornell University Ithaca, NY, USA UNAM IIM 2012 2 Focus on computing using MATLAB Computer Science Computational Science

More information

Mini-Matlab Lesson 5: Functions and Loops

Mini-Matlab Lesson 5: Functions and Loops Mini-Matlab Lesson 5: Functions and Loops Writing Functions and Scripts Contents Relational and logical operators IF loops FOR loops WHILE loops Scripts and functions Defining and using functions Anonymous

More information

A Tutorial on Matlab Ch. 3 Programming in Matlab

A Tutorial on Matlab Ch. 3 Programming in Matlab Department of Electrical Engineering University of Arkansas A Tutorial on Matlab Ch. 3 Programming in Matlab Dr. Jingxian Wu wuj@uark.edu OUTLINE 2 Plotting M-file Scripts Functions Control Flows Exercises

More information

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

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB SECTION 2: PROGRAMMING WITH MATLAB MAE 4020/5020 Numerical Methods with MATLAB 2 Functions and M Files M Files 3 Script file so called due to.m filename extension Contains a series of MATLAB commands The

More information

MATLAB Lecture 4. Programming in MATLAB

MATLAB Lecture 4. Programming in MATLAB MATLAB Lecture 4. Programming in MATLAB In this lecture we will see how to write scripts and functions. Scripts are sequences of MATLAB statements stored in a file. Using conditional statements (if-then-else)

More information

INTRODUCTION TO MATLAB Part2 - Programming UNIVERSITY OF SHEFFIELD. July 2018

INTRODUCTION TO MATLAB Part2 - Programming UNIVERSITY OF SHEFFIELD. July 2018 INTRODUCTION TO MATLAB Part2 - Programming UNIVERSITY OF SHEFFIELD CiCS DEPARTMENT Deniz Savas & Mike Griffiths July 2018 Outline MATLAB Scripts Relational Operations Program Control Statements Writing

More information

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

MATLAB - FUNCTIONS. Functions can accept more than one input arguments and may return more than one output arguments. MATLAB - FUNCTIONS http://www.tutorialspoint.com/matlab/matlab_functions.htm Copyright tutorialspoint.com A function is a group of statements that together perform a task. In MATLAB, functions are defined

More information

INTRODUCTION TO NUMERICAL ANALYSIS

INTRODUCTION TO NUMERICAL ANALYSIS INTRODUCTION TO NUMERICAL ANALYSIS Cho, Hyoung Kyu Department of Nuclear Engineering Seoul National University 0. MATLAB USAGE 1. Background MATLAB MATrix LABoratory Mathematical computations, modeling

More information

An Introduction to MATLAB

An Introduction to MATLAB An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu High level language Programing language and development environment Built-in development tools Numerical manipulation Plotting of

More information

Practice Reading for Loops

Practice Reading for Loops ME 350 Lab Exercise 3 Fall 07 for loops, fprintf, if constructs Practice Reading for Loops For each of the following code snippets, fill out the table to the right with the values displayed when the code

More information

User Defined Functions

User Defined Functions User Defined Functions 120 90 1 0.8 60 Chapter 6 150 0.6 0.4 30 0.2 180 0 210 330 240 270 300 Objectives Create and use MATLAB functions with both single and multiple inputs and outputs Learn how to store

More information

Introduction to MATLAB LAB 1

Introduction to MATLAB LAB 1 Introduction to MATLAB LAB 1 1 Basics of MATLAB MATrix LABoratory A super-powerful graphing calculator Matrix based numeric computation Embedded Functions Also a programming language User defined functions

More information

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline MATLAB Tutorial EE351M DSP Created: Thursday Jan 25, 2007 Rayyan Jaber Modified by: Kitaek Bae Outline Part I: Introduction and Overview Part II: Matrix manipulations and common functions Part III: Plots

More information

Chapters 6-7. User-Defined Functions

Chapters 6-7. User-Defined Functions Chapters 6-7 User-Defined Functions User-Defined Functions, Iteration, and Debugging Strategies Learning objectives: 1. Write simple program modules to implement single numerical methods and algorithms

More information

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures Introduction to Octave/Matlab Deployment of Telecommunication Infrastructures 1 What is Octave? Software for numerical computations and graphics Particularly designed for matrix computations Solving equations,

More information

Chapter 6: User defined functions and function files

Chapter 6: User defined functions and function files The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Computer Programming (ECIV 2302) Chapter 6: User defined functions and function files ١ 6.1 Creating a function file Input

More information

Desktop Command window

Desktop Command window Chapter 1 Matlab Overview EGR1302 Desktop Command window Current Directory window Tb Tabs to toggle between Current Directory & Workspace Windows Command History window 1 Desktop Default appearance Command

More information

MATLAB INTRODUCTION. Risk analysis lab Ceffer Attila. PhD student BUTE Department Of Networked Systems and Services

MATLAB INTRODUCTION. Risk analysis lab Ceffer Attila. PhD student BUTE Department Of Networked Systems and Services MATLAB INTRODUCTION Risk analysis lab 2018 2018. szeptember 10., Budapest Ceffer Attila PhD student BUTE Department Of Networked Systems and Services ceffer@hit.bme.hu Előadó képe MATLAB Introduction 2

More information

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment What is MATLAB? MATLAB PROGRAMMING Stands for MATrix LABoratory A software built around vectors and matrices A great tool for numerical computation of mathematical problems, such as Calculus Has powerful

More information

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB Fall 2014 MAT 375 Numerical Methods Introduction to Programming using MATLAB Some useful links 1 The MOST useful link: www.google.com 2 MathWorks Webcite: www.mathworks.com/help/matlab/ 3 Wikibooks on

More information

User Defined Functions

User Defined Functions User Defined Functions Aaron S. Donahue Department of Civil and Environmental Engineering and Earth Sciences University of Notre Dame February 27, 2013 CE20140 A. S. Donahue (University of Notre Dame)

More information

Question Points Score Total 100

Question Points Score Total 100 Name Signature General instructions: You may not ask questions during the test. If you believe that there is something wrong with a question, write down what you think the question is trying to ask and

More information

Programming 1. Script files. help cd Example:

Programming 1. Script files. help cd Example: Programming Until now we worked with Matlab interactively, executing simple statements line by line, often reentering the same sequences of commands. Alternatively, we can store the Matlab input commands

More information

Introduction to MATLAB

Introduction to MATLAB ELG 3125 - Lab 1 Introduction to MATLAB TA: Chao Wang (cwang103@site.uottawa.ca) 2008 Fall ELG 3125 Signal and System Analysis P. 1 Do You Speak MATLAB? MATLAB - The Language of Technical Computing ELG

More information

Finding, Starting and Using Matlab

Finding, Starting and Using Matlab Variables and Arrays Finding, Starting and Using Matlab CSC March 6 &, 9 Array: A collection of data values organized into rows and columns, and known by a single name. arr(,) Row Row Row Row 4 Col Col

More information

7 Control Structures, Logical Statements

7 Control Structures, Logical Statements 7 Control Structures, Logical Statements 7.1 Logical Statements 1. Logical (true or false) statements comparing scalars or matrices can be evaluated in MATLAB. Two matrices of the same size may be compared,

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 Functions The first thing of the design of algorithms is to divide and conquer. A large and complex problem would be solved by couples

More information

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis Introduction to Matlab 1 Outline What is Matlab? Matlab desktop & interface Scalar variables Vectors and matrices Exercise 1 Booleans Control structures File organization User defined functions Exercise

More information

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab Islamic University of Gaza Faculty of Engineering Electrical Engineering Department 2012 DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab Goals for this Lab Assignment: In this lab we would have

More information

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD Introduction to MATLAB for Numerical Analysis and Mathematical Modeling Selis Önel, PhD Advantages over other programs Contains large number of functions that access numerical libraries (LINPACK, EISPACK)

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab This tour introduces the basic notions of programming with Matlab. Contents M-file scripts M-file functions Inline functions Loops Conditionals References M-file scripts A script

More information

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable. Scope of Variables The variables used in function m-files are known as local variables. Any variable defined within the function exists only for the function to use. The only way a function can communicate

More information

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu Chapter 3 What we have done so far: Scripts/Functions have executed all commands in order, not matter what What we often need: A piece of code that executes a series of commands, if and only if some condition

More information

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY What is MATLAB? MATLAB (MATrix LABoratory) developed by The Mathworks, Inc. (http://www.mathworks.com) Key Features: High-level language for numerical

More information

Schedule. Matlab Exam. Documentation. Program Building Blocks. Dynamics Makeup Exam. Matlab Exam. Semester Summary and Wrap up.

Schedule. Matlab Exam. Documentation. Program Building Blocks. Dynamics Makeup Exam. Matlab Exam. Semester Summary and Wrap up. Schedule Dynamics Makeup Exam Thursday, May 1, 2:00 p.m., Estabrook 111 3 problems, covers modules 3 & 4 You may bring 1 sheet of notes Matlab Exam See next slide Semester Summary and Wrap up Wednesday,

More information

COMPUTER SKILLS LESSON 12. Valeria Cardellini A.Y. 2015/16

COMPUTER SKILLS LESSON 12. Valeria Cardellini A.Y. 2015/16 COMPUTER SKILLS LESSON 12 Valeria Cardellini cardellini@ing.uniroma2.it A.Y. 2015/16 11/25/15 Computer Skills - Lesson 12 - V. Cardellini 2 Objectives of this lesson We ll discuss Functions that return

More information

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

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB? Appendix A Introduction to MATLAB A.1 What Is MATLAB? MATLAB is a technical computing environment developed by The Math- Works, Inc. for computation and data visualization. It is both an interactive system

More information

EE 301 Signals & Systems I MATLAB Tutorial with Questions

EE 301 Signals & Systems I MATLAB Tutorial with Questions EE 301 Signals & Systems I MATLAB Tutorial with Questions Under the content of the course EE-301, this semester, some MATLAB questions will be assigned in addition to the usual theoretical questions. This

More information

Programming in MATLAB

Programming in MATLAB Programming in MATLAB Scripts, functions, and control structures Some code examples from: Introduction to Numerical Methods and MATLAB Programming for Engineers by Young & Mohlenkamp Script Collection

More information

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie 21-Loops Part 2 text: Chapter 6.4-6.6 ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie While Loop Infinite Loops Break and Continue Overview Dr. Henry Louie 2 WHILE Loop Used to

More information

3 Introduction to MATLAB

3 Introduction to MATLAB 3 Introduction to MATLAB 3 INTRODUCTION TO MATLAB 3.1 Reading Spencer and Ware 2008), secs. 1-7, 9-9.3, 12-12.4. For reference: Matlab online help desk 3.2 Introduction Matlab is a commercial software

More information

Introduction to Computer Programming with MATLAB Matlab Fundamentals. Selis Önel, PhD

Introduction to Computer Programming with MATLAB Matlab Fundamentals. Selis Önel, PhD Introduction to Computer Programming with MATLAB Matlab Fundamentals Selis Önel, PhD Today you will learn to create and execute simple programs in MATLAB the difference between constants, variables and

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab 1 Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control Using of M-File Writing User

More information

Introduction to Programming for Engineers Spring Midterm Examination. February 23, Questions, 45 Minutes

Introduction to Programming for Engineers Spring Midterm Examination. February 23, Questions, 45 Minutes Midterm Examination February 23, 2011 30 Questions, 45 Minutes Notes: 1. Before you begin, please check that your exam has 10 pages (including this one). 2. Write your name, student ID number, lab section,

More information

MatLab Just a beginning

MatLab Just a beginning MatLab Just a beginning P.Kanungo Dept. of E & TC, C.V. Raman College of Engineering, Bhubaneswar Introduction MATLAB is a high-performance language for technical computing. MATLAB is an acronym for MATrix

More information

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators. Relational and Logical Operators MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Both operators take on form expression1 OPERATOR expression2 and evaluate to either TRUE (1) or FALSE

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Violeta Ivanova, Ph.D. Office for Educational Innovation & Technology violeta@mit.edu http://web.mit.edu/violeta/www Topics MATLAB Interface and Basics Calculus, Linear Algebra,

More information

1 Introduction. 2 Useful linear algebra (reprise) Introduction to MATLAB Reading. Spencer and Ware (2008), secs. 1-7, 9-9.3,

1 Introduction. 2 Useful linear algebra (reprise) Introduction to MATLAB Reading. Spencer and Ware (2008), secs. 1-7, 9-9.3, Introduction to MATLAB Reading Spencer and Ware (2008), secs. 1-7, 9-9.3, 12-12.4. For reference: matlab online help desk 1 Introduction MATLAB is commercial software that provides a computing environment

More information

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms: Appendix A Basic MATLAB Tutorial Extracted from: http://www1.gantep.edu.tr/ bingul/ep375 http://www.mathworks.com/products/matlab A.1 Introduction This is a basic tutorial for the MATLAB program which

More information

ECE 202 LAB 3 ADVANCED MATLAB

ECE 202 LAB 3 ADVANCED MATLAB Version 1.2 1 of 13 BEFORE YOU BEGIN PREREQUISITE LABS ECE 201 Labs EXPECTED KNOWLEDGE ECE 202 LAB 3 ADVANCED MATLAB Understanding of the Laplace transform and transfer functions EQUIPMENT Intel PC with

More information

What is Matlab? The command line Variables Operators Functions

What is Matlab? The command line Variables Operators Functions What is Matlab? The command line Variables Operators Functions Vectors Matrices Control Structures Programming in Matlab Graphics and Plotting A numerical computing environment Simple and effective programming

More information

23-Functions Part 2 text: Chapter 7.3, ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

23-Functions Part 2 text: Chapter 7.3, ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie 23-Functions Part 2 text: Chapter 7.3, 7.11 ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie Overview Example Variable Passing Local and Global Variables Nested Functions Dr. Henry

More information

Chapter 1 Introduction to MATLAB

Chapter 1 Introduction to MATLAB Chapter 1 Introduction to MATLAB 1.1 What is MATLAB? MATLAB = MATrix LABoratory, the language of technical computing, modeling and simulation, data analysis and processing, visualization and graphics,

More information

EE 1105 Pre-lab 3 MATLAB - the ins and outs

EE 1105 Pre-lab 3 MATLAB - the ins and outs EE 1105 Pre-lab 3 MATLAB - the ins and outs INTRODUCTION MATLAB is a software tool used by engineers for wide variety of day to day tasks. MATLAB is available for UTEP s students via My Desktop. To access

More information

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Lisa A. Oberbroeckling Loyola University Maryland loberbroeckling@loyola.edu L. Oberbroeckling (Loyola University) MATLAB 10/07/10

More information

function [s p] = sumprod (f, g)

function [s p] = sumprod (f, g) Outline of the Lecture Introduction to M-function programming Matlab Programming Example Relational operators Logical Operators Matlab Flow control structures Introduction to M-function programming M-files:

More information

ROSE-HULMAN INSTITUTE OF TECHNOLOGY

ROSE-HULMAN INSTITUTE OF TECHNOLOGY EXAM 2 WRITTEN PORTION NAME SECTION NUMBER CAMPUS MAILBOX NUMBER EMAIL ADDRESS @rose-hulman.edu Written Portion / 40 Computer Portion / 60 Total / 100 USE MATLAB SYNTAX FOR ALL PROGRAMS AND COMMANDS YOU

More information

CS227-Scientific Computing. Lecture 3-MATLAB Programming

CS227-Scientific Computing. Lecture 3-MATLAB Programming CS227-Scientific Computing Lecture 3-MATLAB Programming Contents of this lecture Relational operators The MATLAB while Function M-files vs script M-files The MATLAB for Logical Operators The MATLAB if

More information

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations MATLAB Tutorial The following tutorial has been compiled from several resources including the online Help menu of MATLAB. It contains a list of commands that will be directly helpful for understanding

More information

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

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu 0. What is MATLAB? 1 MATLAB stands for matrix laboratory and is one of the most popular software for numerical computation. MATLAB s basic

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Dr./ Ahmed Nagib Mechanical Engineering department, Alexandria university, Egypt Spring 2017 Chapter 4 Decision making and looping functions (If, for and while functions) 4-1 Flowcharts

More information

C/C++ Programming for Engineers: Matlab Branches and Loops

C/C++ Programming for Engineers: Matlab Branches and Loops C/C++ Programming for Engineers: Matlab Branches and Loops John T. Bell Department of Computer Science University of Illinois, Chicago Review What is the difference between a script and a function in Matlab?

More information

Lecture 4: Complex Numbers Functions, and Data Input

Lecture 4: Complex Numbers Functions, and Data Input Lecture 4: Complex Numbers Functions, and Data Input Dr. Mohammed Hawa Electrical Engineering Department University of Jordan EE201: Computer Applications. See Textbook Chapter 3. What is a Function? A

More information

LEARNING TO PROGRAM WITH MATLAB. Building GUI Tools. Wiley. University of Notre Dame. Craig S. Lent Department of Electrical Engineering

LEARNING TO PROGRAM WITH MATLAB. Building GUI Tools. Wiley. University of Notre Dame. Craig S. Lent Department of Electrical Engineering LEARNING TO PROGRAM WITH MATLAB Building GUI Tools Craig S. Lent Department of Electrical Engineering University of Notre Dame Wiley Contents Preface ix I MATLAB Programming 1 1 Getting Started 3 1.1 Running

More information

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab MATH 495.3 (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab Below is a screen similar to what you should see when you open Matlab. The command window is the large box to the right containing the

More information

Lecture 1: Hello, MATLAB!

Lecture 1: Hello, MATLAB! Lecture 1: Hello, MATLAB! Math 98, Spring 2018 Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 1 / 21 Syllabus Instructor: Eric Hallman Class Website: https://math.berkeley.edu/~ehallman/98-fa18/ Login:!cmfmath98

More information

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

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An. CSE 10 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

CSE/Math 485 Matlab Tutorial and Demo

CSE/Math 485 Matlab Tutorial and Demo CSE/Math 485 Matlab Tutorial and Demo Some Tutorial Information on MATLAB Matrices are the main data element. They can be introduced in the following four ways. 1. As an explicit list of elements. 2. Generated

More information

FUNCTIONS ( WEEK 5 ) DR. USMAN ULLAH SHEIKH DR. MUSA MOHD MOKJI DR. MICHAEL TAN LONG PENG DR. AMIRJAN NAWABJAN DR. MOHD ADIB SARIJARI

FUNCTIONS ( WEEK 5 ) DR. USMAN ULLAH SHEIKH DR. MUSA MOHD MOKJI DR. MICHAEL TAN LONG PENG DR. AMIRJAN NAWABJAN DR. MOHD ADIB SARIJARI FUNCTIONS SKEE1022 SCIENTIFIC PROGRAMMING ( WEEK 5 ) DR. USMAN ULLAH SHEIKH DR. MUSA MOHD MOKJI DR. MICHAEL TAN LONG PENG DR. AMIRJAN NAWABJAN DR. MOHD ADIB SARIJARI OBJECTIVES Create Function 1) Create

More information

Chapter 4: Programming with MATLAB

Chapter 4: Programming with MATLAB Chapter 4: Programming with MATLAB Topics Covered: Programming Overview Relational Operators and Logical Variables Logical Operators and Functions Conditional Statements For Loops While Loops Debugging

More information

MATLAB TUTORIAL WORKSHEET

MATLAB TUTORIAL WORKSHEET MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent

More information

Matlab Advanced Programming. Matt Wyant University of Washington

Matlab Advanced Programming. Matt Wyant University of Washington Matlab Advanced Programming Matt Wyant University of Washington Matlab as a programming Language Strengths (as compared to C/C++/Fortran) Fast to write -no type declarations needed Memory allocation/deallocation

More information

MATH2070: LAB 3: Roots of Equations

MATH2070: LAB 3: Roots of Equations MATH2070: LAB 3: Roots of Equations 1 Introduction Introduction Exercise 1 A Sample Problem Exercise 2 The Bisection Idea Exercise 3 Programming Bisection Exercise 4 Variable Function Names Exercise 5

More information

1) Generate a vector of the even numbers between 5 and 50.

1) Generate a vector of the even numbers between 5 and 50. MATLAB Sheet 1) Generate a vector of the even numbers between 5 and 50. 2) Let x = [3 5 4 2 8 9]. a) Add 20 to each element. b) Subtract 2 from each element. c) Add 3 to just the odd index elements. d)

More information

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk Control Statements ELEC 206 Prof. Siripong Potisuk 1 Objectives Learn how to change the flow of execution of a MATLAB program through some kind of a decision-making process within that program The program

More information

5. Single-row function

5. Single-row function 1. 2. Introduction Oracle 11g Oracle 11g Application Server Oracle database Relational and Object Relational Database Management system Oracle internet platform System Development Life cycle 3. Writing

More information

Numerical Methods for Civil Engineers

Numerical Methods for Civil Engineers Numerical Methods for Civil Engineers Lecture 3 - MATLAB 3 Programming with MATLAB : - Script m-filesm - Function m-filesm - Input and Output - Flow Control Mongkol JIRAVACHARADET S U R A N A R E E UNIVERSITY

More information

Introduction to MATLAB

Introduction to MATLAB Quick Start Tutorial Introduction to MATLAB Hans-Petter Halvorsen, M.Sc. What is MATLAB? MATLAB is a tool for technical computing, computation and visualization in an integrated environment. MATLAB is

More information

Getting started with MATLAB

Getting started with MATLAB Sapienza University of Rome Department of economics and law Advanced Monetary Theory and Policy EPOS 2013/14 Getting started with MATLAB Giovanni Di Bartolomeo giovanni.dibartolomeo@uniroma1.it Outline

More information

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

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Programming for Experimental Research. Flow Control

Programming for Experimental Research. Flow Control Programming for Experimental Research Flow Control FLOW CONTROL In a simple program, the commands are executed one after the other in the order they are typed. Many situations require more sophisticated

More information

What is a Function? EF102 - Spring, A&S Lecture 4 Matlab Functions

What is a Function? EF102 - Spring, A&S Lecture 4 Matlab Functions What is a Function? EF102 - Spring, 2002 A&S Lecture 4 Matlab Functions What is a M-file? Matlab Building Blocks Matlab commands Built-in commands (if, for, ) Built-in functions sin, cos, max, min Matlab

More information

What is MATLAB and howtostart it up?

What is MATLAB and howtostart it up? MAT rix LABoratory What is MATLAB and howtostart it up? Object-oriented high-level interactive software package for scientific and engineering numerical computations Enables easy manipulation of matrix

More information

Scilab Programming. The open source platform for numerical computation. Satish Annigeri Ph.D.

Scilab Programming. The open source platform for numerical computation. Satish Annigeri Ph.D. Scilab Programming The open source platform for numerical computation Satish Annigeri Ph.D. Professor, Civil Engineering Department B.V.B. College of Engineering & Technology Hubli 580 031 satish@bvb.edu

More information

CGS 3066: Spring 2015 JavaScript Reference

CGS 3066: Spring 2015 JavaScript Reference CGS 3066: Spring 2015 JavaScript Reference Can also be used as a study guide. Only covers topics discussed in class. 1 Introduction JavaScript is a scripting language produced by Netscape for use within

More information

Basic MATLAB Tutorial

Basic MATLAB Tutorial Basic MATLAB Tutorial http://www1gantepedutr/~bingul/ep375 http://wwwmathworkscom/products/matlab This is a basic tutorial for the Matlab program which is a high-performance language for technical computing

More information

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

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain Introduction to Matlab By: Dr. Maher O. EL-Ghossain Outline: q What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control

More information

50 Basic Examples for Matlab

50 Basic Examples for Matlab 50 Basic Examples for Matlab v. 2012.3 by HP Huang (typos corrected, 10/2/2012) Supplementary material for MAE384, 502, 578, 598 1 Ex. 1 Write your first Matlab program a = 3; b = 5; c = a+b 8 Part 1.

More information

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

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name: 1 Matlab Tutorial 1- What is Matlab? Matlab is a powerful tool for almost any kind of mathematical application. It enables one to develop programs with a high degree of functionality. The user can write

More information

A Brief Introduction to MATLAB Evans Library Research Support Workshops

A Brief Introduction to MATLAB Evans Library Research Support Workshops A Brief Introduction to MATLAB Evans Library Research Support Workshops G.C. Anagnostopoulos 1 1 ICE Laboratory, Florida Institute of Technology November 4 th, 2015 The Roadmap 1 Introduction 2 Programming

More information

Flow Control. Spring Flow Control Spring / 26

Flow Control. Spring Flow Control Spring / 26 Flow Control Spring 2019 Flow Control Spring 2019 1 / 26 Relational Expressions Conditions in if statements use expressions that are conceptually either true or false. These expressions are called relational

More information

LECTURE 1. What Is Matlab? Matlab Windows. Help

LECTURE 1. What Is Matlab? Matlab Windows. Help LECTURE 1 What Is Matlab? Matlab ("MATrix LABoratory") is a software package (and accompanying programming language) that simplifies many operations in numerical methods, matrix manipulation/linear algebra,

More information

Lecture 6 MATLAB programming (4) Dr.Qi Ying

Lecture 6 MATLAB programming (4) Dr.Qi Ying Lecture 6 MATLAB programming (4) Dr.Qi Ying Objectives User-defined Functions Anonymous Functions Function name as an input argument Subfunctions and nested functions Arguments persistent variable Anonymous

More information

6 Appendix B: Quick Guide to MATLAB R

6 Appendix B: Quick Guide to MATLAB R 6 Appendix B: Quick Guide to MATLAB R 6.1 Introduction In this course we will be using the software package MATLAB R. Version 17.12.0, Release R2011a has been installed in Foster 100, the labs on the third

More information

ENGR 1181 MATLAB 15: Functions 2

ENGR 1181 MATLAB 15: Functions 2 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

More information

Branches, Conditional Statements

Branches, Conditional Statements Branches, Conditional Statements Branches, Conditional Statements A conditional statement lets you execute lines of code if some condition is met. There are 3 general forms in MATLAB: if if/else if/elseif/else

More information

Programming in MATLAB

Programming in MATLAB trevor.spiteri@um.edu.mt http://staff.um.edu.mt/trevor.spiteri Department of Communications and Computer Engineering Faculty of Information and Communication Technology University of Malta 17 February,

More information