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:

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane.
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Introduction to Matlab
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.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Introduction to Engineering MATLAB - 12 Agenda Function files.
Numerical Operations S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Numerical Integration.
 In this scene, the bear throws the deer into the air.  The deer is airborne for about 18 seconds  We will prove that the deer went too high and.
Functions.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
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.
MATLAB Functions – Part I Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September 2013.
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”)
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 tutorial course Exercises 2:. Exercises Copy the script ‘face_points.m’ from my webpage into your ‘scripts’ folder Create a new folder in your.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Functions 1 ENGR 1181 MATLAB 14.
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
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.
MATLAB FUNDAMENTALS: MATRIX/ARRAY FUNCTIONS THE COLON MATRIX/ARRAY MANIPULATION INPUT/OUTPUT HP 100 – MATLAB Wednesday, 9/3/2014
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.
EGR 115 Introduction to Computing for Engineers Introduction to MATLAB Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers.
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.
Array Operations ENGR 1181 MATLAB 4.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
The Hong Kong Polytechnic University Industrial Centre MatLAB Lesson 1 : Overview & Environment Edward Cheung Room W311g.
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.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
PHYSIC S. Think back to GCSE 1.Write down the definition of velocity Speed in a given direction 2.Write down the calculation for velocity But that is.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
MATLAB – More Script Files
Introduction to Matlab
Lecture: MATLAB Chapter 1 Introduction
MATLAB Basics Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Introduction to Programming for Mechanical Engineers
Functions (subprograms)
Vectors and Two Dimensional motion
MATLAB DENC 2533 ECADD LAB 9.
User Defined Functions
Matlab review Matlab is a numerical analysis system
MATH 493 Introduction to MATLAB
Matlab.
Vectors.
Using Script Files and Managing Data
Matlab Basics Tutorial
Programming The ideal style of programming is Structured or
Presentation transcript:

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: 1.User-Defined Functions 2.Function files

Functions are special purpose programs MATLAB has many Built-in Functions... sin(x), log(x), length(v), sum(v)... You can also create your own functions These are called User-Defined Functions Functions in MatLab y = f(x)

Consider the function f 1 (x), where: y = f 1 (x) = 2 x x – 3 The name of the function is f 1 It takes the number x (input variable) And calculates a number y (output variable) Now, let's do the same thing in MatLab.... User-Defined Functions y = f(x)

Open a new M-File Type the first line: function [y] = f1(x) Type the second line: y = 2*x^2 + 5*x – 3; SAVE ! the M-File with the filename f1.m The FunctionName and the FileName MUST be the same ! ! ! y = f 1 (x) = 2 x x – 3 User-Defined Functions

function [y] = f1(x) y = 2*x^2 + 5*x – 3; Let's use the function to calculate f1(2) >> x = 2; >> y = f1(x) y = 15 y = f 1 (2) = 2 * * 2 – 3 = 15 M-File: f1.m User-Defined Functions

function [y] = f1(x) y = 2*x^2 + 5*x – 3; Try these commands (after clear, clc): >> f1(2) ans = 15 >> a = 3 >> b = f1(a) b = 30 M-File: f1.m >> c = f1(2)+f1(3) c = 45 >> d = f1(c) d = 4272 User-Defined Functions

function [y] = f1(x) y = 2*x^2 + 5*x – 3 Let's see if f1.m works if x is a vector! >> x = [1:10] >> y = f1(x) y = 2*x.^2 + 5*x – 3 >> y = f1(x) y = M-File: f1.m User-Defined Functions ??? Error !! Why? SAVE !!!

Open a new M-File Define the function function [Joules] = BTUs2Joules(BTUs) % This function converts BTUs to Joules Joules = BTUs * SAVE ! the M-File.... BTUs2Joules.m The FunctionName and the FileName MUST be the same ! ! ! Let's create a function to convert BTUs to Joules User-Defined Functions

function [Joules] = BTUs2Joules(BTUs) % This function converts BTUs to Joules Joules = BTUs * >> B1 = 1 >> J1 = BTUs2Joules(B1) J1 = >> J5 = BTUs2Joules(5) J5 = BTUs2Joules.m User-Defined Functions % Convert 1 BTU to Joules % Convert 5 BTUs

For a ballistic projectile, thrown with an initial velocity v 0, at an angle theta, create a function to calculate the Distance, Height, and Time in the air. User-Defined Functions Functions can have several inputs and several outputs

function [D,H,T] = ballistic(v0,theta) % For a ballistic object thrown with speed v0 at % an angle theta, this function calculates the % Distance (D), Height (H), and Time (T) of the % object's flight. All dimensions are in meters. g = 9.81; D = v0^2*sind(2*theta)/g; H = v0^2*(sind(theta))^2/(2*g); T = 2*v0*sind(theta)/g; ballistic.m User-Defined Functions Save the file as: ballistic.m !

function [D,H,T] = ballistic(v0,theta) g = 9.81; D = v0^2*sind(2*theta)/g; H = v0^2*(sind(theta))^2/(2*g); T = 2*v0*sind(theta)/g; >> v0 = 50; >> theta = 45; >> [d,h,t] = ballistic(v0,theta) d = h = t = User-Defined Functions

User-Defined Functions can be called from: the command window inside MatLab programs (script files) Every programming language has this capability MATLAB calls it a User-Defined Function In FORTRAN and BASIC it’s a Subroutine In PASCAL it’s a Procedure In C it’s a Function In programming literature it’s a Subroutine User-Defined Functions

1.Syntax function [output list] = Function_Name(input list) 2.Save the function m-file as Function_Name.m 3.The function definition is the first line in the m-file. 4.All functions use Local Variables. 5.Functions can have any combination of scalars, vectors and arrays (and string variables, too) as inputs and outputs. Rules for User-Defined Functions