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

...

Plot σx vs. ri

...

Step 3: Plot σx vs. ri

Calculate σx at O

Essentially, we need to repeat our previous calculation of σx at O for a range of rivalues. One way to do this is to loop over the previous calculation. There are two types of loops: for and while. Let's check the documentation to see what MATLAB provides for loops.

Help > Index (tab)

Search index for loops.

There are two types of loops: for and while. Bring up the documentation for the for loop and look Enter for loop in the search bar and then select for which is the first entry up top. Look at the first example. This shows the format of the for loop. It is noted on this page that " You can often speed up the execution of MATLAB code by replacing for and while loops with vectorized code". That is what we'll do in Step 4. Let's stick it out with slowpoke for loop for now.

We'll create a new M-script file from beam.m and implement the for loop in that file.

Editor (tab) > File Save > Save As

Enter beam2.m for filename. Confirm that you are editing beam2.m (beam.m remains intact).

...

Note that we did not replace I with I(k). Essentially, I is recalculated during each iteration but its value is not stored, saving us some memory. It's always a good idea to be stingy with memory usage since this will make the progam program run faster. Save your program by clicking on the Save icon but don't run it yet.

...

Check that your Workspace is cleared. Type help clear at the command line for help on this command. The command line help is a quick-and-easy way to check the documentation. But the information is not as extensive as the help documentation browser.

Clear the command window by entering clc

...

Our program reports sigma_x at the end of each iteration (since we didn't add a semi-colon after the sigma_x statement). From the output, we can deduce that sigma_x is a row vector, with the columns getting built up at each iteration. Note that a different way to look at the sigma_x array is to double-click on sigma_x in the Workspace. 

How do we know if the results are right? Let's spot-check the result for ri=1e-2 m against the previous step; this value of ri corresponds to k=6. What is your value of sigma_x(k) for k=6? You can check this from the output or by typing sigma_x(6) at the command line. My value is exactly the same as in Step 2 ... nailed that one! How about you?

Now that we have spot checked our result, add a semi-colon after the sigma_x statement to avoid cluttering up the output.

Plot σx vs. ri

Let's pick MATLAB's brain on how to make a plot.

Wiki Markup
{*}Help > Index (tab) > Plot \[2\]{*}

Home (tab) > Documentation (icon) > Search: Plotting Functions > Plotting Functions

Note that you can access this exact page without using the search field. Doing this will give you a sense of how to navigate the documentation. 

Home (tab) > Documentation (icon) > MATLAB > Graphics > 2-D and 3-D Plots > Plotting Basics > Plotting Functions

This page contains These help pages contain a lucid description of the extensive plotting capabilities available in MATLAB. When looking for plotting help, this would be the first place to turn to.

...

Click the Run icon in the Editor to create the plot. Things to note: the factor 1e2 is used to convert ri to cm. The '-r' is the curve style specification with - and r specifying a solid line and red color, respectively. Check the documentation for all the line styles and colors available.

...

Click the Run icon in the Editor. The underscore symbol (_) gives subscripts and \sigma gives the corresponding Greek symbol. SeeHelp > Index (tab) > Greek letters and mathematical symbols 

See my entire program here (opens in a new windowright click and select save target as, or just left-click and copy-paste in the editor).

See how we are building up the program slowly and checking the results obsessively at each stage? This is a good programming approach and ends up being the most time-efficient especially for larger programs. So I'd highly recommend this approach.

Recall that "you can often speed up the execution of MATLAB code by replacing for and while loops with vectorized code". We are into speed, so let's vectorize our program.

Go to Step 4: Plot σx vs. ri:  (Take 2See and rate the complete Learning Module)

Go to all MATLAB Learning Modules