Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Lecture 5.
Programming with MATLAB
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Functions MATLAB’s power comes from functions Functions allow projects to be partitioned into manageable, testable, reusable modules.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
EGR 106 – Week 8 Data Files & Functions Interacting with Data Files Functions – Concept – Examples and applications Textbook chapter ,
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
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.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
INTRODUCTION TO MATLAB LAB# 01
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.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
E177, Review of Matlab Spring, Copyright , Andy Packard. This work is licensed under the Creative Commons.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Scientific Computing Introduction to Matlab Programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Introduction to Matlab
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Introduction to Literate Programming in Matlab 2WN50 – Week programming-in-matlab.pptx?dl=0.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Introduction to Matlab PHY September Why Matlab? No need to compile code Huge amount of built-in functions Highly optimized and fast, in general.
Lecture: MATLAB Chapter 1 Introduction
Scripts & Functions Scripts and functions are contained in .m-files
Lecture 1: Introduction
Functions In Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Programming with Matlab
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Programming The ideal style of programming is Structured or
Presentation transcript:

Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Script Files Script files text files (created with a text editor) contain many lines of MATLAB expressions and assignments. You can execute all of the lines of the file (sequentially) by typing the filename at the MATLAB prompt. If filename is computetraj.m, then >> computetraj causes the instructions (ie., the MATLAB commands) in the file to be run.

Functions (35 minutes) Basics –Function declaration line (3) –Input and output arguments (3) –Call by value, but with delayed copy (2) –Function workspaces, recursion, towers example (7) –Special features (4) nargin, nargout, varargin, varargout, assignin, evalin Subfunctions (4) Nested functions (4) Anonymous functions (4) Function_handles (4)

Script file example Suppose a file called runthis.m contains the text X = linspace(0,4*pi,1000); Y = sin(3*X) + 2*cos(5*X); plot(X,Y) maxy = max(abs(Y)); disp([’Peak of Y is ’ num2str(maxy)]); Then, typing >> runthis will cause the 5 lines to execute, resulting in a plot and a message about the peak value of Y.

Functions (Chapter 11, pg ) In mathematics, a function is a rule that assigns to each value of the input, a corresponding output value. Consider the function f defined by the rule f(x) = x 2 for all numbers x. Here, x is the input value, and f(x) is the output value. Equivalently, we could have written the rule as f(y) = y 2 for all numbers y. Functions can have many inputs and produce (through multiple rules) many outputs, f 1 (a,b,c) = 2a+3b, f 2 (a,b,c) = bc.

FUNCTIONS in programming If you have a “complex” task (eg., more than two lines of Matlab program code) that you will reuse in a few (or several places), you will want to write that task as a reusable function file. Example: trigonometric SIN function Then, every time you need to execute that task, you will only need to “call” the function. This modularity helps break down a huge program task into a collection of smaller tasks, which individually are easier to design, write, debug and maintain. functions are sometimes also called subroutines, methods, etc.

A MATLAB function file The first line is the function declaration line. function [dp,cp] = vecop(v,w) The function name, this function should be saved in a file called vecop.m The input variables. This function has two. Within the function, the names of the input variables are v and w. The output variables. This function has two. The function’s purpose is to compute these variables, based on the values of the input variables. The input and output variables are also called the input and output arguments.

Picture of VECOP function InputArgument #1 InputArgument #2 OutputArgument #1 OutputArgument #2 VECOP

6-line function in vecop.m function [dp,cp] = vecop(v,w) dp = sum(v.*w); cp = zeros(3,1); cp(1) = v(2)*w(3) – w(2)*v(3); cp(2) = v(3)*w(1) – w(3)*v(1); cp(3) = v(1)*w(2) – w(1)*v(2); Logically correct expressions and assignments that compute the output variables using the values of the input variables. Function declaration line

Comments and blank lines add readability function [dp,cp] = vecop2(v,w) % VECOP computes dot product and cross % product of two 3-by-1 vectors. dp = sum(v.*w); cp = zeros(3,1); % create 3-by-1 % Fill cp cp(1) = v(2)*w(3) – w(2)*v(3); cp(2) = v(3)*w(1) – w(3)*v(1); cp(3) = v(1)*w(2) – w(1)*v(2); comments

Calling a function >> >> v1 = [1;-2;3]; >> v2 = [0;1;1]; >> [A,B] = vecop(v1,v2); >> function [dp,cp] = vecop(v,w) dp = sum(v.*w); cp = zeros(3,1); cp(1) = v(2)*w(3)-w(2)*v(3); cp(2) = v(3)*w(1)-w(3)*v(1); cp(3) = v(1)*w(2)-w(1)*v(2); BASE WORKSPACE v1 3-by-1 v2 3-by-1 A 1-by-1 B 3-by-1 FUNCTION INSTANCE WS v 3-by-1 w 3-by-1 dp 1-by-1 cp 3-by-1

Can we see evidence of this ( evalin )? function [dp,cp] = vecop3(v,w) disp(’Just inside VECOP3 function’) disp(’ FunctionWS’); whos disp(’ CallerWS’); evalin(’caller’,’whos’); dp = sum(v.*w); disp(’After DP create’) disp(’ FunctionWS’); whos disp(’ CallerWS’); evalin(’caller’,’whos’); cp = zeros(3,1); disp(’After CP create’) disp(’ FunctionWS’); whos disp(’ CallerWS’); evalin(’caller’,’whos’); cp(1) = v(2)*w(3)-w(2)*v(3); cp(2) = v(3)*w(1)-w(3)*v(1); cp(3) = v(1)*w(2)-w(1)*v(2);

Built-in Functions MATLAB has many functions called built-in functions. –sin, cos, tan, exp –reshape, size, plot If we could see the programs for these, we would see that they are written in C or assembly language (specific to each processor that MATLAB is available for). These low-level, important functions give MATLAB its basic functionality. Individuals (like yourself) expand the functionality of MATLAB by writing their own m-file functions, which use the built-in functions, as well as already written m-file functions. We will write m-file functions, using MATLAB expressions and assignments.

TYPE shows m-files The command >> type filename will either –inform you that the function is a built-in, or –print out (to the command window) all of the lines in the m- file with that name. Try >> type sin >> type strvcat >> type vecop3