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
Panel

Problem Specification
Step 1: Find Reactions RA, RB
Step 2: Calculate σx for ri = 1 cm
Step 3: Plot σx vs. ri
Step 4: Plot σx vs. ri: Take 2
Step 5: Plot σx vs. ri, Take 3: File Input/Output
Step 6: Plot σx vs. ri, Take 4: Functions

...

Find Reactions RA, RB

Derive Equations for RA, RB

Recast into matrix form:

We can solve this system easily. What are the resulting values for RA and RB ? Note these values since we'll compare them to what MATLAB reports. It is, of course, big-time overkill to solve this system using MATLAB (something probably only a government contractor would do). However, it is useful for learning the ropes, so let's go about being a government contractor.

Start MATLAB

Create a working folder called, say, matlab101, in a convenient location. Note the path to this folder so that you will be able to browse to it from MATLAB.

Launch MATLAB on your computer. Set the matlab101 folder as your Current Directory by browsing to it using the browse button near the top of the MATLAB window (see image below). Confirm that the correct path appears in the Current Directory field.

Image RemovedImage Added

Solve Matrix Equation in MATLAB

MATLAB comes with extensive on-line help. Let's see if this documentation can show us how to solve our simultaneous equations. Bring up the on-line help: Select Help > MATLAB Help. Click the Search tab, enter From the home tab select Help > Documentation or simply click the documentation icon shown by an interrogation point. Enter the search phrase solve simultaneous equations and hit Enter. Select the hit entitled mldivide \, mrdivide / Systems of Linear Equations. It's usually easiest to start by looking at the examples provided which tend to be towards the bottom of the help pages. Scroll down to look at Example 1the first example under Square Systems.

Image Removed  Image Added 

This example shows you how to use the \ (mldivide) operator to solve a matrix system.  This help documentation page has some juicy information; glance through it for future reference.unmigrated-wiki-markup

We'll work in the <span style="color: purple"><strong><em>Command Window</em></strong></span> and enter the MATLAB commands at the prompt <span style="color: purple"><strong><em>&gt;&gt;</em></strong></span>. Create matrix <span style="color: blue"><em>A</em></span> by entering {{A = \[1 1; 0 12\]}} at the command the Command Window and enter the MATLAB commands at the prompt >>. Create matrix A by entering A = [1 1; 0 12] at the command prompt:

A semi-colon is used to start a new matrix row. Note that MATLAB shows you the resulting values for the elements of A. Similarly, create matrix B:

...

Calculate the reaction matrix R using the \ operator by entering R = A\B.

Are these the values you expect?

The Workspace window shows all the currently defined variables. If you don't see this window, select Desktop Home (tab) > Layout > Workspace so  so that a tick mark appears next to the window name.

Image RemovedImage Added

Double-click on any variable name in the Workspace to take a closer peek at it.

 We want to start the next part of the tutorial with a clean slate. Some MATLAB commands that can help you do this are:

clear all; % Clear all variables from the workspace

clc; % Clear command window

Another way to do this is to click on the Clear Workspace and Clear Commands buttons.

Create a MATLAB Program

We have essentially used three MATLAB statements to calculate R. These three statements should be shown in the Command History window.

...

If you don't see this window, select Desktop Home (tab) > Layout > Command History .

We'll create a MATLAB program to calculate R using the above three statements. Nothing fancy here: the program will just be a text file with the above statements. Such a file is called an M-File. Leta Script File and have the filename extension .m . Let's take a peek at the documentation on M-Files. In the Help window, select

Contents (tab) > MATLAB > Programming > M-File Programming > Working with M-files

Script files. Go in Help > Documentation and search for Script.

Click on the first hit and glance Glance through this section for future reference.. Also search for Working with files and folders.

Let's now create a Script file and simultaneously bring Bring up the Editor: Select the Command window by clicking in it. In the MATLAB GUI, select

File > New > M-File

. Select Home (tab) > New Script.

First, note that you can dock and undock the editor by clicking on the Show Editor Actions icon,  Image Added,  located in the top right corner of the Editor window. Notice that the editor tabs merges with the three default tabs when the Editor window is docked. 

Now In the Editor, enter the above three statements in the editor (you can be lazy like me and copy-and-paste from the Command History; hold down the Ctrl-key to select multiple statements simultaneously). In addition add the clear all and clc commands at the top to start with a clean slate.

Image RemovedImage Added

Save this file: File > Click on the Save icon
Enter beam.m for  for File name. M-Files need to have .m as the suffixThis will create a .m (script) file. Make sure you are saving into your working directory. Click Save.

Run this program from the command line by typing in the filename without the .m suffix:

...

Let's say we don't want the values of A and B reported each time we run this program. To suppress the reporting of A and B values, add a semi-colon at the end of the statements for creating A and B.Image Removed

Image Added
Save and run the program again, this time using the Run Image Removed icon in the Editoricon   Image Added  from the Editor tab. Check the result in the Command window.
Add comments to your program to lay out, in a human language, what it does. Comments are prefixed with a %; See below.

Image RemovedImage Added

Any line beginning with a % will be skipped over by MATLAB; these are for human eyes only. You should libreally add comments to your program to remind yourself what it does. Otherwise, a few months (or days!) down the line, you'll look at it, wonder what all the fuss is about and curse the ^&^*%%&* author.

Tip: To comment multiples lines at once, highlight the text then either click on the comment button, right-click and select comment or press Ctrl-R. You can uncomment in a similar manner, with the shortcut being Ctrl-T this time. 

I hear you saying "this is a piece of cake, gimme more!". So let's move on to Step 2.

Go to Step 2: Calculate σx for ri = 1 cmSee and rate the complete Learning Module

Go to all MATLAB Learning Modules