0% found this document useful (0 votes)
12 views

Lab 2- Overview of MATLAB and Simulink

The document provides an overview of MATLAB and Simulink, detailing various features and functionalities of MATLAB including variable assignment, matrix manipulation, and plotting. It also introduces Simulink, emphasizing its block diagram representation for systems and the integration with MATLAB. Key concepts such as script and function files, conditional loops, and polynomial operations are highlighted throughout the document.

Uploaded by

202374231
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab 2- Overview of MATLAB and Simulink

The document provides an overview of MATLAB and Simulink, detailing various features and functionalities of MATLAB including variable assignment, matrix manipulation, and plotting. It also introduces Simulink, emphasizing its block diagram representation for systems and the integration with MATLAB. Key concepts such as script and function files, conditional loops, and polynomial operations are highlighted throughout the document.

Uploaded by

202374231
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

University of Sana’a

Faculty of Engineering
Mechatronics Department

LAB2:OVERVIEW OF MATLAB AND


SIMULINK.
By :

Eng. Mohammed Abdalnasser Alzaghir

773215130
FIRST: OVERVIEW OF MATLAB
OVERVIEW OF MATLAB
 Variable Assignment.  Plotting
 Matrix manipulation.  Conditionals and loops.
 Element-wise operator.  Timing.
 Right and left division.  Animation.
 Scalar, vector and matrix functions.  Polynomials.
 Complex numbers.  Others.
 M-Files  Help.
 Scripts.  Matlab Cheat sheet.
 Functions.
 Polynomials.
OVERVIEW OF MATLAB - ASSIGNMENT
 Single Variable assignment.
 A row vector – values are separated by spaces or comma (,).
 A column vector – values are separated by semi–colon (;).
 Matrices: Combination of row and column are created using the
combination of the previous rules, for each new row item put(spaces or
comma (,)) and for each now column put (semi–colon (;)). >> C = [1 2 3 ; 4 5 6 ; 7 8 9]
>> B = [10;12;14;16]
>> A = [1 2 3 4 5]
A = B =
>> v=3 C =
1 2 3 4 5 10
v=
12
3 >>> A = [1,2,3,4,5] 1 2 3
A = 14
4 5 6
1 2 3 4 5 16
7 8 9
OVERVIEW OF MATLAB - ASSIGNMENT
 Linspace: If we want to construct a vector of, say, 100 elements between
0 and 2*pi.
a = linspace(0,(2*pi),100);

 colon notation: If we want to construct a vector of, elements between 0


and 2 with a step size of 0.1
b = 0:0.1:2;
OVERVIEW OF MATLAB – MATRIX
MANIPULATION
 Arithmetic operations – Add and subtract.

>> A+3 >> A-2


 1 2 3
ans = ans =
4 5 6 -1 0 1 A=  4 5 6
7 8 9 2 3 4
 
10 11 12 5 6 7
 7 8 9

 Arithmetic operations – Multiply and divide.

>> A*2 >> A/3  1 2 3


ans = ans =  4 5 6
2 4 6 0.3333 0.6667 1.0000 A=
 
8 10 12 1.3333 1.6667 2.0000  7 8 9
14 16 18 2.3333 2.6667 3.0000
OVERVIEW OF MATLAB – MATRIX
MANIPULATION
 Access elements in a matrix or a vector.
>> A(:,3)
>> A(2,3) ans =
ans = 3
6 6
9  1 2 3
A=  4 5 6
 
>> A(2,:)= [6 5 4]  7 8 9
A =
>> A(1,:)
ans =
1 2 3
1 2 3
6 5 4
7 8 9
OVERVIEW OF MATLAB - MATRIX MANIPULATION
ELEMENT–WISE OPERATOR (.)
 element–wise operator:

Performing operations to every entry in a matrix.

 1 2 3  1 1 1   14 14 14 
A*B  4 5 6  2 2 2 =  32 32 32 
     
 7 8 9  3 3 3  50 50 50 

 1x1 2x1 3x1  1 2 3


 4 x 2 5x 2 6 x 2  8 10 12 
=
A.*B    
 7 x3 8x3 9x3  21 24 27 
OVERVIEW OF MATLAB - MATRIX MANIPULATION
ELEMENT–WISE OPERATOR (.)
element–wise operator and other operators :>> A=[1 2 3;4 5 6;7 8 9]
A=
1 2 3
4 5 6
7 8 9

