Sie sind auf Seite 1von 46

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>R2008b>MATLAB R2008b
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 MathWorks 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 and 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:





| | | | | |
(
(
(

=
(
(
(

=
(
(
(

=
5
2
1
,
4 3 1
3 2 7
2 6 4
,
9 2 9
7 3 8
0 5 0
C B A
| | | |
| | | |
| | | | | | | | A B B A
A B
B A
+ = +
(
(
(


=
(
(
(

+ + +
+ + +
+ + +
=
(
(
(

+
(
(
(

= +
(
(
(


=
(
(
(

+ + +
+ + +
+ + +
=
(
(
(

+
(
(
(

= +
5 1 10
10 5 15
2 11 4
9 4 ) 2 ( 3 9 1
7 3 3 2 8 7
0 2 5 6 0 4
9 2 9
7 3 8
0 5 0
4 3 1
3 2 7
2 6 4
5 1 10
10 5 15
2 11 4
) 4 ( 9 3 2 1 9
3 7 2 3 7 8
) 2 ( 0 6 5 4 0
4 3 1
3 2 7
2 6 4
9 2 9
7 3 8
0 5 0
| | | |
( ) ( )
( ) ( )
( ) ( ) ( ) ( ) ( )
| | | |
( ) ( ) ( ) ( )
( )
( ) ( ) ( ) ( )
| | | | | | | | A B B A
A B
B A
=
(
(
(


=
(
(
(

+ + + + + +
+ + + + + +
+ + + + + +
=
(
(
(

(
(
(

=
(
(
(

=
(
(
(

+ + + + + +
+ + + + + +
+ + + + + +
=
(
(
(

(
(
(

=
15 22 12
41 35 43
24 42 30
9 4 7 3 0 1 2 4 3 3 5 1 9 4 8 3 0 1
9 3 7 2 0 7 2 3 3 2 5 7 9 3 8 2 0 7
9 2 7 6 0 4 2 2 3 6 5 4 9 2 8 6 0 4
9 2 9
7 3 8
0 5 0
4 3 1
3 2 7
2 6 4
60 77 31
35 75 60
15 10 35
4 9 3 2 2 9 3 9 2 2 6 9 1 9 7 2 4 9
4 7 3 3 2 8 3 7 2 3 6 8 1 7 7 3 4 8
4 0 3 5 2 0 3 0 2 5 6 0 1 0 7 5 4 0
4 3 1
3 2 7
2 6 4
9 2 9
7 3 8
0 5 0
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






| | | | | |
| | | | | |
( )
( )
( ) ( ) (
(
(

=
(
(
(

+ +
+ +
+ +
=
(
(
(

(
(
(

= =
= + +
= + +
= + +
(
(
(

=
(
(
(

(
(
(

= =
32
33
10
5 9 2 2 1 9
5 7 2 3 1 8
5 0 2 5 1 0
5
2
1
9 2 9
7 3 8
0 5 0
3 3 33 2 32 1 31
2 3 23 2 22 1 21
1 3 13 2 12 1 11
3
2
1
3
2
1
33 32 31
23 22 21
13 12 11
C A D
D C A C A C A
D C A C A C A
D C A C A C A
D
D
D
C
C
C
A A A
A A A
A A A
C A D
( ) ( ) ( )
( ) ( ) ( ) ( ) ( ) 45 3 9 2 8 0 7 9 9 8 5 7 2 9 3 0
9 2 9
7 3 8
0 5 0
22 31 32 21 13 23 31 33 21 12 23 32 33 22 11
33 32 31
23 22 21
13 12 11
= + =
(
(
(

=
+ =
(
(
(

=
A
A A A A A A A A A A A A A A A
A A A
A A A
A A A
A
| |
(
(
(

=
(
(
(

=
(
(
(

=
(
(
(

=
9 7 0
2 3 5
9 8 0
9 2 9
7 3 8
0 5 0
33 23 13
32 22 12
31 21 11
33 32 31
23 22 21
13 12 11
T T
T
A A A
A A A
A A A
A A A
A A A
A A A
A
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












| |
| |
(
(
(


=
(
(
(
(
(
(
(
(

= = =

=
= = = =

= =
= =

=
=

8889 . 0 1 9556 . 0
0 0 2 . 0
7778 . 0 1 9111 . 0
40
3 5
8 0
1
45
45
2 5
9 0
1
1
45
43
2 3
9 8
1
0
7 0
8 0
1
1 0
9 0
9 0
1
45
9
9 7
9 8
1
1
45
35
7 0
3 5
1
45
45
9 0
2 5
1
1
45
41
9 7
2 3
1
1
1
33
1
32
1
31
1
23
1
22
1
21
1
13
1
12
1
11
1
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
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 (A\B)
Inverse of a matrix (A
-1
=inv(A))
A\B=inv(A)*B








Matrix Operations 5/5
Element by element operation:





In the MATLAB command window type: a.*b
| | | |
| | | |
( )
( ) (
(
(


=
(
(
(




=
(
(
(

(
(
(

=
(
(
(




=
(
(
(

(
(
(

=
36 6 9
21 6 56
0 30 0
4 9 3 2 1 9
3 7 2 3 7 8
2 0 6 5 4 0
4 3 1
3 2 7
2 6 4
.
9 2 9
7 3 8
0 5 0
.
. .
33 33 32 32 31 31
23 23 22 22 21 21
13 13 12 12 11 11
33 32 31
23 22 21
13 12 11
33 32 31
23 22 21
13 12 11
B A
B A B A B A
B A B A B A
B A B A B A
B B B
B B B
B B B
A A A
A A A
A A A
B A
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 F1 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 MATLABs 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, atan2
Degree trigonometric functions:
sind, cosd, tand, atand
Less number of degree trig functions
Conversion from radians to degree
rad2deg(angle in)
Conversion from degree to radians
deg2rad(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
Example of a if/elseif/else statement
if test
statement
elseif 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
( ) ( ) ( ) x x x x x V = =
2
2 10 2 10 2 10
( ) ( ) { }
( ) ( )
6667 . 1 , 0 . 5
12 2
100 12 4 80 80
100 80 12 2 10 4 2 10 0
2
2 2
=


=
+ = + = =
x
x x x x x
dx
V d
( )
( )
6667 . 1
0741 . 74 6667 . 1 6667 . 1 2 10
0 0 . 5 0 . 5 2 10
max
2
2
2
1
max
=
|
|
.
|

\
|

= =
= =
=
cut
x
V
V
V
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 (LineSpec, 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, 3d plots, 3d mesh plots, 3d 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.

Das könnte Ihnen auch gefallen