Introduction to Engineering MATLAB - 12 Agenda Function files.

Slides:



Advertisements
Similar presentations
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Advertisements

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:
Flow Charts, Loop Structures
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
CSE123 Introduction to Computing Lecture 1 Introduction Engineering Problem Solving.
1 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Today's Agenda ● Introduce Matlab User Functions.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
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.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
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”)
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
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.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
Engineering Problem Solving Kuncicky – MatLab Programming G. Polya – How to Solve it.
Chapter 1 Computing Tools Analytic and Algorithmic Solutions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
An Introduction to Programming and Algorithms. Course Objectives A basic understanding of engineering problem solving process. A basic understanding of.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Plotting in MATLAB Rachel Davies Brittany Earnest Greg Fannon Nicholas Millan Khurram Najeeb.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
 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.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
A Brief introduction to MatLAB ($50 - $99 for students)
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.
Physics Lesson 6 Projectile Motion
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
CSE123 Introduction to Computing Lecture 1 – Introduction to Computers 1.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
User-defined Matlab functions. Creating function m-files with a plain text editor MATLAB m-files must be plain text files. Most word-processors provide.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Introduction to Engineering MATLAB – 7 Script Files - 2 Agenda Script files continued.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
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.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Matlab for Engineers Matlab Environment Chapter 2.
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.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
CS005 Introduction to Programming
Physics Lesson 6 Projectile Motion
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Introduction to MATLAB for Engineers, Third Edition
Basic operations in Matlab
PROJECTILE MOTION.
The height of the building
Kinematics Projectile Motion
Projectile Motion A projectile is an object moving in two or three dimensions only under the influence of gravity.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 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.
Using Script Files and Managing Data
Presentation transcript:

Introduction to Engineering MATLAB - 12 Agenda Function files

FUNCTION FILE A function file is a program that can be used in two ways:  Perform a task frequently. Examples: calculate the value of a math function for different values of the independent variable, performs a series of commands with different values of variables.  Be a subprogram in a large program. In this way a large program can be made of smaller “building blocks” that can be tested independently.

FUNCTION FILE  Function files are used in the same way as built in functions. i.e. once they are created, the functions can be used in the command window, in script files, or inside other function files.  Usually, data is transferred to a function file by input variables and the results are transferred back by output variables.  All the calculations and the variables that are used inside the function are local. i.e. they are not recognized, transferred, or available to other parts of MATLAB.  Function files are like subroutines in FORTRAN and BASIC, procedures in PASCAL, and functions in C.

CREATING A FUNCTION FILE Once M-file is selected, the M-file Editor/Debugger window opens. A function file is created in the M-file Editor/Debugger window (the same window that is used to create a script file). In the command window click on the File menu, select New, and then select M-file.

The M-file Editor/Debugger window The first line of the function file is typed here.

FUNCTION DEFINITION LINE The first line of a function file MUST be the function definition line (without this line it will be a script file). The function definition line: 1.Defines that the M-file is a function. 2.Defines the name of the function. 3.Has the list of input and output variables. When the file is saved, the file name MUST be identical to the function_name (with the.m extension). function [output variables] = function_name (input variables)

EXAMPLES OF FUNCTION DEFINITION LINES Function definition lineFile name function [A] = RectArea(a,b)RectArea.m function[V, S] = SphereVolArea(r)SphereVolArea.m function[d,h] = projectile(v,theta)projectile.m function = CirclePlot(r)CirclePlot.m

FORMAT OF A FUNCTION FILE function [xout,yout] = functioname(xin,yin) % description of the function % help comments % name of creator, date a = …. b = …... xout = …… yout = …… Function definition line Output variables Input variables Function name Optional comments. Displayed when help functioname is typed In the command window. Body of the function The output variables must be assigned values

