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 User-Defined Functions in MATLAB: Syntax, Examples, and Local Variables, Study notes of Matlab skills

Applied MathematicsNumerical AnalysisComputer EngineeringMATLAB Programming

Learn how to write and use user-defined functions in MATLAB with this comprehensive guide. Discover the syntax, examples, and local variables. Create simple functions and more complex ones like the quadratic formula. Understand the concept of dummy variables and local variables.

What you will learn

  • What is the syntax for writing a user-defined function in MATLAB?
  • How do you create a simple user-defined function in MATLAB?
  • What is the difference between a local variable and a dummy variable in a MATLAB function?

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

raimond
raimond 🇬🇧

4.8

(11)

218 documents

1 / 6

Toggle sidebar

Related documents


Partial preview of the text

Download Writing User-Defined Functions in MATLAB: Syntax, Examples, and Local Variables and more Study notes Matlab skills in PDF only on Docsity! User Defined Functions Leigh J. Little SUNY Brockport October 20, 2021 Contents 1 Writing Your Own MATLAB Functions 1 2 Syntax for a Function 1 3 Example 1: A very simple function 2 4 Example 2: An Absolute Value Function 3 5 Example 3: Local Variables 3 6 The Quadratic Formula 4 7 A Second Solvequad Example 6 1 Writing Your Own MATLAB Functions The ability to write your own functions is a critical component to programming, not just in MATLAB, but in any programming language. It makes writing complex programs easier to manage and understand. 2 Syntax for a Function The following rules apply when you write your own function in MATLAB. 1) You must choose a name for your function. This cannot be the same as an existing MATLAB function, some other function that you have written yourself or a variable name you are likely to use in your programs. For example, choosing to name your function n would be a terrible idea because n is commonly used as a variable. For this discussion, we will assume the name of the function is fname 2) Your function must be saved in a file called fname.m. 3) Your function must be in your working directory (there are ways around this, but we will use this rule for now). A MATLAB function has the generic template function [list of output arguments] = fname(list of input arguments) % % Comments (not required, but recommended) % | | | MATLAB statements to be executed by the function | Some important items to note: a) The function starts with the function keyword. This line must be the first line in the file. b) The list of output arguments is enclosed in square brackets, not parentheses. c) You can include a comment section at the top (and anywhere else in your function). d) You can have any number of input arguments, of any type (scalar, vector, matrix, etc.). You can also have a function with no inputs. 1 e) Item d also applies to output arguments. f) The ordering of the arguments is important. We will see more on this shortly. g) This example function would need to be saved in a file called fname.m 3 Example 1: A very simple function Enter the following into the MATLAB editor and save it as fun1.m function [] = fun1(invalue) disp(’The value you sent in is ’) invalue Examine what this function does by executing it several times for different inputs: >> fun1(5) The value you sent in is invalue = 5 >> fun1(-1) The value you sent in is invalue = -1 >> fun1([1 2 3 4 5]) The value you sent in is invalue = 1 2 3 4 5 >> fun1(’Evil Steve’) The value you sent in is invalue = ’Evil Steve’ We can see that this function displays whatever you put between the ( ) along with a message. The values sent as inputs to a function do not have to be static values. They can also be variables: >> a = 3; >> fun1(a) The value you sent in is invalue = 3 >> x = [5 7 2 -1 4]; >> fun1(x) The value you sent in is invalue = 5 7 2 -1 4 This is the most critical aspect of functions. The variable invalue that appears in the function is called a dummy variable or dummy argument. Consider the following: f(x) = x2 sin (x) f(z) = z2 sin (z) f(t) = t2 sin (t) f(blob) = (blob)2 sin (blob) Each of these functions is stating the same relationship. All of them say to square the input then multiply that by the sine of the input. In this example, we say that x, z, t and blob are dummy arguments. The 2 >> soln = solvquad(1,1,-6) soln = 2 -3 Notice that the function correctly returns 2 and -3 as the roots. Similarly for the equation x2 + 9 = 0 we know the two roots are x = 3i and x = −3i and that the coefficients are a = 1, b = 0 and c = 9. We can compute the roots of this equation by doing >> soln = solvquad(1,0,9) soln = 0.0000 + 3.0000i 0.0000 - 3.0000i One critical item to note is that even though we named the output variable x in the function body, we can name the output anything we want when we call the solvquad function. In the main workspace, we named the output of the function soln. The is another example of what we mean when we say that the variable names in the function .m file are dummy variables. To further illustrate this, consider the following example: >> hat = 1 >> cat = 0 >> football = 9 >> ratsnest = solvquad(hat,cat,football) ratsnest = 0.0000 + 3.0000i 0.0000 - 3.0000i This example illustrates two important concepts: 1) Arguments to functions can be hard-coded numbers (like in the first two examples) or they can be the names of other variables. 2) We are using the variable names hat, cat and football in the workspace. These are different names than we use in the solvequad.m file. When the function is invoked, the function will use hat as the a variable, cat as the b variable and football as the c variable. This is what makes functions so useful. We can write a process for solving a quadratic equation regardless of the variable names we send in from the main workspace. For example, the following version of the solvequad.m function would perform exactly the same: function [cow] = solvquad(orange,apple,banana) % [cow] = solvquad(orange,apple,banana) % % This function solves the quadratic equation % % orange * x^2 + apple * x + banana = 0 % % for the two roots and returns the result in a % vector of length 2. cow(1) = (-apple + sqrt(apple^2-4*orange*banana)) / (2*orange); cow(2) = (-apple - sqrt(apple^2-4*orange*banana)) / (2*orange); Finally, if you ask for help on the solvquad function, MATLAB will echo the comment section at the top >> help solvquad [x] = solvquad(a,b,c) This function solves the quadratic equation 5 a * x^2 + b * x + c = 0 for the two roots and returns the result in a vector of length 2. 7 A Second Solvequad Example Enter the following into a file called solvequad2.m: function [x1,x2] = solvquad2(a,b,c) % [x1,x2] = solvquad2(a,b,c) % % This function solves the quadratic equation % % a * x^2 + b * x + c = 0 % % for the two roots and returns the result in % the variables x1 and x2 x1 = (-b + sqrt(b^2-4*a*c)) / (2*a); x2 = (-b - sqrt(b^2-4*a*c)) / (2*a); Notice that this function performs the same task as the first one. The difference is that instead of returning the two roots in a vector of length 2, the roots are returned individually as separate scalars. For example, >> [soln1,soln2] = solvquad2(1,1,-6) soln1 = 2 soln2 = -3 Both versions of the solvquad function are valid and can be used to solve quadratic equations. Generally, the first one would be preferred from a data organization point of view. This illustrates the flexibility you have when you write functions. As long as it produces the necessary output and works properly, you can set up the function calling sequence however you wish. 6
Docsity logo



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