![]() This means that the naming conventions of a script do not need to be changed to match a function’s naming.The factorial of 3 = 3! = n* ( n – 1) * (n – 2)= 3* ( 3 – 1)* (3 – 2) = 3 * 2 * 1 = 6. For example “ x = quadraticsolver(i,j,k)” will work the same as the code cares only about the order the function is fed information, not the actual variable names. When using the function, different variables could have been used. Notice how in this example the variables “a”, “b”, “c”, and “x” are used in both the function and main script. This completes the arithmetic included in the “quadraticsolver” function file and outputs the results to x. These variables are plugged into the function “quadraticsolver” in line 9. Within the main script “QuadraticFunction”, a user inputs values for variables “a”, “b”, and “c”. The function, entitled “quadraticsolver” begins with a line defining that the script is a function that outputs a variable “x” when given inputs for variables “a”, “b”, and “c”. Instead of utilizing equations within the base script, a separate quadratic equation function is created and used for this application. This example contains a script that would like to use the quadratic equation. This enables a function to be used several times for several different values throughout a script, demonstrating the simplifying benefits of functions.Īn example of an application for creating a function in a script is included below. This demonstrates that variable names do not need to be consistent between the function and the script, as the function will accept any variable entered in the correct location. Notice this operation takes the value assigned to “x” as input and enters that into the function for the variable “ n“. ![]() Then, MATLAB will assign the variable “ y” a value after completing the operation within the “fact” function. Within this script, a variable “x” is assigned a value. Another word of warning, remember to rename the function file if you rename the function while editing the code during revisions! Calling the function in a script is shown in the following example. This is a very easy mistake to make that will prevent MATLAB from finding the correct function. m file should appear as “fact.m” in this example. Remember to save the function file with the same name as the function, so the. m file, the user can call the function like any other MATLAB command in a script that is being written. ![]() When saving the newly created file, it must be called “ fact” in order for the function to work properly. In this case, the function is called “ fact”. By having “function” being the first thing in the script, MATLAB can automatically determine that this is not a full script, but instead just a function that may be called to a different script. The following shows a very simple function that can compute the factorial of some number n by summing every integer between one and n. ![]() By having the first line of a function contain the function command, the script will be saved as a function file and not as a. How to Create a Function in MATLABĪ function can be created in a similar method to normal scripts. This is handy when using the same equations several times in different codes and would like to save time and not repeat the same code over and over. MATLAB has a file type called functions which enables the user to create their own functions in separate script files and refer to them in another script they are writing. As codes become longer and more complicated it is necessary to find ways to reuse common sections which are used in multiple scripts efficiently. Chapter 8: Functions and Function Handles What is a Function?Īt this point, students have learned many commands within ME 160 and have the tools to complete operations through more and more complex codes.
0 Comments
Leave a Reply. |