%[text] %[text:anchor:T_33DE9782] # 454MI: Introduction to MATLAB - Part 4 - Functions %[text] %[text:anchor:T_362EE8B4] # %[text] This is the fourth MATLAB live script of the collection ***Using MATLAB in the 454MI "******Data Driven Digital Systems***" course, devoted to introduce the MATLAB/Simulink environment and tools for solving practical problems related to the topics of the 454MI course, i.e. performance analysis of dynamic systems, parametric estimation, identification of models from data, and prediction of the evolution of dynamic systems. %[text] Use [this link](file:./usingMATLAB_454MI_Data_Driven_Digital_Systems.m) to go back to the main live script of the collection. %[text] %[text:tableOfContents]{"heading":"Table of Contents"} %[text] %[text:anchor:H_905E1908] ## Objectives %[text] The aim of this live script is to illustrate how to use functions and to define custom function and anonymous functions in MATLAB. %[text] What you should know by the end of this module: %[text] - how to call functions provided as part of MATLAB; %[text] - use of input and output arguments; %[text] - how to create a custom MATLAB function. \ %[text] %[text:anchor:H_D80C2E5E] ## An Introduction %[text] %[text] Functions are an essential mechanism for grouping a sequence of frequently used MATLAB commands to perform a specific task. Examples include all the standard mathematical functions such as `sin`, `cos`, `tan`, `exp`, `log`, `sqrt`, `abs` and many others. MATLAB also offers the possibility of **defining custom functions**. In this live script, we will only look at the simplest way of doing this, using anonymous functions. This is essential for problems such as solving non-linear equations, integrating a function and minimising or maximising a function. One of the great powers of MATLAB is the ability to apply functions to vectors and matrices with any number of elements in a single call. %[text] %[text] %[text:anchor:H_78FBEEDB] ## The General Syntax %[text] MATLAB provides the ability to define your own functions: %[text] - An M-file is a **MATLAB function** if and only if the first line of the file contains the following text \ %[text] ```matlabCodeExample %[text] function [output variable list] = function_name (input variable list) %[text] ``` %[text] - Terminating the file with a keyword is unnecessary: usually, a function ends when the last instruction line of the M-file describing it (which may be an "`end`" MATLAB keyword) is reached. %[text] - You can force termination at any point using the '`return`' instruction (see `help return` for details). \ %[text] %[text:anchor:H_B463FF77] ### An Example %[text] Calculating the area of any triangle according to the formula %[text]{"align":"center"} $\\mathcal{A} = \\displaystyle \\sqrt{s\\cdot \\left(s-a\\right)\\cdot \\left(s-b\\right)\\cdot \\left(s-c\\right)}\\;,\\quad s= \\frac{a+b+c}{2}$ %[text] where $a\\,,\\;b\\,,\\;c$ are the lengths of the triangle sides. %[text] %[text] The function then (in its simplest version) has 3 input parameters and only 1 output parameter: %edit evalTriangleArea help evalTriangleArea %[output:8276a060] %% %[text] Please note the initial comment lines in the M-file text. They are usefull to implement the help contents for the custom MATLAB function. %[text] To see how the help command uses such helping lines, just run the following help evalTriangleArea %% %[text] Using the custom function %[text] - assigning an output variable \ format short Area = evalTriangleArea(10,15,20) %[text] - without assigning the output variable \ evalTriangleArea(10,15,20) %% %[text] %[text:anchor:H_A0F8ED3E] ## Some Built-in Functions %[text] %[text] You may find the list of built-in elementary mathematical function by typing help elfun %[text] or using the MATLAB help browser (search for **Elementary Math**) doc Elementary Math %[text] %% %[text] %[text:anchor:H_4644F8A6] ## Functions Applied to a Vector %[text] %[text] Most MATLAB functions have been modified (overloaded) so they work with **inputs** which are **vectors** as well as scalars. When an argument is a vector the function is applied to each element of the vector, producing a vector of the same size as the input vector. %[text] %[text:anchor:H_C1482D7A] ### Examples sqrt(1:10) xv = -1:1/2:1 % a row vector with 5 elements sin( (pi/2)*xv ) %[text] Sometimes the result of the function has fewer elements than the input: sum(xv) max(xv) %% %[text] %[text:anchor:H_BE2690D8] ## Summary %[text] %[text] Using this live script you have: %[text] - learned about MATLAB wide range of elementary mathematical functions; %[text] - learned how to write an own custom MATLAB function; %[text] - learned that many of MATLAB functions accept a vector as input, creating a vector as output, where the function has been evaluated at each element of the input vector. \ %[text] %[text] %[text:anchor:H_B713125A] #### Back to the Index %[text] Use [this link](file:./usingMATLAB_454MI_Data_Driven_Digital_Systems.m) to go back to the main live script of the collection. %[text] %[text] %[text:anchor:H_C8A86778] #### Back to the Previous Part: Vectors & Matrices %[text] Use [this link](file:./Intro_MATLAB4DDDS_p3.m) to go back to the previous live script of this collection. %[text] %[text] %[text:anchor:H_A2D5477B] #### Go to the Next Part: LTI Systems %[text] Use [this link](file:./Intro_MATLAB4DDDS_p5.m) to go to the next live script of the collection. %[text] %[text] %[text] %[text] %[appendix]{"version":"1.0"} %--- %[metadata:view] % data: {"layout":"inline","rightPanelPercent":40} %--- %[output:8276a060] % data: {"dataType":"text","outputData":{"text":" The function evalTriangleArea<\/strong>(a, b, c) \n computes the area of a triangle whose\n sides have length a, b and c.\n Inputs:\n a,b,c: Lengths of sides\n Output:\n A: area of triangle\n Usage:\n Area = evalArea(2,3,4);\n Written by XXX, MM\/DD\/YY.\n\n","truncated":false}} %---