Experiment 01
Experiment 01
Current
Folder
Files in
your files.
Workspace
Explore data that
you create or
import from files.
Command Window
Enter commands at the
command line, indicated
by the prompt (>>).
Command History
View or rerun commands
that you entered at the
command line.
MATLAB is extremely easy to use. A quick introduction to learn how to use MATLAB can be
obtained by typing demo command at the command line, indicated by the prompt (>>).
demo
at Command Window. Typing demo brings up a menu of demonstrations. An on-line Help facility
is also available, providing information on most MATLAB topics. To get a list of Help topics, type
help
gives a long list stars with
matlab\general
- General purpose commands.
matlab\ops
- Operators and special characters.
matlab\lang
- Programming language constructs.
matlab\elmat
- Elementary matrices and matrix manipulation.
:
If
help elmat
is typed then a list contains elementary matrices and matrix manipulation will be seen. To get Help
on a specific topic, type help topic. For examples,
help inv
provides HELP information on the use of the Matrix inverse,
help control
lists comments exists in Control System Toolbox.
MATLAB is usually used in command-driven mode; when single-line commands are entered
MATLAB process them immediately and displays the results. MATLAB is also capable of
executing sequences of commands that are stored in files. Together, these two modes form an
interpretive environment.
3. Variables, Operators, and Numerical Data Types in MATLAB
The variables begin with an alphabetic character and are case sensitive, thus the variable A is
different from a.
a=2
b=5, c=ok, d=1.3;
Default output variable is ans.
All MATLAB variables are multidimensional arrays, no matter what type of data. For instant b
variable is stored as b(1,1)=5. Type the following comments notice the result and massages.
a(1,1)
a(1,2)
a(1,0)
If a statement ended with a semicolon, (;), MATLAB performs the computation, but suppresses the
display of output in the Command Window.
a=2;
Built-in constants are pi, i, j, and Inf.
The comment clear removes variables and who lists variables.
Special characters are [ ] ( ) { } ; % : = . @. For example % is used to add comments
T=0.2; % is the system time constant.
Arithmetic operators are + - / \ ^ .\ . / .* .^. Relational operators are < > <= >= == ~=.
Logical operators are | & || && true false. Operator precedence are ( ) { } [ ] ->Arithmetic
-> Relational -> Logical.
Do not use special characters, operators, or keywords in variable names.
The notations are typed similar to variables;
Exp.1:2
0.5
0.5
y=cos(x)
-0.5
-0.5
-1
-1
0
(a)
(b)
Figure 2: y=cos(x) function; (a) without and (b) with axis labels and title.
Exp.1:3
Command
det(A)
eig(A)
[x,d]=eig(A)
expm(A)
norm(A)
norm(A,1)
norm(A,2)
Commends
Infinity norm
p-norm (vectors only)
f-norm
Characteristic polynomial
Rank
Matrix square root
Sum of diagonal elements
P ( x) x 3 x
P( x) a1 x 3 a 2 x 2 a3 x a 4
1x 3 0 x 2 1x 0
Function M-Files;
a) Extend the MATLAB language,
b) Can accept input arguments with parentheses ( ) and return output arguments with the brackets [],
c) Store variables in internal workspace.
function [out1, out2, ...] = funname(in1, in2, ...) defines function funname that accepts inputs in1,
in2, etc. and returns outputs out1, out2, etc
Function M-Files allow new function to be added to the existing function to create new functions
that solve user specific problems.
The MATLAB product provides a powerful programming language, as well as an interactive
computational environment. It is very easy to create the program files in MATLAB.
A MATLAB program;
a) always has one script M-File,
b) uses built-in functions as well as new functions defined in function M-files,
c) saved as<filename>.m,
d) to run: filename only (no .m extension)
>> <filename>
e) created in Editor / Debugger.
Note: It needs to be saved in your directory not in C:\ ...\Documents \MATLAB. To change and
activate your directory as follows;
Otherwise if you save your files in C:\ ...\Documents \MATLAB they may be conflicted with
existing MATLAB files.
Example 7.1: The M File for Example 6.1 and the graphical result are;
% M File for Example 6. 1
clear all % clear all is used to clear variables and functions from memory.
Px=[1 0 -1 0];
x=-2:0.3:2;
y=polyval(Px,x);
plot(x,y)
xlabel('x'),ylabel('y'),grid
Exp.1:6
6
4
2
0
-2
-4
-6
-2
-1
0
x
1
1
6
2
(( x 0.3) 0.1) (( x 0.9) 2 0.04)
Exp.1:7
B=A.^3
C= [3+4i 0 0, 0 4+3j 0; 0 0 -10]
A(1:3,2)
max(A);
D=[A,B]
E= magic(4)
La=eig(A)
Lc=eig(C)
Task 8.4: Explain the commands given in the following M-file and write returns from the
workspace
% M File for Task 8.4
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
hold on
y2 = cos(x);
plot(x,y2,'r:')
legend('sin','cos')
ylabel('sin(x) & cos(x) ')
title('Plot of the Sine & Cosine Functions')
P1 ( x)
defined in (a) and (b).
P2 ( x)
Task 8.6: Use help to read information for residue built in function.
Write an M-File to get the fraction expansion of
s 2 3s 2
P( s ) 4
s 6 s 3 5s 2 2 s
Exp.1:8
2
8 7 9 x3 4
Task 8.8:
Write an M-Function to model and M-File to solve the differential equation given in equation (1)
and Plot x(t) for the time frame 0 t 10 sec.
1 x1
x1 0
x 3 5 x ,
2
2
x1 (0) 1
x (0) 0
2
(1)
x1 0 6 1 x1
x1 (0) 1
x 6
2 16 x2 , x2 (0) 1
2
x3 5 20 10 x3
x3 (0) 1
and has x(t) solution given in equation (3)
x(t ) x(0)e tA .
write an M-File to Plot x1(t), x2(t) and x3(t) for the time frame 0 t 5 sec.
(2)
(3)
Task 9.2:
a) Write an M-Function to model and an M-File to solve the differential equation given in equation
(4) and Plot x(t) for the time frame 0 t 5 sec.
x1 0 6 1 x1
x 6
2 16 x2 ,
2
x3 5 20 10 x3
x1 (0) 1
x (0) 1
2
x3 (0) 1
(4)
(5)
b) Write an M-File to solve model of the differential equation in equation (5) for n = 4 rad/sec,
=0.5, u = 10, x(0) = [0;0] and for the time frame 0 t 10/(n) sec.
c) Plot x(t)=[x1(t), x2(t)] for the time frame of defined t.
Exp.1:9
Task 9.4: Consider the following block diagram where the blocks dynamics given in terms of the
fraction expansion in equation (6).
10( s 1)
( s 3)
, G p ( s)
( s 3)
s ( s 1)(s 10)
where s is Laplace operator.
R(s)
Gc ( s )
Reference
(6)
Gc(s)
Controller
U(s)
Gp(s)
Y(s)
Output
Plant
Y (s)
Gc ( s )G p ( s)
R( s )
(Hint: type help tf and series in command window read the information)
(7)
Exp.1:10