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 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 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 couple of minutes 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.

...

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:

{{}}You can be lazy like me and copy-and-paste the last two statements from your previous code. Save this file as bending_stress.m, which is the name that MATLAB automatically assigns the file. Thus, the function name and the .m 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 evaluation 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.

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:

...