What Is Siemens NX And Cost Of It? Which Companies Are Using Siemens NX?
If you are working for a very specific task, for examle in a very special branch of mechanical engineering, creating functions for your profession can be very useful, when you are making calculations or specific programs by using Matlab. Functions are codes that you create only for once, you can use them everytime when you call it from function library in Matlab. You can create your own code libraries in Matlab to create special environment for your profession. In here we explain how to create funtions in Matlab and how to use them effectively in calculations and codings.
Actually, all the codes in Matlab are functions that are created to calculate or do the speficic tasks, when you call them from library. For example, reshape() command in Matlab does a specific task when you use it, and reshape() command is a specific function that is created automatically, when you install Matlab into your computer.
Creating functions in Matlab is very easy task and its logic is very straightforward.
Above, you are seeing an example of function that is created in editor. Whn you are creating a function, you need start with the string that ‘function’ as shown by red arrow above. Inside the pharantheses, type the variables that the function will calculate. Use square brackets here.
At above function, ‘r1’ and ‘r2’ values are calculated as you see in green box above. These values are the values that you are expecting from your function to calculate.
For example, it is a function that calculates the roots of second degree polynomial. A secomd degree polynomial has three of coefficients which are the coefficients of X1a^2+X2a+X3. a is the variable of polynomial, and X1, X2 and X3 are coefficients. So you need to take the information about these coefficients from user of function.
What you need to do is, type the name of function as shown by red arrow, after equalize it with ‘function(r1, r2)’. Writhe the value that needed to be taken from user inside the name of function. In here, use normal brackets.
As we said, in square brackets at next to ‘function’ are the values that you want to find. In normal brackets, these are the values that you will take from users. So you need to define, r1 and r2 with x1, x2 and x3.
As you see above we defined the r1 and r2, with x1, x2 and x3. r1 and r2 values will be shown when the user will call this findroots() function at further codings in Matlab.
After creating a function, you need to save these functions into a file, which is defined in Matlab to call functions from it. If you save this function randomly a file, the program will not call that function probably, because this file directory is not set for MatLab.
If you want to create your own file path to save and call your functions that you created, create your file, then define it in Matlab by using ‘Set Path’ just like below.
In ‘Home’ tab, click on ‘Set Path’ as shown in blue box above to set your file as functions can be called.
Click on ‘Add Folder’ as shown by blue arrow, select the directory of your file that functions will be saved. Then click ‘Select’ as shown by red arrow above. Then click on ‘Save’ on ‘Set Path’ window.
After that, you can save and call your function from this file in Matlab.
After creating your function, click on ‘Editor’ tab as shown in red box anb click on ‘Save As’ to save your function as .m file inside your folder that set as path in Matlab.
When your function is saved inside tha path folder that is defined above, you can use it as an ordinary Matlab command in Command Window. For example we want to find the roots of 3x^2+5x+7 polynomial, just type the names of roots as shown by greena arrow above inside square brackets. And type the coefficients inside the findroots() function as shown in green box above.
YOU CAN LEARN MatLab IN MECHANICAL BASE; Click And Start To Learn MatLab!
When you press enter, you can see the roots of your polynomial as x1 and x2 as shown by red arrows above.
Creating functions, saving them into files that set for Matlab as .m files and using them as ordinary command in Matlab Command Window is very easy like above.
Do not forget to leave your comments and questions about creating functions in Matlab below. Your precious feedbacks are very important for us.