PDF download Download Article PDF download Download Article

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.


  1. This button will be on the upper left side of your screen.
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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

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

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      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
      • In other programming language the new line character is "/n"; in MATLAB it is "\n".
      • If you do not save your program it will not work, or when you execute or call your function nothing will happen.
      • For every Matlab function you create you must add a END to it this is crucial and your program will not work otherwise.
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 88,860 times.

      Is this article up to date?

      Advertisement