Lab. Manual

advertisement
‫المملكة العربية السعودية‬
‫وزارة التعليم العالي‬
‫جامعة جازان‬
‫كلية الھندسة‬
‫قسم الھندسة الكھربائية‬
KINGDOM OF SAUDI ARABIA
JAZAN UNIVERSTY
College of Engineering
Electrical Engineering Department
Lab. Manual
Practical Special Topics
(Matlab Programming)
(EngE416)
Prepared By
Dr. Emad Saeid
1 Table of Contents
General Introduction…………………………………………………..………….3
The Purpose of This Manual……………………………………………………..4
Lab Instructions………………………………………………………….……….4
Part One
M-Files & Function Files
Lab. No. 1………………………………………………….………………...…….5
Lab. No. 2………………………………………………………………..……….13
Lab. No. 3…………………………………………………………………..…….14
Lab. No. 4……………………………………………………………………..….19
Lab. No. 5……………………………………………………………….…….….24
Lab. No. 6……………………………………………………………….…….….25
Part Two
Simulink
Introduction……………..……………………………………………………….26
Starting Simulink………………………………………………………..……….27
1st Model………………………………………………………………………….28
Running Simulations…………………………………………………………….30
2nd Model……………………………………………………………………...….31
3rd Model………………………………………………………………………….32
4th Model………………………………………………………………………….33
5th Model………………………………………………………………………….34
2 General Introduction
• MATLAB is a powerful language for technical computing. The name
MATLAB stands for MATrix LABoratory, because its basic data element is
a matrix (array).
• MATLAB can be used for math computations, modeling and simulations,
data analysis and processing, visualization and graphics, and algorithm
development.
• MATLAB is widely used in universities and colleges in introductory and
advanced courses in mathematics, science, and especially in engineering. In
industry the software is used in research, development and design. The
standard MATLAB program has tools (functions) that can be used to solve
common problems.
• In addition, MATLAB has optional toolboxes that are collections of
specialized programs designed to solve specific types of problems.
Examples include toolboxes for signal processing, symbolic calculations,
and control systems.
• Until recently, most of the users of MATLAB have been people who had
previous knowledge of programming languages such as FORTRAN or C,
and switched to MATLAB as the software became popular. Consequently,
the majority of the literature that has been written about MATLAB assumes
that the reader has knowledge of computer programming.
3 The Purpose of This Manual
This manual is intended for students who are using MATLAB for the first time and
have little or no experience in computer programming.
Lab Instructions
When you attend to the lab you must follow the following instructions:
1. Attend at your time, not before not after;
2. Try to use the same computer every time you attend the lab;
3. Open your computer and run the Matlab program;
4. Make a folder with your name and save the programs you perform at
this folder, (this important for your evaluation).
4 Part One
Lab. No. 1
1. Explain the main windows in matlab desktop?
5 1. Command Window
• This window is used to enter variables and run functions and Mfiles.
• For more information, click the Help button.
6 2. Command History
• In this window, we can view previously used functions, and copy and
execute selected lines.
7 3. Launch Pad
•
MATLAB’s Launch Pad provides easy access to tools, demos, and documentation.
8 4. Help browser
• The Help browser is used to search and view documentation for all your
Math Works products.
• To open the Help browser, click the help button in the toolbar, or Type
helpbrowser in the Command Window.
9 5. MATLAB Workspace
•
The MATLAB workspace consists of the set of variables built up during a
MATLAB session and stored in memory.
10 6. Current Directory Browser
• MATLAB file operations use the current directory and the search
path as reference points.
• Any file you want to run must either be in the current directory
or on the search path.
11 2. Enter the following matrix,
A=1 3 4 2
2 0 1 6
4 1 2 7
(a) Get the matrix size
(b) Get the matrix transpose
3. We have the following two matrices
B = [2 2 3; 4 0 6; 8 1 5]
C = [1 1 2; 6 3 5; 1 9 1]
Calculate:
(a) D = B - C
(b) E = B + C
(c) F= E+2
(d) G=B*C
(e) H= B.*C
4. If V1 = 5v, V2 = 6v, Z11=2, Z12=1, Z21=3 Z22=4, get the value of I1 and I2?
5. If A1 = [2 7 6 8 9 10] and B1 = [6 4 3 2 3 4], get
(a) C1 = A1.*B1
(b) D1 = A1./B1
6. If r1 = [ 7 3 5] and s1 = [ 2 4 3], get
(a)
q1 = r1.^s1
(b) q2 = r1.^2
7. state if the following statements are true or false,
(a) If a MATLAB statement ends with a semicolon (;) MATLAB
evaluates the statement but suppresses the display of the results.
(b) The end of each row in entering a matrix, is indicated by a
semicolon (;)
(c) MATLAB is case sensitive in naming variables only.
12 Lab. No. 2
1. Enter the following matrix,
A=1 3 4 2
2 0 1 6
4 1 2 7
0 3 6 4
(a) Get the diagonal of the matrix A
(b) Get the sum of each column in the matrix A
(c) Get the sum of each row in the matrix A
(d) Get the sum of all elements in the matrix A
(e) Add 2 to the element in the 2nd row and 3rd column
2. Enter the following complex number, z = 2-j3 then
(a) Get the real and the imaginary parts of z
(b) Get the magnitude and the phase angle of z
(c) If y = 3+j5, calculate the following: y+z, y-z, y×z
3. If w = [1+j 5-2*j; 3+2*j 4+3*j]
(a) Get the conjugate transpose
(b) Get the point transpose
(c) Type the elements of 2nd row only
4. If R = 10 Ohms and the current is increased from 0 to 10 A with
increments of 2A, write a MATLAB m-file program to generate a table
of current, voltage and power dissipation.
13 Lab No. 3
14 15 16 17 18 Lab No. 4
Function File
1. Write
a
function
file
(name
it
chp6one)
for
the
function
The input to the function is x and the output is f(x). Use the function to
calculate:
a) f(x) for x = 6.
b) f(x) for x = 1, 3, 5, 7, 9, and 11.
2. Write a function file to solve the equivalent resistance of series connected
resistors, R1, R2, R3, …, Rn. Use the function to calculate: the series
equivalent resistance of 10, 20, 15, 16 and 5 ohms.
19 3. plot, xlabel, ylabel, title, and axis commands;
4. The difference between plot, semilogy, semilogx, logog commands
20 5. bar plot command
6. pie plot
21 7. Write an m-file to give the following figure?
figure no. 1
figure no. 2
1
z = cos(x)
y = sin(x)
1
0
-1
0
2
4
x-axis
0
-1
6
0
2
4
6
x-axis
figure no. 3
m = exp(-x)
1
0.5
0
0
1
2
3
4
x-axis
22 5
6
7
8. Write an m-file to give the following figure?
figure no. 1
y = sin(x)
1
0
-1
0
1
2
3
x-axis
figure no. 2
0
-1
6
figure no. 3
0
2
4
0.5
0
6
x-axis
23 5
1
m = exp(-x)
z = cos(x)
1
4
0
5
x-axis
10
Lab No. 5
Q1. What is meant by relational and logical operators, give examples?
Q.2 The following were the daily maximum temperatures (in F): 58 73 73 53
50 48 56 73 73 66 69 63 74 82 84 91 93 89 91 80 59 69 56 64 63 66 64 74 63
69. Use relational and logical operations to determine the following:
a) The number of days the temperature was above 75.
b) The number of days the temperature was between 65 and 80.
c) The days of the month that the temperature was between 50 and 60.
Q.3
Q.4 A 3-bit A/D converter, with an analog input x and digital output y, is
represented by the equation:
y=0
x < -2.5
= 1 -2.5 ≤ x < -1.5
= 2 -1.5 ≤ x < -0.5
= 3 -0.5 ≤ x < 0.5
= 4 0.5 ≤ x < 1.5
= 5 1.5 ≤ x < 2.5
= 6 2.5 ≤ x < 3.5
=7
x ≥ 3.5
(a) Write a MATLAB program to convert analog signal x to digital signal
y.
(b) Test the program by using an analog signal with the following
amplitudes: -1.25, 2.57 and 6.0.
24 Lab No. 6
Q.1 Determine the number of consecutive integer numbers which when added
together will give a value equal to or just less than 210.
--------------------------------------------------------------------------------------------------Q.2
C) The roots of the polynomial f(x)
--------------------------------------------------------------------------------------------------Q.3 If we have the following roots of the polynomial, r = 6.5 4 2.3 -1.2 0.5 find
the polynomial?
--------------------------------------------------------------------------------------------------Q.4 we have
Find
a) f1(x) + f2(x)
b) f1(x) * f2(x)
--------------------------------------------------------------------------------------------------Q.5 Divide
a)
b)
by
25 Part Two
Simulink
Introduction
• Simulink is a graphical extension to MATLAB for the modeling and
simulation of systems.
• 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 devices (such as
function generators) and output devices (such as oscilloscopes).
Simulink is integrated with MATLAB and data can be easily
transferred between the programs.
• In this part, we will introduce the basics of using Simulink to model and
simulate a system.
• Simulink is supported on Unix, Macintosh, and Windows environments,
and it is included in the student version of MATLAB for personal
computers.
26 Starting Simulink
• Simulink is started from the MATLAB command prompt by entering
the following command:
>> Simulink
• Alternatively, you can click on the "Simulink Library Browser" button
at the top of the MATLAB.
27 The Simulink Library Browser window should now appear on the screen.
Most of the blocks needed for modeling basic systems can be found in the
subfolders of the main "Simulink" folder (opened by clicking on the "+" in
front of "Simulink"). Once the "Simulink" folder has been opened, the
Library Browser window should look like:
Building a System
1. 1st Model
To demonstrate how a system is represented using Simulink, we will build the
block diagram for a simple model consisting of a sinusoidal input multiplied
by a constant gain, which is shown below:
28 This model will consist of three blocks: Sine Wave, Gain, and Scope. The Sine
Wave is a Source Block from which a sinusoidal input signal originates. This
signal is transferred through a line in the direction indicated by the arrow to the
Gain Math Block. The Gain block modifies its input signal (multiplies it by a
constant value) and outputs a new signal through a line to the Scope block. The
Scope is a Sink Block used to display a signal (much like an oscilloscope).
29 Running Simulations
Now that our model has been constructed, we are ready to simulate the system. To
do this, go to the Simulation menu and click on Start, or just click on the
"Start/Pause Simulation" button in the model window toolbar (looks like the "Play"
button on a VCR). Because our example is a relatively simple model, its simulation
runs almost instantaneously. With more complicated systems, however, you will be
able to see the progress of the simulation by observing its running time in the the
lower box of the model window. Double-click the Scope block to view the output
of the Gain block for the simulation as a function of time. Once the Scope window
appears, click the "Autoscale" button in its toolbar (looks like a pair of binoculars)
to scale the graph to better fit the window. Having done this, you should see the
following:
30 2. 2nd Model
Signal Routing
Draw the output of each scope?
31 3. 3rd Model
Transfer Functions
32 4. 4th Model
Closed Loop Control System
Here is an example of a closed-loop system with an on-off controller. Notice
the oscillations in the response.
33 5. 5th Model
Integrators and Derivatives
34 
Download