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)

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.

...

The correct syntax for creating a function is:

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

...

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.

...


Add this to beam5.m. Run the file 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.

...

Please go through the subfunction page (or local function as MATLAB calls it) in its entirety before watching the following tutorial video. 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:

Image Removed

...

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 tour. Before we part, let's remind ourselves of some important programming guidelines that we have followed in this tour:

...

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 Tips

Go to all MATLAB Learning Modules