laboratory in automatic control lab1
Post on 06-Apr-2018
223 Views
Preview:
TRANSCRIPT
-
8/2/2019 Laboratory in Automatic Control Lab1
1/23
Laboratory in Automatic Control
Lab 1
MatLab Basics
-
8/2/2019 Laboratory in Automatic Control Lab1
2/23
2
View or change current directory
Command Window
Current Directory
Command History
Status Bar
-
8/2/2019 Laboratory in Automatic Control Lab1
3/23
3
Desktop | Desktop Layout | Default
-
8/2/2019 Laboratory in Automatic Control Lab1
4/23
4
Editor M-file
File | New |Blank M-File
-
8/2/2019 Laboratory in Automatic Control Lab1
5/23
5
Editor M-file
Command
-
8/2/2019 Laboratory in Automatic Control Lab1
6/23
6
Debug | Save and Run F5
-
8/2/2019 Laboratory in Automatic Control Lab1
7/23
7
Naming rules
The first letter of the filename must be English.
SPACE is not allowed to use.
-
8/2/2019 Laboratory in Automatic Control Lab1
8/23
8
Matlab program
a=1 ;
b=2
c=3
-
8/2/2019 Laboratory in Automatic Control Lab1
9/23
9
clc % clearing the Command Window
-
8/2/2019 Laboratory in Automatic Control Lab1
10/23
10
clear all % clearing the Workspace
-
8/2/2019 Laboratory in Automatic Control Lab1
11/23
Naming Variables
MATLAB variable names must begin with a letter, which may befollowed by any combination of letters, digits, and underscores.
MATLAB distinguishes between uppercase and lowercasecharacters, so A and a are not the same variable.
Avoid Using Function Names for Variables To test whether a proposed variable name is already used as a
function name, use
which -all variable_name
-
8/2/2019 Laboratory in Automatic Control Lab1
12/23
Special Values
Function Return Value
ansMost recent answer (variable). If you do not assign an output variable to an expression,
MATLAB automatically stores the result in ans.
eps Floating-point relative accuracy. This is the tolerance MATLAB uses in itscalculations.
intmax Largest 8-, 16-, 32-, or 64-bit integer your computer can represent.
intmin Smallest 8-, 16-, 32-, or 64-bit integer your computer can represent.
realmax Largest floating-point number your computer can represent.
realmin Smallest positive floating-point number your computer can represent.
pi 3.1415926535897...
i, j Imaginary unit.
inf Infinity. Calculations like n/0, where n is any nonzero real value, result in inf.
NaNNot a Number, an invalid numeric value. Expressions like 0/0 and inf/inf result in a
NaN, as do arithmetic operations involving a NaN.
version MATLAB version string.
Several functions return important special values that you can
use in your M-files.
-
8/2/2019 Laboratory in Automatic Control Lab1
13/23
13
How to use the HELP?
-
8/2/2019 Laboratory in Automatic Control Lab1
14/23
14
-
8/2/2019 Laboratory in Automatic Control Lab1
15/23
15
-
8/2/2019 Laboratory in Automatic Control Lab1
16/23
Mathematical Operators
Mathematical Operators
+ Addition or unary plus.
- Subtraction or unary minus.
* Matrix multiplication.
.* Array multiplication.
/ Backslash or matrix left division.
./ Array left division.
^ Matrix power.
.^ Array power.
Matrix transpose.
-
8/2/2019 Laboratory in Automatic Control Lab1
17/23
17
Example
-
8/2/2019 Laboratory in Automatic Control Lab1
18/23
Subplot function
Syntax
Subplot(m,n,p)
Matlab code
x = -10:0.1:10;y = x.^2 ;
subplot(2,2,1),plot(x,sin(x))
subplot(2,2,2),plot(x,y,'--')
subplot(2,2,3),plot(-x)
subplot(2,2,4)
subplot(2,2,1),plot(x,sin(x)) subplot(2,2,2),plot(x,y,'--')
subplot(2,2,3),plot(-x) subplot(2,2,4)
-
8/2/2019 Laboratory in Automatic Control Lab1
19/23
19
How to do exponential ?
Syntax
Y=exp(X)
Example
Matlab code
-0.2
When 5 10
and vector in increment of 0.2
Solve eX
X
X
Y
X = 5:0.2:10
Y = exp(-0.2*X)
Result
-
8/2/2019 Laboratory in Automatic Control Lab1
20/23
Plot Function
Syntax
Plot(X, Y, 'PropertyName',PropertyValue,...)
Matlab code
Result
X = -pi:0.1:pi ;
Y = cos(X) ;Z = sin(X) ;
plot(X, Y, '-xr', X, Z, ':og') ;
-
8/2/2019 Laboratory in Automatic Control Lab1
21/23
21
Matlab code
X = -pi:0.1:pi;Y = cos(X);
Z = sin(X);
plot(X, Y, '-xr', X, Z, ':og');
legend('cos_x','sin_x',0);
Xlabel('X');
Ylabel('Return Values');
title('Plot of function');
grid on;
Result
Legend
Ylabel
Xlabel
Title
Grid
-
8/2/2019 Laboratory in Automatic Control Lab1
22/23
22
File | Save As
File Name
-
8/2/2019 Laboratory in Automatic Control Lab1
23/23
23
2 '
-
1.
1 2 3 9 8 7
4 5 6 , 6 5 4
7 8 9 3 2 1
solve : ( ) , ( ) , ( ) , and ( )
2.
( ) plot: cos( )
(b) plot: sin( )
(c) plot: cos( ) and sin( )
(d) plot:note:
(1) subplot, (2) - ,0.1, (3)
x
A B
a A B b A B c A d B
a x
x
x x
e
x
title, (4) and ladel,
(5) grid, and (6) legend
x y
Lab1 Homework
top related