Module 4 Matlab - Georgia Institute of Technology

Download Report

Transcript Module 4 Matlab - Georgia Institute of Technology

Systems Realization Laboratory
ME6104: CAD. Module 4.
Module 4 Matlab
ME 6104 – Fundamentals of
Computer-Aided Design
Systems Realization Laboratory
Learning Objectives
ME6104: CAD. Module 4.
• Learn how to use Matlab - layout of
windows, interpreter, on-line help.
• Learn the types of programming
methods.
• Learn Matlab language syntax:
– variables,
– scalars, vectors, matrices
– if, for, while statements
• Learn how to plot graphs.
Systems Realization Laboratory
MATLAB
• MATLAB = MATrix LABoratory.
• Interactive system and programming
language for technical computation.
ME6104: CAD. Module 4.
• Interpreter - like Basic, not compiler like C,
C++, Fortran.
• Toolboxes - prepared sets of integrated
tools for work in a specific area (Controls,
PDE’s).
• We will use it for simple 3D modeling,
parametrics, and graphics.
Systems Realization Laboratory
ME6104: CAD. Module 4.
User Interface
3 Windows:
Command Window - enter Matlab commands,
executed immediately by interpreter, answers
printed in Window.
Graphics Window - displays plots and graphs.
Edit Window - prepare scripts and functions.
Scripts - collection of commands that the
interpreter executes.
Function - collection of commands enveloped in
a function call.
Systems Realization Laboratory
User Interface
Edit Window
ME6104: CAD. Module 4.
Graphics Window
Command Window
Systems Realization Laboratory
Programming
3 Methods:
• Type directly into Command Window: try
new things, experiment, learn Matlab.
ME6104: CAD. Module 4.
• Write scripts using Edit Window: save
commands to execute at one time.
• Write functions using Edit Window: save
commands encapsulated into reusable
fragments (functions).
Example: math functions like sin(), cos(),
tan()...
Systems Realization Laboratory
Matlab Language
Some similarities to C and Basic.
Variables: Scalars, Vectors, Matrices
• Start with a letter
• Can contain letters, digits, and underscore
• Any length, but must be unique within first 19
characters
• Case-sensitive
ME6104: CAD. Module 4.
Examples: engine, Engine, degree2radian, cam_shaft
Scalars: x = 2.0;
deg2rad = pi/180;
Vectors: x = [3.5]; y = [1.5, 3.1];
Matrices: z = [-1, 0, 0; 1, 1, 0; 1, -1, 0; 0, 0, 2];
Systems Realization Laboratory
ME6104: CAD. Module 4.
Language (2)
xy = [x y] yields [3.5, 1.5, 3.1]
Accessing vectors and matrices
x(1) yields 3.5
z(3,2) yields -1
result = y(2) + z(3,2) yields 2.1
Size of Matrix:
size(z) => [4 3] (4 rows, 3 columns)
=> means ‘yields’
What is ‘;’? It instructs the interpreter to NOT
print the result of the computation.
Systems Realization Laboratory
ME6104: CAD. Module 4.
Language (3)
Colon Operator: ‘:’ extracts row or column from
matrix.
A = z(:,1) extracts first column from z
b = z(3:4, 1:2) extracts bottom left 2x2
submatrix.
Transpose Operator: ‘
Row vector is not the same as column vector.
йщ0
B = a’
къ1
къ
a = 0:4; => a = [0 1 2 3 4]
a ' = къ2
къ
къ3
къ
лы4
Systems Realization Laboratory
Language (4)
ME6104: CAD. Module 4.
Special Values and Matrices
• pi
i,j = sqrt(-1)
• Inf = infinity
• NaN = not a number - occurs when dividing
by 0
• eps = machine epsilon
йщ1 1 1
• A = zeros(3)
B = къ
къ1 1 1
йщ1 0 0 0
• B = ones(3)
къ
1
1
1
лы
къ0 1 0 0
йщ0 0 0
• I = eye(4) A = къ0 0 0
I = къ
къ
къ
лы0 0 0
къ0 0 1 0
къ
лы0 0 0 1
Systems Realization Laboratory
Language (5)
Operations:
Scalars Matrices
Addition
+
+
Subtraction
Multiplication
*
*
element by element
ME6104: CAD. Module 4.
Division
Exponentiation
йщ2 4
a = къ
лы3 5
йщ10
b = къ
лы20
/
^
./
.^
c ===
a*b
.*
(element by element)
2*10 + 4 *20
йщйщ
къкъ
3*10 + 5* 20
лылы
d ===
a(:,1).* b
2
10
йщйщйщ
.*
къкъкъ
3
20
лылылы
100
130
20
60
Systems Realization Laboratory
Language - Logic and Control
If Statement:
if logical expression
ME6104: CAD. Module 4.
statements
…
Else and elseif:
if temp > 100
disp(‘Really hot!’);
elseif temp > 65
disp(‘Temp OK’);
else
disp (‘Too cold’);
end
end
if g < 50
cnt = cnt + 1;
fprintf (1, ‘Count is now %d\n’, cnt);
if b > g
b = 0;
end
end
Systems Realization Laboratory
Relational & Logical Operators
ME6104: CAD. Module 4.
Relational Operators
< less than
<= less than or equal
> greater than
>= greater than or equal
== equal
~= not equal
Logical Operators
~ not
& and
| or
Systems Realization Laboratory
Looping - For
ME6104: CAD. Module 4.
For Loops
for index = expression
statements
end
expression is initial: increment: limit
for theta = 0.0: 2.0: 360.0
rth = deg2rad * theta;
ctheta = cos (rth);
Replace ‘for’ loop:
stheta = sin (rth);
theta = 0.0: 2.0: 360.0
end
rth = theta * deg2rad;
ctheta = cos (rth);
stheta = sin (rth);
Systems Realization Laboratory
Looping - While
ME6104: CAD. Module 4.
While Loops
Loop continues executing until “expression”
evaluates to false.
While expression
statements …
end
found_yet = 0;
k = 0;
while k < 10 & (~found_yet)
k = k + 1;
end
Systems Realization Laboratory
Plotting
2D Plots: plot (x,y, ‘abc’)
x, y = vectors of #’s to be plotted
‘abc’ = up to 3 characters that control line type,
color, and how points are plotted.
plot (x,y), title(‘Lab Exp 1’), xlabel(‘Trial’), …
ylabel(‘Distance (ft)’), grid;
ME6104: CAD. Module 4.
3D Plots: plot3 (x,y,z, ‘abc’)
plot3 (x, y, z, ‘b+-’);
plots x, y, z in blue (‘b’), with ‘+’ at the given
data points, and using a solid line (‘-’).
Rotate3D - command to allow you to rotate the
image in the graphics window.
Systems Realization Laboratory
Matlab Summary
• How to use Matlab - layout of windows,
interpreter, on-line help.
• Types of programming methods.
• Matlab language syntax
– variables,
ME6104: CAD. Module 4.
– scalars, vectors, matrices
– if, for, while statements
• Plotting