How do matlab functions work




















If we want to return a value from the function, we must assign that value to a variable named ktemp —in the first line of our function, we promised that the output of our function would be named ktemp. This is one of the major differences between scripts and functions: a script can be thought of as automating the command line, with full access to all variables in the base workspace, whereas a function can only read and write variables from the calling workspace if they are passed as arguments — i.

What about converting Fahrenheit to Celsius? Instead, we can compose the two functions we have already created:. This is our first taste of how larger programs are built: we define basic operations, then combine them in ever-larger chunks to get the effect we want. Write a function called fence that has two parameters, original and wrapper and adds wrapper before and after original :. Write a function called outer that returns a string made up of just the first and last characters of its input:.

Once we start putting things in functions so that we can re-use them, we need to start testing that those functions are working correctly. It is possible to have multiple output parameters by including them within a vector.

In the function definition example above, the y variable is the output parameter. The ''f x '' portion of our mathematical function acts as its output parameter. Look at the full version of our example function and make sure you can recognize all of the parts.

Look at these example function definitions and see if you can figure out the number of input and output parameters they have:. Comments are extremely useful , especially when working with functions.

After each function definition, you should supply comments that describe the function: the inputs, outputs, and a brief description of what the function does. After your function has been defined, it is useless to you unless you use it to do something. This is accomplished by calling your function. Manipulating vectors is almost as easy as creating them. First, suppose you would like to add 2 to each of the elements in the vector a. The equation for that looks like:. Now suppose, you would like to add two vectors together.

If the two vectors are the same length, it is easy. Simply add the two as shown below:. Each function is a block of code that accomplishes a specific task. MATLAB contains all of the standard functions such as sin , cos , log , exp , sqrt , as well as many others. MATLAB even allows you to write your own functions with the function command; follow the link to learn how to write your own functions and see a listing of the functions we created for this tutorial.

Suppose you wanted to plot a sine wave as a function of time. First, make a time vector the semicolon after each statement tells MATLAB we don't want to see all the values and then compute the sin value at each time. The commands after the plot function title , xlabel , ylabel will add annotations to the plot. The plot contains approximately one period of a sine wave. You may visit the plotting page to learn more. To create a polynomial in MATLAB, simply enter each coefficient of the polynomial into the vector in descending order.

For instance, let's say you have the following polynomial:. Thus, if your polynomial is missing any coefficients, you must enter zeros in the appropriate place in the vector.

Download Code and Files Download the code used in this video. Related Information. Up Next:. Select a Web Site Choose a web site to get translated content where available and see local events and offers. Select web site.



0コメント

  • 1000 / 1000