Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Include Page
MATLAB Google Analytics
MATLAB Google Analytics
Include Page
Intro Learning Module - Panel
Intro Learning Module - Panel

...

Author: Rajesh Bhaskaran, Cornell University

...

Plot σx vs. ri

...

 (Take 4: Functions

...

)

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 Before we start doing this bring up the help file for creating functions in MATLAB. You can find it in the directory specified below:Bring up the following page from Documentation. 

Home (tab) > Documentation (icon) > MATLAB > Programming Help > Contents > MATLAB > Programming > M-File Programming > M-File Scripts and Functions > M-File Functions.

Look at Simple Function Example: The average function that calculates the average of the elements in a vector. As you can see MATLAB has a very good help support system that you can use as a reference whenever you run into problems.

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 As you can see the correct syntax for creating a funcion 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

...

  1. 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

...

  1. MATLAB will not be able to access your function when you call it.
  2. 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 ). Image Removed 

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. 
Image Added

You can be lazy like me and copy-and-paste the last two statements from your previous code. Save this file  
Save this as bending_stress.m, which you can notice is the same name as 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 beam4beam3.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.


 

Now we will need to call the function bending_press we just created to obtain the value for σx that is needed to create the plot. Do this by typing:

Image Removed

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

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

Subfunctions

Functions can be called from within a function. You can put multiple functions in one M-file as shown in the example located in this documentation page.

Home (tab) > Documentation (icon) > Search: Subfunctions > Local Functions

Please go through the subfunction page (or local function as MATLAB calls it) in its entirety before watching the file beam3.m
following tutorial video. 

HTML
<iframe width="600" height="338" src="//www.youtube.com/embed/ld3Li5BAU78?rel=0" frameborder="0" allowfullscreen></iframe>


Your main_function file should look like this. 

Image Added

Subfunction workspace

The last paragraph of the Local Functions documentation page says that "All functions, including local functions, have their own workspace that are separate from the base workspace.  Local functions cannot access variables used by other functions unless you pass them as arguments" and vice-versa. The following video covers this very important concept. 

HTML
<iframe width="600" height="338" src="//www.youtube.com/embed/pnzydaFyEa4?rel=0" frameborder="0" allowfullscreen></iframe>

That brings us to the end of this tourh7. 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.

Go to Tips

Go to all MATLAB Learning Modules