Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

Before we start doing this bring up the help file for creating Let's first pick MATLAB's brain on how to create functions in MATLAB. Bring up the following page in the Help navigator:

...

This page has a simple example: a function called average  that calculates the average of the elements in a vector. Take a minute to peruse this example. 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.

...

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

...

  1. type help function_name in the Command Window.
  2. 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.

We will start by creating the bending stress function that outputs σx given (M, ri, ro ). Create a new page in the editor. Type in the following statements into the new page (yeah, yeah, yeah ... go ahead and copy-and-paste):


Image Added Image Removed 

Save this as bending_stress.m, which you can notice is the name that MATLAB automatically assigns the file. Notice is the same name as the function name.  Make a copy of beam4.m using Save As ...  and call the new file beam5.m

...