Include Page | ||||
---|---|---|---|---|
|
Include Page | ||||
---|---|---|---|---|
|
...
Plot σx vs. ri
...
(Take 2)
Vectorize σx Calculation
Again, we pick MATLAB's brain on vectorization:
*Help > Index (tab) > Vectorization \[1\]*Home (tab) > Documentation (icon) > Search: Vectorization > Vectorization (with book icon) Wiki Markup
Go through the example. We'll vectorize our program by replacing loops with array operations. Since the syntax for array operations very closely resembles that for corresponding scalar operations, let's create our program starting from beam.m rather than loopy beam2.m.
Select beam.m in the Editor by clicking on its name at the bottom.
Editor (tab) > File Save > Save As
Enter beam3.m
for filename. Confirm that you are editing beam3.m.
MATLAB's vectorization example shows you how you can create the vector ri using the : operator. Let's check how we can use this animal. At the command prompt, type
...
Note that this is achieved by preceding the ^ operator with the period character (.). The period character can be used with other arithmetic operators such as * and /. Chew over this as it is a subtle but powerful feature. For more info, see
Help > Search Home (tab) > Documentation (icon) > Search for: ) elementwise > Arthimetic Operator: arithmetic operators > Arithmetic Operators + - * / \ ^ '
So in the I statement, we replace ri^4
with ri.^4
...
How do your values of sigma_x correspond to the values from beam2.m? Mine are the same ... yipppee!
Plot σx vs. ri: Take 2
Recycle the plot section from our program in Step 3. Copy this section and append it to your current program:
...
Execute this statement by clicking on the step icon in icon in the Editor. This should bring up the Figure 1 window. Note that the green arrow moves to the next statement indicating that execution is paused there.
Click again on the step icon. clf will clear anything in the current figure i.e. figure 1. This means you start with a clean slate.
...
Keep an eye on the axes lines and labels in your figure as you step through the set(gca,...) command. Did you see that this command changed the font and line styles for the axes? gca stands for get current axes handle; once we have the handle, we can manipulate the axes properties (yup, same as my office). You can use this statement after any plot command; if you have multiple plot commands for the same figure, this statement needs to be used only once.
Now we will make the curve in the plot thicker. Keep an eye on the σx curve as you step through the set(h,...) statement. Did you notice a change in the line style?
Retain an eye on your figure as you step through the remaining statements. You should end up, as below, with a nice-looking plot with legible lines and text. The axis square statement makes the current axis box square in size.
...
Exit debug mode by clicking on the icon the icon in the Editor.
See my entire program here (right click and select save target as).
...
figure(1); %Create figure #1
clf; %Clear current figure
h=plot(,,'');
set(gca,'Box','on','LineWidth',2,...
'FontName','Helvetica',...
'FontSize',14);
%Set axis properties
set(h,'LineWidth',2);
%Set linewidth for curve
xlabel('');
ylabel('');
title('');
axis square; %Make axis box square
Go to Step 5: Plot σx σx vs. ri, ri (Take 3: File Input/OutputSee and rate the complete Learning Module)