You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 49 Next »

Step 6: Plot σx vs. ri , Take 4: Functions

Let's create a function to calculate the bending stress that outputs σx given (M, ri, ro ). Functions are really useful to break down your code into modules and also reuse parts of your code.

Let's first pick MATLAB's brain on how to create functions in MATLAB. Bring up the following page from Documentation. 

Home (tab) > Documentation (icon) > MATLAB > Programming Scripts and Functions > Functions > Function Basics > function

Take a couple of minutes to peruse the examples for functions with one output and functions with multiple outputs. MATLAB has extensive documentation on the use of functions; however, one has to poke around a bit before finding the most useful information. I personally go for the examples first.

The correct syntax for creating a function is:

function return_value = function_name(parameter_1, parameter_2,...)

%function description - MUST  be in a comment

code...

return_value = value

A few noteworthy points to ponder:

  1. return_value is the only data that gets passed back to the main code.
  2. You do not need to have a function description but it is good programming practice to add a comment on each function that describes what the function does. Also, if you add a description of the function, MATLAB will be able to index it and return a description of your function if you type help function_name in the Command Window.
  3. Once you have created a function you MUST name the .m file with the same name the function has. Otherwise MATLAB will not be able to access your function when you call it.
  4. You need brackets around your outputs if you have multiple output arguments. For only one output, the convention is to not include brackets. 

We will start by creating the bending stress function that outputs σx given (M, ri, ro ). 

Editor (tab) > New > Function

Note that the main code for creating a function is already built-in. We could have also selected New > Script and get a blank page, but it's nice to have the function template right away.  What's important to understand here is that both the script files and function files have the file extension .m. A function file is really just a script file but with a function statement in its first line and with its function name matching its file name. 

Replace the default template by the following statements. 

You can be lazy like me and copy-and-paste the last two statements from your previous code. Save this file as bending_stress, which is the name that MATLAB automatically assigns the file. Thus, the function name and the file name have the same name. 
Bring up beam3.m in the MATLAB editor. Make a copy of beam3.m using Save As ...  and call the new file beam5.m. In this file, comment out the lines below since this calculation is now done within the function.

 

We'll replace these statements with a call to the bending_stress function. The following statement does this:


Add this to beam5.m. Run the file and check the output. You should get the same plot you got with beam3.m.

Sub-Functions

Functions can be called from within a function. You can put multiple functions in one M-file as shown in the following example from the MATLAB help:

In the above example, there are three functions: the main function is called newsstats. Within this function, there are two subfunctions; one called mean and the other, median. The subfunction is defined the exact same way as the main function; but the difference is that the subfunction is available to functions in this M-file only - you cannot call your subfunctions in the command window. In the above example, the main function calls both of the subfunctions, making the code much shorter and simpler. For longer, more complex code, this can save you a lot of debugging time by breaking down your code into smaller pieces.

Open up beam5.m. We will use the subfunction concept to incorporate the main program (beam5.m) and the function bending_stress into one M-file. We are going to designate beam5.m as a function. So at the top of the code, include the following code:

function main_function

Click save as - and name the file main_function - the same as the function's name. Now, open up bending_stress.m and copy the entire code. Paste it below the main function. Close both functions with an end. What you have now should look like this:

Now, run main_function in the command window. The result should be the same plot you saw when you ran beam3.m and beam5.m.

That brings us to the end of this tour. Before we part, let's remind ourselves of some important programming guidelines that we have followed in this tour:

  • Develop code incrementally, testing obsessively at each stage. Develop a plan for how you are going to build your code before you sit at the computer.
  • Dig through the MATLAB help diligently to figure out how to use specific functions etc. Usually, the examples are the best place to start. This is a better strategy than desperately hunting for the TA every time you need help with your code.
  • Comment your program liberally.

In addition to this tutorial, there are several other references to use when learning Matlab. This link here will take you to Mathworks' video tutorials for Matlab.


See and rate the complete Learning Module

Go to all MATLAB Learning Modules

  • No labels