Functions are the basis of all scripting and programming languages. With functions, you can make your applications do anything you want. Functions are very useful and necessary in all applications that are design in MATLAB. We will be designing the math function y = mx+ b which is known as the slope equation this equation if programmatically defined is helpful since we can just plug in the known inputs and the program will output the answer. This instruction set assumes you have basic knowledge of MATLAB, such as how to open a script file and how to perform simple data operations.

Things You Should Know

  • Start your script with function, followed by the name you want to assign it.
  • After writing your function in the script editor, you can call it using the format yourfunction(inputvalue1, inputvalue2, inputvalueN).
  • Adding comments to your script makes it easy for anyone to understand the purpose of each input.


Steps

  1. 1
    Open up MATHWORKS MATLAB and press the New Script button. This button will be on the upper left side of your screen.
  2. 2
    Type your function name. The name of your function should be the name of your file, so when you save this new script file it will be the name of your file. In this case, for example, you can name our function slope-equation.
    Advertisement
  3. 3
    Type the inputs of your function in between the parenthesis. An input is something you need the user to give to you. For example, if you want to determine the slope equation y = mx+b, you need the user to tell us what is the slope value(m), the x coordinate and the y-intercept(b).
  4. 4
    Comment on what each input is. Skip to line 2 in your program and type for example, “%m is the value of the slope of the line”. Repeat this for each 3 inputs. Commenting is useful in programming for you and others who modify your program to understand all the variables and things you have done and how they are defined.
  5. 5
    Type in the operation you want your program to do using your inputs. What this means, in this case, is you want your equation to define a variable y as the product of our input m and x and then add the y intercept value (b) to it. In line 5, you define your equation. Do not forget the semicolon this semicolon suppresses the output! What does that mean? That means the software Matlab automatically assigns the y variable the value of mx+ b and it doesn't output the value to the screen.
  6. 6
    Use an fprintf statement to output the result of your equation. An fprintf statement is used to output information to the user of the program. You will first define the fprintf statement and then go into more details. Type in at line 6 fprintf(‘blank message’);
  7. 7
    Decide on what you want your message to display. Replace the words blank message with your own words your sentence should be descriptive of the output of your function. You can say, “The y coordinate of this line is:”
  8. 8
    Insert the data type of the output of your function after your sentence but still in between the single quotation marks. This means since you are dealing with integers you should use “%i” this will call an integer value from our fprintf statement. What are the different data types? Well the most common one is integer which in an fprintf statement is defined as %i but there is also a whole list of numeric data types at this website http://www.mathworks.com/help/matlab/numeric-types.html where you can look and decide at which data type you would like your answer to be formatted in!
  9. 9
    Type the output of your function after the single quotation mark. In your case, the output is the value y so after the single quotation you type “, y”. The fprintf statement automatically recognizes this variable and puts it in the first %(datatype) it sees in between the single quotation marks.
  10. 10
    Add an fprintf statement which contains the new line character. This line just simply is to make your program look neater. It makes your program enter after your initial fprintf statement. This is just the line “fprintf(‘\n’);’. The new line character in other programming languages is “/n”; in MATLAB it will only work with the backwards slash.
  11. 11
    Add an end to the last line of your program and save your program as your function name. This end will close our function and is necessary in every function you create in MATLAB. If you do not save your program you will get the wrong output or no values when you execute.
  12. 12
    Test your function out on the command prompt! This part is considered calling your function; you go to the command prompt and type “yourfunction(inputvalue1, inputvalue2, inputvalueN)”. This means you type your function's name and the values you want to assign to the inputs. Test your function with the input value of 4, 5 and 6. That means on the command prompt you would write slope Equation(4,5,6). As you can see from the picture mistakes are bound to happen in your code just refer back to these steps and the warnings and see what you missed or messed up on!
  13. Advertisement

Community Q&A

  • Question
    How do I integrate a fourier series Q=x(t)*sin(w*t) from the interval [0, 2*pi/w]?
    Community Answer
    Community Answer
    To integrate over time, you will need to know what the function x(t) is in terms of t.
Advertisement

Warnings

  • Do not forget the semicolon when defining your equation this will suppress the output which means it will not output the number before you want it to be outputted to the user
    ⧼thumbs_response⧽
  • In other programming language the new line character is "/n"; in MATLAB it is "\n".
    ⧼thumbs_response⧽
  • If you do not save your program it will not work, or when you execute or call your function nothing will happen.
    ⧼thumbs_response⧽
  • For every Matlab function you create you must add a END to it this is crucial and your program will not work otherwise.
    ⧼thumbs_response⧽
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time. This article has been viewed 65,276 times.
How helpful is this?
Co-authors: 8
Updated: January 24, 2023
Views: 65,276
Categories: MATLAB
Advertisement