Sie sind auf Seite 1von 23

Laboratory in Automatic Control

Lab 1
MatLab Basics
2
View or change current directory
Command Window
Current Directory
Command History
Status Bar
3
Desktop | Desktop Layout | Default
4
Editor M-file
File | New |Blank M-File
5
Editor M-file
Command
6
Debug | Save and Run F5
7
Naming rules
The first letter of the filename must be English.
SPACE is not allowed to use.
8
Matlab program
a=1 ;
b=2
c=3
9
clc % clearing the Command Window
10
clear all % clearing the Workspace
Naming Variables
MATLAB variable names must begin with a letter, which may be
followed by any combination of letters, digits, and underscores.
MATLAB distinguishes between uppercase and lowercase
characters, 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
Special Values
Function Return Value
ans
Most 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 its
calculations.
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.
NaN
Not 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.
13
How to use the HELP?
14
15
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.
17
Example
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)
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 e
X
X
X
Y
s s
=
X = 5:0.2:10
Y = exp(-0.2*X)
Result
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') ;

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
22
File | Save As
File Name
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 t t
( (
( (
= =
( (
( (

+
s s title, (4) and ladel,
(5) grid, and (6) legend
x y
Lab1 Homework

Das könnte Ihnen auch gefallen