CMPS 1371 Introduction to Computing for Engineers FUNCTIONS.

Slides:



Advertisements
Similar presentations
Lecture 5.
Advertisements

Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Introduction to Matlab. I use Matlab for: Data analysis Data plotting Image Analysis Also – Simulations (solving odes/pdes/finite element methods) – Minimisations,
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Introduction to MATLAB
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
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.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Builtin and user defined functions
4. Week 04.March Use of M-File Editor/Debugger: text editor, debugger; editor works with file types in addition to.m (MATLAB “m-files”)
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Predefined MATLAB Functions ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
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.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Matlab for Engineers Built-in Matlab Functions Chapter 3.
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
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.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Recap Script M-file Editor/Debugger Window Cell Mode Chapter 3 “Built in MATLAB Function” Using Built-in Functions Using the HELP Feature Window HELP.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Scientific Computing Introduction to Matlab Programming.
Introduction to Matlab
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Lec-10 Manipulations of Matlab Matrices. Built-in functions The chapter will be covered quickly There are quite a few built-in functions in MATLAB – If.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
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.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
Lecture 24: Rough and Ready Analysis. MATLAB uses function names consistent with most major programming languages For example sqrt sin cos log.
MATLAB Lecture 1 염익준. Introduction MATLAB (MATrix LABoratory) a special purpose computer program optimized to perform engineering and scientific calculations.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Fall 2006AE6382 Design Computing1 Scripts and Functions Matlab’s Command prompt is where you can enter commands to be executed immediately… –Just like.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Some “What’s the output” questions to get the day started… >>A = [1 2 3; 3 5 6] This statement stores the matrix: 1. A= 2. A= 3. A= 4. A= Ask Garvin’s.
Built-in MATLAB Functions Chapter 3
Computer Simulation Lab
User Defined Functions
Introduction To Matlab Class 1
Fourth Year – Software Engineering
Introduction to Matlab
Matlab Basics Tutorial
Presentation transcript:

CMPS 1371 Introduction to Computing for Engineers FUNCTIONS

Functions MATLAB uses function names consistent with most major programming languages For example sqrt sin cos log

Function Input can be either scalars or matrices

Using Predefined Functions Functions consist of Name Input argument(s) Output sqrt (x) = result In MATLAB sqrt(4) ans = 2

Some functions require multiple inputs Remainder function returns the remainder in a division problem For example the remainder of 10/3, is 1

Some functions return multiple results size function determines the number of rows and columns

You can assign names to the output

Nesting Functions

Functions There are functions for almost anything you want to do Use the help feature to find out what they are and how to use them From the command window From the help selection on the menu bar

Elementary Math Functions abs(x)absolute value sign(x)plus or minus exp(x)e x log(x) natural log log10(x)log base 10

Rounding Functions round(x) fix(x) floor(x) ceil(x)

Discrete Mathematics factor(x) gcd(x,y) greatest common denominator lcm(x) lowest common multiple rats(x) represent x as a fraction factorial(x) primes(x) isprime(x)

Trigonometric Functions sin(x)sine cos(x)cosine tan(x)tangent asin(x)inverse sine sinh(x)hyperbolic sine asinh(x)inverse hyperbolic sine sind(x)sine with degree input asind(x)inverse sin with degree output

Data Analysis max(x) min(x) mean(x) median(x) sum(x) prod(x) sort(x)

Variance and Standard Deviation std(x) var(x)

Random Numbers rand(x) Returns an x by x matrix of random numbers between 0 and 1 rand(n,m) Returns an n by m matrix of random numbers

Computational Limits MATLAB’s computational range on most computers is: When you divide by 0, the computer returns Inf

Function specification function = ( ) % The names of variables in functions are only visible in the function. Scripts are used for the top level call to the functions. Functions must be saved in an M-file of the same name as the function. (For now in the working directory.) (Actually filename is the name of the function!)

A simple example Suppose I have a cylinder, and I want the volume: Going to write a function, called cyl, which will input the radius and the height of the cylinder and produce the desired volume h r rh vol cyl

Function cylinder Write function function volume = cyl(radius, height) % CYLINDER computes volume of circular cylinder % given radius and height % Use: % vol=cylinder(radius, height) % volume=pi.*radius^2.*height; % why the dots? Save as m-file with function name (cyl.m) Type help cyl

Results of Cylinder

M-function Workspace Each time an m-function is executed, a new workspace is created just for that instance All variables except arguments and returned variable are defined only in the function workspace Function variables are not defined in Base workspace, nor are Base variables defined in function workspace Function workspace is destroyed when function completes Function variables are “hidden” from world (and from accidental misuse or alteration)

Restrictions, style Functions do not use INPUT or DISP or any real input output unless specified in the assignment or for debugging Semi colons – use semi colons unless you need to see step by step results when debugging Can’t save and run a function. Why not? Because doesn’t know the calling environment or context.

Test Cylinder Function Create a script: test_cyl Rad = Input(‘Radius:’) Disp(rad) % note different names Ht = Input(‘Height:’) Disp(ht) Cyl(rad,ht) Try with vectors.

How are M-Functions Located? Sometimes it is important to know how Matlab will look for an m- function, especially if two have the same name… 1. First checks current workspace 2. is it a built-in Matlab function? 3. is it a sub-function in the current function? 4. is it a private function? 5. is it in the current directory? 6. is it in the Matlab path, searching from the current directory down? The first instance is used and any others are ignored This can cause problems if you don’t understand… It can also let you replace functions with different versions

Multiple Values Functions M-functions Can Return Multiple Values The returned variable from an M-function can be a scalar or an array (even a cell array) M-functions can use the [ ] constructor to return an array formed from multiple scalar values

Multiple Value Return Lets update our cylinder function to return the area and the volume: function [area volume] = cylAV(radius, height) % CYLINDER computes volume of circular cylinder % given radius and height % Use: % [ar, vol] =cylinder(radius, height) % volume=pi.*radius^2.*height; area = 2.* pi.* height + 2.* pi.*r.*r;