Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Writing MATLAB Functions: Anonymous Functions and Function Files, Slides of Matlab skills

Anonymous FunctionsMATLAB ProgrammingFunction FilesFunctions

Students to writing their own matlab functions through anonymous functions and function files. Anonymous functions are defined using the '@' symbol and are useful for simplifying calculations and reusing programs. Function files, on the other hand, are saved in .m files and are available the next time matlab is run or when the function requires more than a single statement. Students will learn how to define, save, and use functions to convert between miles per hour and meters per second, as well as how to handle multiple input arguments and outputs.

What you will learn

  • How can students create a function to convert between miles per hour and meters per second?
  • What is the process for defining and using a function file in MATLAB?
  • How do anonymous functions differ from function files in MATLAB?

Typology: Slides

2021/2022

Uploaded on 09/12/2022

danmarino
danmarino 🇺🇸

4.2

(11)

35 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Writing MATLAB Functions: Anonymous Functions and Function Files and more Slides Matlab skills in PDF only on Docsity! EGR111 - p. 1 of 4 - Functions_rev5.docx EGR 111 Functions This lab is an introduction to writing your own MATLAB functions. The lab also introduces relational operators and logical operators which allows MATLAB to compare values and count the number of values that satisfy a given condition. New MATLAB Commands: @, function 1. Anonymous Functions In addition to the built-in functions such as cos and sin, we can create our own functions in MATLAB to simplify common calculations and allow us to re-use programs. There are two ways to define functions. The first way is called anonymous functions. For example, to define an anonymous function that converts from kilometers to miles, we would type the following command into MATLAB’s command window (there are 0.62137119 miles per km): km2miles = @(x) x * 0.62137119; The command above defines a function named km2miles. The “@(x)” indicates that this is an anonymous function definition and that x represents the input value in the function definition. Once the function has been defined, you can use it like any built-in function. For example, to find out how many miles a 10 km run is, you could type the following: km2miles(10) ans = 6.2137 When we call the function using the command above, MATLAB replaces the input x in the function definition with the input value 10, and then computes the output. Exercise 1: Define an anonymous function called miles2km that converts miles to km, and test it using the input value of 6.2137 miles. Anonymous functions are a handy way to define a function, but this method of defining a function has some limitations. First, just like MATLAB variables, any anonymous functions that you define are lost if you shut MATLAB down. Second, anonymous functions are limited to a single MATLAB executable statement. Next we will see how we can define a function in a file that allows an unlimited number of statements. EGR111 - p. 2 of 4 - Functions_rev5.docx 2. Defining a Function in a .m File If we want a function definition to be available the next time we run MATLAB, or if the function requires more than a single MATLAB executable statement, then we need to define the function in a .m file. For example, let's write a function to convert a speed in miles per hours to meters per second. To open MATLAB’s text editor to define a new function, click on New and then click on Function. Then change the text to the following: function y = mph2mps(x) % Convert miles per hour to meters per second y = 0.44704*x; Save the file as mph2mps.m in your P:\MATLAB folder. Note that the filename “mph2mps.m” needs to be the same as the function name in the first line with an added “.m” extension. Because of the word “function” in the first line, this file will behave very differently than the script files from previous labs. The first line in the mph2mps.m file tells MATLAB that this file defines a new function (as opposed to a script file) called mph2mps that has one input argument, x, and one output argument y. In MATLAB the symbol “%” is used to indicate a comment. All of the text on the line after the “%” symbol is ignored by MATLAB, but is used to document the function for people who may need to use or modify the function. Type “help mph2mps” in the command window to see that MATLAB will print the first block of comments. Now we can use this function to convert a speed from miles per hour to meters per second instead of trying to remember how to do it (and possibly doing it incorrectly). For example, to convert 55 miles per hour to meters per second, we can simply type the following command in the MATLAB command window: mph2mps(55) If you get an error that says something like “??? Undefined function or method ‘mph2mps’ for input arguments of type 'double'”, check to make sure that you saved the file in your P:\MATLAB folder and that you have changed the Current Folder to point to that folder. When you type the command mph2mps(55), MATLAB places the value 55 into the input argument x in the function definition, then MATLAB executes the commands in the file (y = 0.44704*x;), and finally returns the value of the output argument y to the workspace. It is important to note that the variable names x and y in the function file are separate from the variable names in the MATLAB workspace. We say that x and y are “private”
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved