Lecture 5 MATLAB programming (3)

Slides:



Advertisements
Similar presentations
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.
Advertisements

MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Al-Amer An Introduction to MATLAB Lesson 2: M-files Dr. Samir Al-Amer Term 061.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
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.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Hydroinformatics: Session4 Dr Ivan Stoianov Room 328B Dr Andrew Ireson (Room 304) Mr Juan Rodriguez-Sanchez (411A) Mr Baback.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: User Input / Output Programming Environment.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
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.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
Matlab Programming for Engineers
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
Creating a script create new blank document. Editor options Docking and undocking tabs.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Introduction to Matlab PHY September Why Matlab? No need to compile code Huge amount of built-in functions Highly optimized and fast, in general.
Matlab Programming for Engineers
User-Written Functions
MATLAB – More Script Files
EEE 161 Applied Electromagnetics
CS005 Introduction to Programming
L2. Basics Variables and Expressions Assignment Statements
Matlab Training Session 4: Control, Flow and Functions
Chapter 4 MATLAB Programming
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Functions.
Counted Loops.
User Defined Functions
Lecture 4 MATLAB programming (2)
Control Structure Senior Lecturer
Arrays An Array is an ordered collection of variables
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
MATLAB – Basic For Loops
Use of Mathematics using Technology (Maltlab)
Value returning Functions
Lecture 7 MATLAB programming (5)
Lecture 2 Introduction to MATLAB
Communication and Coding Theory Lab(CS491)
Writing Functions( ) (Part 4)
Computational Methods 2018/2019 Fall Chapter 2-B
Islamic University of Gaza
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.
Lets Play with arrays Singh Tripty
Using Script Files and Managing Data
Arrays in MatLab Arrays can have any number dimensions
Creating a script create new blank document.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Redundant code repositories
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

Lecture 5 MATLAB programming (3) Dr .Qi Ying

Objectives User-defined Functions

A Simple Example: factorial Write a program to calculate the factorial: N!=1, N=0 N!=N*(N-1)*(N-2)…*3*2*1, N>0 n=input('Please enter N:'); n_factorial=1; for ii=1:n n_factorial=n_factorial*ii; end disp ([ num2str(n),'!=',num2str(n_factorial) ]);

A Simple Example: factorial We can develop a general MATLAB function that calculates N!, given N. List of factorial.m function y = factorial( n ) %FACTORIAL Calculate the factorial of n % Function FACTORIAL calculates the factorial of n: % n!=1*2*...*(n-1)*n % % Written by: Qi Ying (Jan 2011) % CVEN 302 y=1; for ii=1:n % calculate N! y=y*ii; end

A Simple Example: factorial Calling the user-defined function: List of test1.m % demonstrates how to call the user % defined function 'factorial' for ii=0:5 n_factorial=factorial(ii); fprintf ('%d!=%d\n',ii,n_factorial); end >> test1 0!=1 1!=1 2!=2 3!=6 4!=24 5!=120

A Simple Example: factorial Some definitions: List of factorial.m function y = factorial( n ) %FACTORIAL Calculate the factorial of n % Function FACTORIAL calculates the factorial of n: % n!=1*2*...*(n-1)*n % % Written by: Qi Ying (Jan 2011) % CVEN 302 y=1; for ii=1:n y=y*ii; end The M-file is called factorial.m. n is called the input argument y is called the output argument The first comment line is called the H1 comment line, which is searchable by the ‘lookfor’ command The remaining comment lines until the first blank line or executable line is displayed by the ‘help’ command Loop variable ii is a ‘local’ variable, only visible inside the function.

General form of a MATLAB function function [outarg1, outarg2, …] = fname (inarg1, inarg2, …) %H1 comment line % other comment lines % % more comment lines (executable code) … end

Another example: statistics function [ mean, std_dev ] = stat1( x ) %STAT1 Calculate the mean and standard deviation of x. sum_x=0; sum_x2=0; n=length(x); % number of data points in x % loop over each data point in x to calculate sum(x) and sum(x^2) for ii=1:n sum_x=sum_x+x(ii); sum_x2=sum_x2+x(ii)^2; end % return the mean and standard deviation mean=sum_x/n; std_dev=sqrt((n*sum_x2-sum_x^2)/(n*(n-1))); end % end function avg1

Another example: statistics List of test3.m x=[1 2 3 4 5] [m1,s1]=stat1(x); fprintf('mean=%f, std=%f\n',m1,s1); % generate some random numbers x=rand(1,5) [m2,s2]=stat1(x); fprintf('mean=%f, std=%f\n',m2,s2); Results >> test3 x = 1 2 3 4 5 mean=3.000000, std=1.581139 0.1419 0.4218 0.9157 0.7922 0.9595 mean=0.646217, std=0.352429

Variable Passing in MATLAB Remember: MATLAB communicate with the functions using a ‘pass-by-value’ scheme. See the following example: List of func1.m function y = func1( x ) fprintf('In the beginning of func1, x=%f\n',x); for ii=1:3 x=x+1; fprintf('In func1: x=%f\n',x); end List of test2.m x=5; fprintf('Before func1, in the main program x=%f\n',x); func1(x); fprintf('After func1, in the main program x=%f\n',x);

Variable Passing in MATLAB >> test2 Before func1, in the main program x=5.000000 In the beginning of func1, x=5.000000 In func1: x=6.000000 In func1: x=7.000000 In func1: x=8.000000 After func1, in the main program x=5.000000 Output from func1 Pass-by-value is important for data isolation The value of x in the main program (test2.m) is passed into the function func1 to the first input argument, x. However, the argument x in the function is isolated from the main program. No changes of the argument will be visible to the main program.

In-class Exercise Write a MATLAB function that returns the minimum and maximum values of a 2D array and also return their row and column indexes