>> A.^2
ans =
>> c=[1 2]; 1 4 9 A^2 = A * A
>> d=[3 4]; 16 25 36
>> c*d 49 64 81
??? Error using _*_
times >> A^2
ans =
Matrix dimensions must
30 36 42
agree. 66 81 96
102 126 150
OVERVIEW OF MATLAB - MATRIX MANIPULATION
MATRICES (DIVIDE AND ELEMENT-WISE OPERATOR)
 The right division: the conventional division we all day use. >> 8/2
 The left division: Contrary to the right division, the left division reverse ans =
4
the division.
>> 8\2
ans =
0.2500

Using element by element division

Using the left division >> A./B >> A.\B

ans = ans =

0.3333 1.0000 3.0000 1.0000


2.0000 2.0000 0.5000 0.5000
OVERVIEW OF MATLAB – SCALAR, VECTOR AND MATRIX
FUNCTIONS:

 Scalar functions – used for scalars and operate element-wise A=


1 2
when applied to a matrix or vector: 2 2
e.g. sin - cos – tan - atan – asin
>> sin(A)
abs - angle –sqrt ans =
0.8415 0.9093
 Vector functions – operate on vectors returning scalar value:
0.9093 0.9093
e.g. max – min – mean – prod – sum – length
 Matrix functions – perform operations on matrices:

e.g. Size – inv –Bdet


= B= A=
3 2 3 2 1 2 3
1 1 1 1 4 5 6
7 8 9
>> max(B) >> max(B, [],2)
ans = ans = >> size(A)
3 ans =
3 2 1 3 3
OVERVIEW OF MATLAB – COMPLEX NUMBERS

 [i] is one of the special variables used to represent complex >> c = 1+i

Number. c =

 To find the value of complex number use [abs]. 1.0000 + 1.0000i

 To find the value of complex number use [angle].


>> abs(c)
ans =
1.4142

>> angle(c)*(180/pi)
ans =
45
SOLVE EQUATIONS IN MATRIX FORM
 Linsolve

X = linsolve(A,B) solves the matrix


equation AX = B, where A is a symbolic matrix
and B is a symbolic column vector.
OVERVIEW OF MATLAB - MATRIX MANIPULATION

-j5
Task -1:
 Solve for V1 and V2 ?
V1 V2
j10
2-90o 10 1.50o
>> solution

solution =
14.0000+ 8.0000i
28.0000+ 1.0000i (0.1 + j0.2)V1 – j0.2V2 = -j2
- j0.2V1 + j0.1V2 = 1.5

 0.1  j 0.2  j 0.2  V1    j2


  j 0.2 j 0.1   V2 
=  
  1.5 
A x = y
OVERVIEW OF MATLAB – M-FILES
 Script and function files.

M-files

Script Functio
User defined commands
Collections of commands n
Executed in sequence when called Normally has input &
output
Saved with extension “.m”
Saved with extension “.m”
OVERVIEW OF MATLAB – FUNCTIONS
 Script and function files.

