Programming with Matlab

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
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:
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Programming with Matlab Day 5: Debugging, efficiency, advanced types of variables, logical indices, images.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Guide To UNIX Using Linux Third Edition
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.
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
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.
Programmer Defined Functions Matthew Verleger. Windows It’s estimated that Window’s XP contains 45 million lines of code (and it’s over 10 years old).
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.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
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.
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.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
Engr 0012 (04-1) LecNotes script/function comparison scriptsfunctions Show program logic answer “what” questions Show program details answer “how”
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
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.
Matlab Programming for Engineers
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Introduction to Literate Programming in Matlab 2WN50 – Week programming-in-matlab.pptx?dl=0.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
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.
EEE 161 Applied Electromagnetics
Computer Application in Engineering Design
JavaScript: Functions
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Functions.
Functions (subprograms)
Agenda Control Flow Statements Purpose test statement
Engineering Innovation Center
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
Writing functions in MATLAB
Functions In Matlab.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Communication and Coding Theory Lab(CS491)
funCTIONs and Data Import/Export
Loop Statements & Vectorizing Code
INTRODUCTION TO MATLAB
Introduction to 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.
Selection Statements Chapter 3.
Matlab Basics Tutorial
Loop Statements & Vectorizing Code
Presentation transcript:

Programming with Matlab Day 3: Functions

Solving problems: Functions Function: Code that performs a specific task FUNCTION (Suma) INPUT (vector) OUTPUT (number)

Difference between functions and scripts Command Window >> vector=[1 2 3 4]; >> n_elems=length(vector); >> s=0; >> for c_elems=1:n_elems s=s+vector(c_elems); end >> Command Window = >> vector=[1 2 3 4]; >> suma >> n_elems=length(vector); s=0; for c_elems=1:n_elems s=s+vector(c_elems); end Editor The script uses previously defined variables (for example, vector). All variables defined in the script (i.e. n_elems, s, c_elems) remain in the workspace after the script is executed.

Difference between functions and scripts Command Window >> vector=[1 2 3 4]; >> n_elems=length(vec); >> s=0; >> for c_elems=1:n_elems s=s+vec(c_elems); end >> Command Window ≠ >> vector=[1 2 3 4]; >> s=suma(vector); >> function s=suma(vec) n_elems=length(vec); s=0; for c_elems=1:n_elems s=s+vec(c_elems); end Editor

Difference between functions and scripts vec Workspace Command Window >> vec=[1 2 3 4]; >> s=suma(vec); >> vec function s=suma(vector) n_elems=length(vector); s=0; for c_elems=1:n_elems s=s+vector(c_elems); end Editor Function suma Create variables Delete variables Change variables All the mess remains inside s A function can only access those workspace variables that are given to it as input arguments. A function only adds to the workspace those variables specified as output arguments. vec s Workspace

Function definition … Function's name. BUT: This name has no effect. The filename is the true name of the function (the one we will use to call and execute it). It is better that both names match. function function [output1,output2,…] = function_name (input1,input2,…) output1=0; for c=1:10 output1=output1+input1(c); end output2=‘Pues sí’; Editor - C:\Directorio\function_name.m … Output arguments From zero to many Between square brackets [] Separated by commas Code Input arguments From zero to many Between parenthesis () Separated by commas The values of output variables (output1, output2…) when the program finishes, are sent to the workspace.

Use of functions Editor - C:\Directorio\sumadorl.m function s = sumadorl (sumando1,sumando2) s = sumando1 + sumando2; Editor - C:\Directorio\sumadorl.m >> vector=[1 2 3 4]; >> suma=sumadorl(vector,10) suma = 11 12 13 14 >> Command Window

Use of functions Editor - C:\Directorio\sumadorl.m function s = sumadorl (sumando1,sumando2) s = sumando1 + sumando2; Editor - C:\Directorio\sumadorl.m Variable names do not need to match inside and outside the function (but they may match). >> vector=[1 2 3 4]; >> suma=sumadorl (vector,10) suma = 11 12 13 14 >> Command Window Input arguments may be either variables or just numbers/vectors typed directly.

Local Variables function s = sumadorl(sumando1,sumando2) s = sumando1 + sumando2; Editor - C:\Directorio\sumadorl.m Local variables: Used inside a function. Each function has its own Workspace, and they never get mixed. >> vector=[1 2 3 4]; >> suma=sumadorl(vector,10) suma = 11 12 13 14 >> Command Window Base Workspace: Used from Command Window. There may be variables with the same name in different workspaces, but they are completely different variables.

Turns out, we have been using functions all the time >> s = sin(2); Command Window Input Output

Logical operators AND, OR, NOT They may be nested. Examples: (a==b & c==d) | e>f a==b & (c==d | e>f) a==b & ~(c>d)

Efficiency: Preallocating variables >> a(1)=5; >> a(2)=3; >> a(3)=7; Reserve space for one number Three space requests Reserve space for another number Reserve space for another number >> a=zeros(1,3); >> a(1)=5; >> a(2)=3; >> a(3)=7; Reserve space for three numbers One space request MUCH FASTER Space was already reserved Specially important in loops If you do not know the final length from the beginning, preallocate plenty of space and cut the vector in the end