How to Call a Function in Matlab Again So We Can Plug in a Number

Download Article

Download Commodity

Functions are the footing of all scripting and programming languages. With functions, yous tin can make your applications exercise anything you desire. Functions are very useful and necessary in all applications that are design in MATLAB. Nosotros will be designing the math function y = mx+ b which is know 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 reply. This instruction set assumes you take basic noesis of MATLAB, such equally how to open up a script file and how to perform simple information operations.

Things You Should Know

  • Commencement your script with function, followed by the name yous 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 Download Article

  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 proper name. The name of your function should be the proper name of your file, so when you save this new script file it will be the name of your file. In this case, for case, you can name our part slope-equation.

    Advertisement

  3. 3

    Blazon the inputs of your function in between the parenthesis. An input is something you need the user to give to yous. For example, if you want to determine the slope equation y = mx+b, y'all demand the user to tell usa what is the slope value(m), the ten coordinate and the y-intercept(b).

  4. 4

    Comment on what each input is. Skip to line two in your plan and type for case, "%m is the value of the slope of the line". Echo this for each iii inputs. Commenting is useful in programming for yous and others who change your programme to understand all the variables and things you have done and how they are defined.

  5. 5

    Type in the operation you desire your program to do using your inputs. What this ways, in this example, is you desire your equation to define a variable y every bit the production of our input k and x and and so add the y intercept value (b) to information technology. In line 5, y'all ascertain your equation. Practice not forget the semicolon this semicolon suppresses the output! What does that hateful? That ways the software Matlab automatically assigns the y variable the value of mx+ b and information technology 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. Yous will first define the fprintf statement and so go into more than details. Blazon in at line vi fprintf('blank message');

  7. seven

    Decide on what you want your message to display. Supercede the words blank message with your ain 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 afterward your sentence but however in between the unmarried quotation marks. This means since yous are dealing with integers y'all should use "%i" this will call an integer value from our fprintf statement. What are the dissimilar data types? Well the most common one is integer which in an fprintf argument is defined as %i just there is also a whole list of numeric information types at this website http://world wide web.mathworks.com/help/matlab/numeric-types.html where you tin can look and decide at which data type you lot would like your answer to be formatted in!

  9. 9

    Blazon the output of your part after the single quotation marker. In your case the output is the value y so after the single quotation you blazon ", y". The fprintf statement automatically recognizes this variable and puts it in the start %(datatype) it sees in betwixt 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 await neater. It makes your program enter afterward your initial fprintf argument. This is merely the line "fprintf('\northward');'. The new line graphic symbol in other programming languages is "/northward"; in MATLAB information technology volition simply work with the backwards slash.

  11. 11

    Add an end to the concluding line of your programme and save your plan as your part name. This stop volition close our function and is necessary in every office you create in MATLAB. If you practise not salve your program you will get the wrong output or no values when you execute.

  12. 12

    Examination your function out on the command prompt! This office is considered calling your role; you go to the command prompt and type "yourfunction(inputvalue1, inputvalue2, inputvalueN)". This means you type your role'due south name and the values yous want to assign to the inputs. Test your role with the input value of 4, five and half dozen. That means on the command prompt you would write slope Equation(four,5,6). As you can come across from the picture mistakes are bound to happen in your lawmaking just refer back to these steps and the warnings and see what you missed or messed up on!

    Advertisement

Add New Question

  • Question

    How do I integrate a fourier series Q=10(t)*sin(westward*t) from the interval [0, two*pi/w]?

    Community Answer

    To integrate over fourth dimension, you will demand to know what the function x(t) is in terms of t.

Ask a Question

200 characters left

Include your email address to get a bulletin when this question is answered.

Submit

Advertisement

Video

Thanks for submitting a tip for review!

  • Do not forget the semicolon when defining your equation this volition suppress the output which ways information technology volition not output the number earlier you desire it to be outputted to the user

  • In other programming language the new line character is "/due north"; in MATLAB it is "\n".

  • If you lot practice not save your program information technology will non work, or when y'all execute or call your office naught will happen.

  • For every Matlab function you create you must add a Finish to it this is crucial and your program will non work otherwise.

Advertisement

About This Article

Thanks to all authors for creating a page that has been read 47,493 times.

Is this article up to engagement?

felkerwanding.blogspot.com

Source: https://www.wikihow.com/Write-a-Function-and-Call-It-in-MATLAB

0 Response to "How to Call a Function in Matlab Again So We Can Plug in a Number"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel