To present results compactly and succinctly, you can combine plots into one figure. For example, shearing force, bending moments and torque plots for a shaft can be presented in the same figure using the subplot function.
These subplots were generated using the following syntax.
Here is the full script to generate this figure. (Right-click and select save target as, or just left-click and copy-paste into the editor)
For more information on subplots, you can refer yourself to the MATLAB documentation.
A few noteworthy points to ponder:
help function_name
in the Command Window.We will start by creating the bending stress function that outputs σx given (M, ri, ro ).
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.
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.
We'll replace these statements with a call to the bending_stress function. The following statement does this:
Add this to beam5.m. Run the file and check the output. You should get the same plot you got with beam3.m.
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 following tutorial video.
Your main_function file should look like this.
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.
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 all MATLAB Learning Modules