Versions Compared

Key

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

...

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:  

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 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.m, which is the name that MATLAB automatically assigns the file. Thus, the function name and the .m have 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  output. You should get the same plot you got with beam3.m.

...