Versions Compared

Key

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

...

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. 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 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).

...

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.

Clear the command window by entering clc

Now run the program from the command line: beam2

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.

...

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.

...

Help > Index (tab) > Greek letters and mathematical symbols

See my entire program here (opens in a new window).

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 toStep to Step 4: Plotσxvs.riPlotσx vs. ri: Take 2