ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
Chapter 3 Program Design And Branching Structures.
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:
 Algebra 1 Remediation September 3 Start on page 208 of practice packet.
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
1 G:\common\eng1102\1102_200508\02matlab\m10.functions.sxi Today's Agenda ● Introduce Matlab User Functions.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
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.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Functions 1 ENGR 1181 MATLAB 14.
Functions and subroutines – Computer and Programming.
INTEGRALS Areas and Distances INTEGRALS In this section, we will learn that: We get the same special type of limit in trying to find the area under.
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.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
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.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Scientific Computing Introduction to Matlab Programming.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
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.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
1 CS1371 Introduction to Computing for Engineers Introduction to Matlab.
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.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
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.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
“Moh’d Sami” AshhabSummer 2008University of Jordan MATLAB By (Mohammed Sami) Ashhab University of Jordan Summer 2008.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Structured Computer Programming EE 201 Introduction to MATLAB 7 for Engineers بسم الله الرحمن الرحيم King Abdulaziz University College of Engineering Dept.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
Surface area of a cylinder Imagine a cylinder opened out Rectangle Circle.
CS005 Introduction to Programming
Lecture: MATLAB Chapter 1 Introduction
MATLAB DENC 2533 ECADD LAB 9.
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript: Introduction to Scripting
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions and Function Files / Chapter 6

ENG  MATLAB has many useful built-in functions which we have been using like sum, sin, max, etc.  User-defined functions: MATLAB allows you to create your own functions. This is useful when a special function may be needed many times or used in many programs. Functions : Introduction

ENG Functions : Introduction  Like a built-in function, the user-defined function can be used within script files or in the command window.  To create your own user-defined function you start with a new file but the format is strictly specified.  Functions are designed to handle one or multiple inputs and outputs.

ENG Function files within script files  More complex programs are broken into logical pieces to be written by one or a team of programmers. Functions are used to perform these subtasks. (Next slide)  Every programming language has a way to do this. In MATLAB it’s called a user-defined function In FORTRAN and BASIC it’s called a subroutine In PASCAL it’s called a procedure In C it’s also called a function

ENG Function files in complex programs Statement 1 ……… m=fnc1(n,p) ………… Statement n ……….. [y z]=fnc2(x) …………. Statement m …………… Script file: function [c] = fnc1(a,b) ………. c= ….. fnc1.m fnc2.m function [b,c]=fnc2(a) ……….. b=…. c=…. n, p x c b, c User Input / Output

ENG  Open a new function file (not script file) Creating a function file

ENG Creating a function file  The first line in the function file should be the function definition line: function [output vars] = function_name(input vars)  If this is omitted, MATLAB interprets the file as a script file, not a function file.

ENG  After adding commands, save the function file using the name function_name.m  Thus, if the name of your function is my_calc, save the function file as my_calc.m Saving Function Files function [output vars] = function_name(input vars)

ENG Function file: Single Input – Single Output  Create a function area_sq where the input is the side of the square and the output is the area of the square. function x= area_sq(side) x=side*side; Now in the command window type: >> area_sq(6) ans = 36  Since no assignment of the result from the function was made, the result was put in ans. Function file : area_sq.m

ENG Suppose you want to find the area of a rectangle given its 2 sides using a function area_rect. function [area_calc] = area_rect(length, width) area_calc = length * width; Now in the command window type the following: >> AREA = area_rect(2, 4) AREA = 8  Note: the function’s output (area_calc) was assigned to variable AREA. More discussion later. Function file: Multiple Inputs – Single Output Function file : area_rect.m

ENG Using a function file in a script file You can use the area_rect function created earlier in a program. Create a script file called my_area.m as follows: clear clc len=input('Enter the length of the rectangle :'); wid=input('Enter the width of the rectangle :'); AREA = area_rect(len,wid) Enter the length of the rectangle :2 Enter the width of the rectangle :4 AREA = 8 Output: Note the assignment of the output to variable AREA

ENG FUNCTION OPERATION len=input(‘Length :’); wid=input(‘Width :’); AREA = area_rect(len,wid)... Script Variables len wid AREA 2 function [area_calc] = area_rect(length, width) area_calc = length * width; Function Variables length width area_calc Call a function to calculate the area of a rectangle with sides len and wid. Script file

ENG EXAMPLES OF FUNCTION DEFINITION LINES Three input variables, two output variables, save as loan.m Two input variables, one output variable, save as RectArea.m One input variable, two output variables, save as SphereVolArea.m Three input variables, no output variables, save as trajectory.m function [mpay, tpay] = loan(amount, rate, years) function trajectory(velocity, height, gravity) function [volume, surfArea] = SphereVolArea(radius) function [area] = RectArea(width, height)

ENG Function file: Multiple Inputs – Multiple Outputs Suppose you want to find the surface area (curved surface only) and volume of a cylinder given the radius of the base and the height, you can write a function cylinder by creating a function file cylinder.m as follows: function [area, volume] = cylinder(radius, height) area = 2 * pi * radius * height; volume = pi * (radius^2) * height; H = Height R= Radius Surface Area = 2 π RH Volume = π R 2 H

ENG Functions Example function v = freefall(t,m,cd) %freefall: bungee velocity with second-order drag %v=freefall(t,m,cd) computes the free-fall velocity of an object with %second-order drag %input: %t=time (s) %m=mass(kg) %cd = second-order drag coefficient(kg/m) %output: %v=downward velocity (m/s) g = 9.81; %acceleration due to gravity v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t); Command Window Output: >>freefall(12,68.1,.25) ans = Now try changing the argument values