MAE 1202 AEROSPACE PRACTICUM An Introduction to MATLAB
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 1 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed by Joel M. Faure
Topics Covered • Basic Ideas: – MATLAB Desktop manipulation – Basic Commands – Matrix Operations – Format – Using Help – Save • Using MATLAB Functions: – Quick function guide – Trigonometric functions – Logical Operations • Plotting with MATLAB: – Optimization Problem – Help Plot – Importing
BASIC IDEAS
Open MATLAB • Programs>Math and Simulation Software>MATLAB>R 2008 b>MATLAB R 2008 b
Menu Bar • Contains the commands you can use to perform certain tasks
Current Directory • Shows the active directory.
Start Button 1/3 • Shows items such as: Help, Demos, Product Webpage.
Start Button 2/3 • Select the “Start Button” • Mouse over the “Web button” • Select the “The Math. Works Web Site” to access the product webpage
Start button 3/3
Command Window • This is where the variables, and MATLAB commands are entered
Workspace • Creates, imports, displays the name, value, min, and max size of the variables.
Command History Window • Shows the time/date at which commands were issued.
Window Manipulation 1/8 • Can customize the desktop layout on the local machine. Here is the default layout:
Window Manipulation 2/8 • First on the menu bar: Help>Product help. A window should shift from the left to the right
Window Manipulation 3/8 • Select the curved arrow on the blue bar in the help window. This will un-dock the help browser. • Exit the help browser
Window Manipulation 4/8 • Press with the left mouse button and hold on the workspace blue bar • Drag the workspace window from right to left until the outline matches the picture below • Release the mouse button
Window Manipulation 5/8 • The workspace window is now “tabbed” with the working directory window on the left side of the desktop.
Window Manipulation 6/8 • Drag the Command history window on the right side of the screen to the left bottom side until it matches the outline in the picture below.
Window Manipulation 7/8 • The desktop layout should now be something like this:
Window Manipulation 8/8 • Now save the layout of desktop: – In the menu bar: Desktop>Save Layout. . . >Type: “practicum default”>press OK
BASIC COMMANDS
Basic Commands 1/6 • x=5 – Press the enter button to enter a variable, or command • y=2; • x+y • 2*x+3*x/y • z=3*y^2
Basic Commands 2/6 • To review the value of the variables recently entered look at the “Workspace window: – Shows the name of the variable – Value of the variable – Min and max values
Basic Commands 3/6 • Use up arrow commands to re-enter a previous command, in reverse sequence – Press the up arrow key once, look at the command window, and press enter – Press the up arrow key 5 times, then press the down arrow key twice, then press enter: • After the 5 times using the arrow key, it shows the x variable • Then the down arrow key sequence moves down to show the x+y variable
Basic Commands 4/6 • Instead of using the arrow keys, use the Command History window: – Inside the Command History window, single “left click”: “ 2*x+3*x/y” – “Right click” the highlighted command select “Copy”: – Click anywhere inside the “Command Window” and right click next to “>>” – Press “Paste button”, the command is now entered into the command window, now press enter button on the keyboard.
Basic Commands 5/6 • Enter in the “clc” command: – Clears the contents of the command window, but does not erase variables that are not in the current memory – Commands in MATLAB is case sensitive • Enter “clear x”: – Clears the x-variable, and only that variable in the workspace • Enter “clear all”: – Clears all of the variables in the workspace
Basic Commands 6/6 • Generate a range of values: – Type “x=0: 0. 25: 100” • This generates an array from 0 to 100, in increments of 0. 25 • array= Start value: Increment: End value – Type “x=0: 100” • This generates an array from 0 to 100, in increments of 1 • array=Start value: End value – Default increment is 1 if it is not specified – Type “countdown=5: -1: 0” • This generates an array with decreasing value, dependent on the increment. • Array=Maximum value (Start value): decrease increment: minimum value
Matrix Operations 1/5 • Go to Page 422 in the MATLAB Handout • Matrix addition: • Matrix Multiplication:
Matrix Operations 2/5 • Matrix multiplication can also be considered as linear equations. • The transpose of any matrix switches the column with row • The determinant a matrix
Matrix Operations 3/5 • The inverse of the A matrix: – Find the determinant of the matrix – Find the transpose of the matrix – Find the cofactors of the matrix – Insert the cofactors into the matrix
Matrix Operations 4/5 • To create a matrix inside MATLAB: – Brackets “[]” – Space or comma “, ” , indicates a new column – A semicolon “; ” indicates a new row • Follow the directions inside example 15. 4 (pg. 422 -423) • Add Matrices (A+B) • Subtract Matrices (A-B) • Multiply Matrices (A*B) • Find the determinant of a Matrix (det(A)) • Follow the directions inside example 15. 5 (pg. 424) – Solve a set of linear equations – Gauss elimination (AB) – Inverse of a matrix (A-1=inv(A)) – AB=inv(A)*B
Matrix Operations 5/5 • Element by element operation: • In the MATLAB command window type: a. *b
Format • Display Functions: – format: • Allows you to display values in certain ways. – disp: • Used to display text or values. Rarely used, often you can just type in the variable name, but disp will leave off the name. – fprintf: • Also a display command that allows you to print text and/or values with a desired number of digits. n and t are line breaks and tabs, respectively. • Example 15. 1 (pg 404) – Uses the fprintf function to display formatted information to the screen • The %g term is a more compact form of exponential notation, insignificant zeros do not print • %s is for character strings, \ is a backslash • For more information on fprintf commands, properties, etc. go to MATLAB help and type the “fprintf”
Using Help • How to launch help: – Three ways: • Press the F 1 key on the keyboard, a window will appear. On the bottom left corner of that window select the “Open Help Browser”.
Using Help • On the menu bar Select Help> Product Help • Instead of typing a function name in the help browser a faster way to determine what the function does is to do the following: – Highlight the function>Right click the highlighted text>select “Help on Selection”.
Using Help
Save • If there are many variables in the directory, rather than retyping them, they can be saved for a later time in a data file. – The data file is similar to a *. dat file format but using MATLAB’s format called a *. mat. • To save the variable workspace: – File>Save Workspace As> Type in filename: “practicum_lab_001”>select the “create new file folder icon > type: “Practicum_lab” in the popup window – Double click the new folder created.
Save • • Change the current directory: Below the menu bar select the button Click the “+” (plus sign) next to MATLAB, and select the “Practicum _lab” folder Press the “OK” button
Save • To create a m-file: – File> New>M-file
Save • Example of a function inside MATLAB • In the command window: – Type “why” • Now type “open why”
USING BASIC MATLAB FUNCTIONS
Basic MATLAB functions • All of the functions (name, description, examples) in MATLAB can be found using help • Anyone using MATLAB uses this resources constantly • In the handout go to page 409 -410 and look at table 15. 7 and 15. 8 – Shows some basic MATLAB functions • Do example 15. 2 in pg 409 • Most trigonometric functions in MATLAB are in radians – Radian trigonometric functions: • sin, cos, tan, atan 2 – Degree trigonometric functions: • sind, cosd, tand, atand – Less number of degree trig functions – Conversion from radians to degree • rad 2 deg(angle in) – Conversion from degree to radians • deg 2 rad(angle in)
Basic MATLAB functions • Logical operators: – Table 15. 10 and 15. 11 pg 412, – If statements use logical operators to determine if something is true/false – Example of a simple if statement • if test • statement • end – Example of a if/else statement • if test • statement • else • statement • end
PLOTTING WITH MATLAB
Plotting with MATLAB • Go to pg 414, look at Example 15. 3 • Optimization problem: – How to determine the maximum volume of a sheet of paper square cuts at every corner by ‘x’ centimeters – This can easily be solved using calculus by taking the differentiation of volume with respect to x and making it equal to zero – Now plug the values into the volume equation, and the cut size will be where the volume is at maximum • Also looking at the volume equation, the larger of the two x values (5 cm) will make the volume zero, or at least smaller
Plotting with MATLAB • Do Example 15. 3 (pg 414) • When finished play around with the plot and line styles – Go on the help browser and type “plot” in keyword search • This will have links to other functions, and line styles – Line styles=Can be found in help (“Line. Spec”, or Table 15. 12 (pg. 416)) • Look up some other types of plot anotation like “legend”, grid, zooming (axis command), x-y-z labels, titles, 3 d plots, 3 d mesh plots, 3 d surface plots, text properties which use the TEX commands which can have greek alphabet, symbols, etc. • Try some of the demos found in help • Best way to learn MATLAB is from the help browser, and by actually using it.
- Slides: 46