Versions Compared

Key

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

Step 3: Plot

...

σx vs.

...

ri

Calculate

...

σx at O

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

...

There are two types of loops: for and while. Bring up the documentation for the for loop and look at the first example. This shows the format of the forloop. 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-file from beam.m and implement the for loop in that file.

Editor > File > Save As

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

We need to put the following three statements inside of our loop.

Image Added

A little exercise to keep you awake: 1.

  1. Create a counter k around these three statements using the format in the documentation example. We'll create 11

...

  1. ri values, so your counter should go from 1 to 11.

...

  1. Indent the statements inside the loop using the Tab key. This makes it clear what's being looped over.

Don't cheat by looking at my code snippet below.

Image Added

Next, we replace ri and sigma_x with ri(k) and sigma_x(k), respectively. Modify the ri ri statement such that we get a linear variation between ri ri(k)=0.5e-2 m when k=1 and ri ri=1.5e-2 m when k=11.

Image Added

Note that we did not replace I with I(k). Essentially, Iis 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 run faster. Save your program by clicking on the Save icon but don't run it yet.

Since we have replaced some scalar variables with arrays, it's a good idea to clear the Workspace before running the program. At the command prompt, type

clear all

Check that your Workspace is cleared. Type help clearat 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 browser.

...

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.

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\]

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.

In your program, leave a blank line and start a new section for plotting by adding a comment line. Then, plot sigma_x (MPa) vs. ri (cm).

Image Added

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.

Check the plot: Does the σx σx value at ri ri=1 cm look right? Does the trend for σx σx in the plot look right?

Add axis labels:

Image Added

Click the Run icon in the Editor. The underscore symbol (_) gives subscripts and \sigma gives the corresponding Greek symbol. See

...

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 toStep 4: Plot σx vsPlotσxvs.ri: Take 2