More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ES 240: Scientific and Engineering Computation. Chapter 17: Numerical IntegrationIntegration  Definition –Total area within a region –In mathematical.
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
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.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Functional analysis y = f(x) things to do 1. sketch graph 2. find roots (zeros) 3. find.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
MATLAB Functions – Part II Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September 2013.
Lecture 26: Numerical Integration Trapezoid rule Simpson's rule Simpson's 3/8 rule Boole’s rule Newton-Cotes Formulas.
Advanced use of functions Anonymous functions function handles subfunctions and nested functions.
MATLAB FUNDAMENTALS: USER DEFINED FUNCTIONS THE SYMBOLIC TOOLBOX HP 100 – MATLAB Wednesday, 10/29/2014
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Programming with MATLAB
Al-Amer An Introduction to MATLAB Lesson 2: M-files Dr. Samir Al-Amer Term 061.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Functions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Al-Amer An Introduction to MATLAB Dr. Samir Al-Amer Term 062.
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.
MATLAB Functions – Part I Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September 2013.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
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.
CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
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.
Function M-File Numerical Computing with. MATLAB for Scientists and Engineers.
Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
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)
Scientific Computing Introduction to Matlab Programming.
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
Analyzing Functions (4.16) y=f(x) MATLAB. Functional Analysis includes: Plotting and evaluating a function Finding extreme points Finding the roots (zeros.
Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.
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.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
More Functions in MATLAB. Functions that operate on other functions A function F() can take another function G() as an argument by using a notation:
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
Basis of Mathematical Modeling LECTURE 3 Numerical Analysis with MATLAB Dr. N.K. Sakhnenko, PhD, Professor Associate.
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 7 : M-file Programming I Edward Cheung Room W311g 2008.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
전자장 1 실험 - Matlab 사용법 - Photonic Systems Laboratory School of EE, Seoul National University Photonic Systems Lab School of EECS, S.N.U.
Loops and Conditional Statements Dr.Abbas Lab 4 1ENG. Ahmad Alaql.
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 7
Introduction to Programming for Mechanical Engineers
EEE 161 Applied Electromagnetics
Introduction to Programming for Mechanical Engineers
Matlab review Matlab is a numerical analysis system
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Programming The ideal style of programming is Structured or
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Presentation transcript:

More on Functions… Lecture 8

Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information within a function between calls to afunction persistent var1,var2… Example: Running averages function [ave,std]=runstats(x) %runstats generate running ave /std deviation persistent n Persistent sum_x Persistent sum_x2 if x == ‘reset’ n=0 Sum_x=0 Sum_x2=0 else n=n+1 Sum_x = Sum_x Sum_x2=Sum_x2+x^2 end

Preserving Data between Calls to A function if n == 0 ave=0; std=0; elseif n == 1 ave=sum_x; std=0; else ave=sum_x/n; std=sqrt((n*sum_x2-sum_x^2)/(n*(n-1))); end script file [ave,std] = runstats(‘reset’) nvals=input(‘enter number of values’); for ii=1:nvals x=input(‘enter a value’, ‘s’); [ave,std]=runstats(x) end

Function functions A function includes the name of other functions in the input argument list. Function NameDescription fminbnd Minimize a function of one variable fzero find a zero of a function of one variable quad Numerically integrate a function ezplot easy to use function plotter fplot plot a function by name Common MATLAB Function functions

Function functions Common MATLAB Function functions >> fzero(‘cos’,[0 pi]) ans = >> fzero('exp(x)-2',[0 1]) ans = Locates a zero of the function cos between 0 and pi. Locates a zero of the function e x -2 between 0 and 1.

Function functions Common MATLAB Function functions >>fminbnd('x^3-2*x', 0,1) ans = >> ans = Locates a minimum between 0 and 1. integrate function in ‘fun’ using simpson’s rule function y=fun(x) y=x.^2-x; ‘fun’ includes any one variable function

Function functions Common MATLAB Function functions >>ezplot('x^3-2*x') quick plot of function in the string

Function functions Common MATLAB Function functions >> fplot('x^3-x',[-5 5]) quick plot of function in the string >> 2*pi])

Function functions eval and feval functions eval evaluates a character string as though it had been typed in the Command Window. feval evaluates a named function at a specific input value. >>x=eval('sin(pi/4)') x = eval (string) >>y=eval('x^2-2*x') ??? Error using ==> eval Undefined function or variable 'x'.

Function functions eval and feval functions eval evaluates a character string as though it had been typed in the Command Window. feval evaluates a named function at a specific input value. >>x=feval('sin’,pi/4) x = feval (fun,value) >> y = 2 function y=fun(x) y=x.^2-x;

Example: ascending sort function out = ssort(a) nvals=length(a); for i=1:nvals-1 iptr=i; for j=i+1:nvals if a(j)<a(iptr); temp = a(j); a(j) = a(iptr); a(iptr)= temp; end out=a; nvals=input('enter number of values to sort'); array=zeros(1,nvals); for i=1:nvals string=['enter value' int2str(i) ': ']; array(i)= input(string); end sorted =ssort(array); disp(' Sorted data '); for i=1:nvals sorted(i) end

Function Handles You can create a function handle to any function by using before the function name. You can name the handle if you wish and use the handle to reference the function. For example, to create a handle to the sine function; >> sine_handle >> plot ([0:0.01:6], sine_handle, [0:0.01:6])

Methods for Calling Functions There are four ways to invoke, or call, a function into a function. 1.As a character string identifying the appropirate function M-file: 2.As a function handle, 3.As an inline function object 4.As a string 1. function y= fun1(x) y = x.^2-4; The function may be called as follows, to compute the zero over the range 0<x<3. [x,value]=fzero(‘fun1’,[0,3])

2. As a function handle: [x,value] = 3. As an inline function object: >>fun1=‘x.^2-4’; >>fun_inline =inline(fun1); >>[x,value] = 4. As a string expression >>fun1=‘x.^2-4’; >>[x,value] = Or >>[x,value] = fzero(=‘x.^2-4’,[0,3]);

Anonymous Functions

You can pass the handle of an anonymous function to another function Multiple input arguments

Calling one function within another

Nested functions