The typical syntax for a function is to call the function using the desired input arguments.
The function will return its outputs, which should be saved in new variables
OVERVIEW OF MATLAB – FUNCTIONS
Some other useful matrix functions are the following:
OVERVIEW OF MATLAB - PLOTTING:
t = 0:0.25:7;
[X,Y] = meshgrid(-2:.2:2);
y = sin(t);
plot(t,y)
Z = X .* exp(-X.^2 - Y.^2);
title('Sine Wave as a Function of
surf(X,Y,Z)
Time')
xlabel('Time (secs)')
ylabel('Amplitude')
OVERVIEW OF MATLAB – M-FILES &
PLOTTING
Task-2:
 RLC circuit

Write an m–file to plot Z, Xc and XL versus frequency for R =10, C = 100 uF, L =
0.01 H.
Solution: R = 10 C

+
V
L

 1 
Z R  j L  
 C 
OVERVIEW OF MATLAB – CONDITIONALS AND
LOOPS:
 You can loop over sections of code and conditionally execute sections
using the keywords: [for, while, if, switch].

for v = 1.0:-0.2:0.0 n = 10; n = input('Enter a number: ');


disp(v) f = n;
while n > 1 switch n
end n = n-1; case -1
f = f*n; disp('negative one')
end case 0
disp(['n! = ' num2str(f)]) disp('zero')
case 1
disp('positive one')
otherwise
if overallMean < .49 disp('other value')
disp('Mean is less than expected') end
elseif overallMean > .51
disp('Mean is greater than expected')
else
disp('Mean is within the expected range')
end
OVERVIEW OF MATLAB - ANIMATION:
 Animation plot of sine function as t change from 0 to 7s:

for n = 0:0.25:7
t = 0:0.25:n;
y = sin(t);
plot(t,y)
title('Sine Wave as a Function of Time')
xlabel('Time (secs)')
ylabel('Amplitude')
axis([0 7 -1 1])
pause(0.1)
end
OVERVIEW OF MATLAB - ANIMATION:
Task-3:
 Animation of sine function ?
OVERVIEW OF MATLAB - POLYNOMIALS:
 Polynomials as Vectors

 p = [1 3 -15
-2 9];

 To find the value of the above polynomial at s = 2.


poly_val = polyval( p ,2)

 Finding the roots would be as easy as entering the following command:


s = roots( p )

 Multiply two polynomials together:


x = [1 2];
y = [1 4 8];
z = conv(x,y)
OVERVIEW OF MATLAB – M-FILES
FUNCTIONS
 Function is a ‘black box’ that communicates with workspace through
input and output variables.

function output = function_name (inputs)

Output variable Must match the file


input variable
name

function f = fact(n) >> fact(5)


f = prod(1:n); ans =
end 120
function result = square(x)
result = x^2;
end
OVERVIEW OF MATLAB - OTHERS
 Some special variables[inf, nan, pi]. >> 1/0
 Utility functions to deal with inf: ans =
ex. [isinf, finite] Inf
>> x = log(0);
>> isinf(x) >> x = 0/0
ans = x =
1 NaN

 Utility functions to deal with NaN: >> pi


ex. [isnan] >> isnan(0/0)
ans =
ans = 3.1416
1
OVERVIEW OF MATLAB - HELP
 Use word [doc]: >> doc sin

 Use word [help]:


>> help sin

 Tutorials in the Matlab documentation:

Getting started with Matlab.


Language fundamentals.
MATLAB CHEAT SHEET
SECOND: OVERVIEW OF SIMULINK
OVERVIEW OF SIMULINK :
 In Simulink, systems are drawn on screen as block diagrams. Many elements of block diagrams are available,
such as transfer functions, summing junctions, etc., as well as virtual input and output devices such as function
generators and oscilloscopes. Simulink is integrated with MATLAB and data can be easily transfered between
the programs.
 Basic Elements.
 Simple Example.
 Help in Simulink.
OVERVIEW OF SIMULINK - BASIC
ELEMENTS:
 There are two major classes of items in Simulink:
blocks and lines.
 Blocks are used to generate, modify, combine, output, and display
signals.
 Lines are used to transfer signals from one block to another.
OVERVIEW OF SIMULINK - BASIC ELEMENTS - BLOCKS:

There are several general classes of blocks within the Simulink


library:
 Sources: used to generate various signals.
 Sinks: used to output or display signals.
 Continuous: continuous-time system elements (transfer
functions, state-space models, PID controllers, etc.)
 Math Operations: contains many common math operations (gain,
sum, product, absolute value, etc.)
OVERVIEW OF SIMULINK – SIMPLE
EXAMPLE:
 Only two blocks: source and sink.
NEXT LAB:

Requirement from the students:


 Check of tasks to test your understanding for this lab
FILES ASSOCIATED WITH LAB1:

 Control Tutorials for MATLAB and Simulink - MATLAB Basics Tutorial.pdf


 Control Tutorials for MATLAB and Simulink - Simulink Basics Tutorial.pdf
 Other resources:
- Matlab documentation.
- Simulink documentation.
- Matlab Cheat sheet.

References:
- https://www.mathworks.com/help/index.html
- http://ctms.engin.umich.edu/CTMS/index.php
ANY QUESTIONS ?!

Eng. Mohammed Abdalnaaser Alzaghir

773215130
ANY QUESTIONS ?!

Eng. Mohammed Abdalnaaser Alzaghir

You might also like