COMMENTS ABOUT FUNCTION FILES The word function, which is the first word in the function definition line must be typed in lower case letters. Square brackets are not required in the function definition line If the function has only one output variable. function [A] = RectArea(a,b) function A = RectArea(a,b) If there are no output variables, the square brackets and the equal sign can be omitted. function = CirclePlot(r) function CirclePlot(r) Either form is ok.

COMMENTS ABOUT FUNCTION FILES The names of the input and output variables given in the function definition line and in the body of the function are local. This means that other variable names can be used in the function call. Specific numbers, or mathematical expressions can also be used as input variables. (Example on the next slide.) The variables are assigned according to their position in the output or input variables list in the function definition line.

Function definition lineExample of variables when the function is used function [A] = RectArea(a,b)S=RectArea(g,r) T=RectArea(8,25) In the first example g and r must have assigned values before they are used as input variables. When the function is executed, a will have the value of g, b will have the value of r, and the value of A will be assigned to S. In the second example a is assigned the value 8, and b is assigned the value 25.

COMMENTS ABOUT FUNCTION FILES As in the command window and a script file, a semicolon in a function file suppresses output of a command. If a semicolon is not typed, the output is displayed in the command window. This can be useful when debugging.

SAVING A FUNCTION FILE  Once the function file is completed, it must be saved. In our class use Save As and save in the floppy A drive.  Do not name a function file a name that is already used by MATLAB for a built-in function. To check if a function name is used by MATLAB type “help name” in the command window.

EXAMPLE OF A FUNCTION FILE function value = accountvalue(depo,t,rate) % The function calculates the the value of a saving % account in which the interest compounds annually. % The output of the function is the account value. % The input to the function is: % depo: the initial deposit. % t: number of years. % rate: the interest rate in percent. format bank value=depo*(1+rate/100)^t; Function definition line Output variable Function name Input variables Comments Value assigned to the output variable

>> x = accountvalue(20000,15,6.5) x = >> amount = 20000; >> years = 15; >> intrat = 6.5; >> money = accountvalue(amount,years,intrat) money = >> amount = 20000; >> accountvalue(amount,15,6.5) ans = EXECUTING THE accountvalue FUNCTION Three examples of executing the accountvalue function in the command window are shown below:

EXAMPLE OF A FUNCTION FILE function [dmax,hmax] = trajectory(v0,theta) % The function calculates the trajectory of a projectile. % The input to the function is: % v0: the initial velocity (units: m/s) of the projectile. % theta: the angle (units: deg.) at which the projectile is shot. % The output of the function are: % dmax: the distance (units: m) the projective travels. % hmax: the max height (units m) the projectile reaches. % in addition, the function creates a plot of the trajectory. v0x = v0*cos(theta*pi/180); % Horizontal component of initial velocity. v0y = v0*sin(theta*pi/180); % Vertical component of initial velocity. (Continues on the next slide) NOTE: Why use the double quotes in PROJECTILE’s?

hmax = v0y^2/(2*9.81); % The max height. t = v0*sin(theta*pi/180)/9.81; % Time to highest point. ttotal = 2*t; % Total flying time. dmax = v0x*ttotal; % Max distance traveled. tplot = linspace(0,ttotal,200); % Creating a vector of time. x = v0x*tplot; % x coordinate as a function of time. y = v0y*tplot+0.5*(-9.81)*tplot.^2; % y coordinate as a function of time. plot(x,y) xlabel('DISTANCE') ylabel('HEIGHT') title('PROJECTILE''S TRAJECTORY') EXAMPLE OF A FUNCTION FILE

EXECUTING THE trajectory FUNCTION Executing the trajectory function in the command window for: V0 = 250 m/s, and theta = 32 degrees. >> [dist,height]=trajectory(250,32) dist = e+003 height =

ASSIGNMENT 8: 1.Problem 16 page 162 in the textbook. 2.Problem 17 page 162 in the textbook. 3. Problem 20 page 163 in the textbook. In each problem submit a printout of the function file, and a printout of the command window showing how the function was used. The second line in the function file, and the first line in the command window should be a comment with your name.