Tutorial 1 – Review of MATLAB

advertisement
Tutorial 1 – Review of MATLAB
Semester 2 2006 – Week 1
Aim
•
•
To review basic operations of MATLAB and engineering problem solving
methodology
To do matrix operations and computation for instrumentation systems, solve algebraic
linear equations and plot graphics with MATLAB.
1. Introduction
Engineering Problem Solving Methodology
The process, or methodology, for engineering problem solving consists of five steps:
• State the problem clearly
• Describe the input and output information (modelling) and draw a block diagram
• Work the problem by hand (or with a calculator) for a simple set of data
• Develop a MATLAB program or Simulink model
• Test the solution with variety of data (initial conditions, system parameters)
Flowchart
Flowchart is usually used for a process. Programming is a process in which each task is
executed by one or several commands.
Example: Solving ordinary differential equation
The relationship between the output yaw rate (rad/s) and the input rudder angle (rad) δ and in
a ship system is represented by the following differential equation:
Tr + r = Kδ
where r is output yaw rate (rad/s, δ is input rudder angle (rad), T and K are constant, namely
manoeuvrability indices. Assuming that T = 7.5 seconds and K = 0.11, make a MATLAB
program (M-files) to solve the above equation and plot the yaw rate (degrees) vs time if the
time is in range of 0 to 400 seconds and sampling interval h = 0.1 (sec).
SOLUTION
Step 1: The problem is stated clearly.
Step 2: From the above equation, we have known the relationship between the output yaw rate
and the input rudder angle as shown in Figure 1.
δ (oC)
Ship
r (rad/s)
Figure 1 Ship system
Step 3: Using a numerical method, e.g. Simple Euler method, with initial conditions
of δ = 20 × π /180 (rad) and r(0) = 0 we can compute the output yaw rate as follows:
rk = Kδ k − rk
rk +1 = rk + h × rk
Write a function (m-file) for the ODE.
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
1
Step 4: Develop a MATLAB program (including m-files, user-defined functions) or a
Simulink model (blocks, connections) using the following flowchart in Figure 2.
Step 5: Test the solution with different input rudder angle.
Start
Initial conditions: delta,
r(0), N, index, h, etc
I/O interface
for
k = 0:h:N
% Computation:
index = index + 1;
rdot = K*delta – r;
r = r + h*rdot;
(statements)
Any conditions?
Computations?
k=k+h
Recalling a user-defined
function that is the desired
differential equation
Yes
Execute statements
such as ‘if … end’,
‘switch … case …
end’, or ‘while … end’
No
% Save data
data(index,1) = k;
data(index,2) = delta;
data(index,3) = r;
No
end
If k = N?
Stop computing?
Yes
I/O interface
Plotting or displaying
data: δ vs time, r vs time
Confirm if the solution is
correct and/or satisfied.
Stop
Figure 2 Flowchart for a MATLAB program or a Simulink model
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
2
MATLAB/Simulink
The name MATLAB® stands for MATtrix LABoratory. MATLAB is a high-performance
language for technical computing. It integrates computation, visualization and programming
in an environment where problems and solutions are expressed in familiar mathematical
notation. Simulink® is a software package for modelling, simulating and analysing dynamic
systems. Simulink runs in the MATLAB environment. It supports linear and nonlinear
systems, modelled in continuous time, sampled time, or a hybrid of the two. The version of
MATLAB/Simulink in your computer is the academic (classroom) version consisting of
MATLAB 7.x and Simulink 6.0 - Release 14 (2005).
In this tutorial we practise programming with MATLAB.
Activity 1 (Quick Review of MATLAB)
Run the MATLAB program. Do the following exercises:
1. Use the MATLAB command window for some calculations.
1.1 Enter the following statements in the Command Window:
Statement:
a = 12
Result:
a=
12
Statement:
b = 13
Result
b=
13
Statement:
c=a+b
Result:
c=
25
1.2 Enter the following matrix by typing in the Command Window:
⎡16 3 2 13⎤
⎢ 5 10 11 8 ⎥
⎥
A=⎢
⎢ 9 6 7 12 ⎥
⎢
⎥
⎣ 4 15 14 11⎦
A = [16 3 2 13; 5 10 11 8; 9 6 7 12;4 15 14 11]
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
3
MATLAB displays the matrix you just entered:
A=
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 11
If we take the sum along any row or column, or along either of the two main diagonals, we
will always get the same manner. Let us try the following statements:
>> sum(A)
MATLAB replies with
ans =
34 34 34 34
>> A’
produces
ans =
16 5 9 4
3 10 6 15
2 11 7 14
13 8 12 11
>> sum(A’)
produces a column vector containing the row sums:
ans =
34
34
34
34
1.3 Practise making a program to do the following tasks:
- Calculate values of the following function:
y = sin( x ) (x is in range of 0 to 2π, i.e. 2*3.14)
- Draw graphics of the function.
- Set a grid for the graphics.
- Place a text, eg “x = 0:2*pi”, in the x-axis
- Place a text, eg “Sin of x’, in the y-axis
- Place a title, e.g “Plot of the Sine Function”, on the top of graphics.
You may open and edit an M-file as follows:
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
4
Sample Program in M-file:
% My first program in MATLAB codes: creates a vector of x values ranging from
% zero to 2π, computes the sine of these values and plot the result.
clear
% to clear all variables in the workspace
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
grid
xlabel('x = 0:2*pi')
ylabel('Sine of x')
title('Plot of the Sine Function', 'FontSize',12)
% End of my first program in MATLAB codes!
Then save the M-file as <My_First_Program.m>. Run the program.
Notes: The semicolon (;) is used to stop displaying the calculated result on the Command
Window when the program is running.
The result should look like:
Plot of the Sine Function
1
0.8
0.6
0.4
Sine of x
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
1
2
3
4
5
6
7
x = 0:2*pi
You may use ‘for … end’ loop (iterative computation) to do this activity.
Sample codes:
% Tute1Activity1_3.m
% To illustate how to use 'for ... end' loop:
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
5
index = 0;
for ii = 0:pi/100:2*pi
index = index + 1;
x = ii;
y = sin(x);
data(index,1) = x;
data(index,2) = y;
end
plot(data(:,1),data(:,2));
grid
xlabel('x = 0:2*pi')
ylabel('Sine of x')
title('Plot of the Sine Function', 'FontSize',12)
% End of program Tute1Activity1_3
If you view the workspace, you will obtain the following information:
» whos
Name
x
y
Size
1x201
1x201
Bytes Class
1608 double array
1608 double array
Grand total is 402 elements using 3216 bytes
2. Use the online help to get information about MATLAB functions you want to use (if
possible). Try to use the command “help <function_name>” in the MATLAB command
window in order to get description of a MATLAB function, for examples,
> help plot
> help sin
> help grid
> help xlabl
> help ylabel
> help title
> help input
> help fpen
> help fclose
> help fprint
> help function
etc.
Tips: When editing MATLAB codes, it is practical to write your own comments and make your
program easy to read! In MATLAB, the percentage mark (%) is used to start a comment.
Well-organised and structured programs will help to debug quickly.
Activity 2 Matrix operations
2.1 Create an M-file to compute the following matrices:
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
6
a) AB (b) ABT (c) A-1 (d) DCDT (e) C–1 (f) (ADAT)–1 (g) BC – D–1 (h) A + B + C
⎡1 − 2⎤
⎡ 5 3⎤
⎡ 1 − 2⎤
⎡5 2 ⎤
where A = ⎢
B= ⎢
C=⎢
D=⎢
⎥
⎥
⎥
⎥.
⎣3 4 ⎦
⎣2 7⎦
⎣− 3 6 ⎦
⎣3 7 ⎦
Save the M-file (as Tute1Activity2_YourName.m) and run it.
2.2 Using ‘help function-name’ to get information on the following functions that are used for
matrix operations:
•
•
•
•
•
•
•
•
Eigenvalues and eigenvector: eig
Singular value decomposition: svd
Pseudoinverse: pinv
Identity matrix: eye
Size of a matrix: size
Length of a vector: length
Inverse matrix: inv
Transpose matrix: ‘
Add the following statements into the M-file you created in 1:
size(A)
length(A(:,1))
Save the M-file and run it.
Activity 3 Solving algebraic linear or nonlinear equations
Create an M-file to solve the following set of linear equations:
x1 + x2 = 36
2x1 + 4x2 = 100
Save the M-file and run it.
Activity 4
A temperature-measuring system incorporates a platinum resistance thermometer, a
Wheatstone bridge, a voltage amplifier, and a pen recorder. The individual sensitivities are as
follows:
Transducer:
0.45 ohm/oC
Wheatstone bridge: 0.02 V/ohm
Amplifier gain:
100 V/V
Pen recorder:
0.2 cm/V
Make an M-file to calculate:
(a) the overall system sensitivity
(b) temperature change corresponding to a recorder pen movement of 5cm.
Sample M-file:
% filename: Activity4.m
% This program calculate overall sensitivity of a measuring system consisting of
% a Platinum resistance thermometer, a Wheatstone bridge, a voltage amplifier
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
7
% and a pen recorder.
% We have: K = K 1 × K 2 × K 3 × K 4 = 0.45(ohm/oC) × 0.02(V/ohm) × 100(V/V) × 0.2(cm/V)
%
= 0.1800 (cm/oC)
∆d
5
∆d
, we have ∆T =
= 27.7778oC
%K=
=
∆T
K
0.18
%
K1 = 0.45;
% ohm/oC
K2 = 0.02;
% V/ohm
K3 = 100;
% V/V
K4 = 0.2;
% cm/V
Delta_d = 5; % cm
% Overall sensitivity:
K = K1*K2*K3*K4;
% cm/oC
% Temperature change corresponding to a recorder pen movement of 5cm:
Delta_T = Delta_d./K;
% oC (Celsius degrees)
% End of Program Activity4
Useful websites for MATLAB/Simulink
1. MATLAB Tutorials: http://www.engin.umich.edu/group/ctm/basic/basic.html
2. Simulink Tutorials: http://rclsgi.eng.ohio-state.edu/~faidley/Simulink_Tutorial.pdf
3. MathWorks’ Student Centre – MATLAB Tutorials and Simulink Tutorials:
http://www.mathworks.com/academia/student_center/tutorials/index.html?link=body#
Unit: E07 267 Instrumentation and Process Control
Copyright © 2006 Hung Nguyen H.Nguyen@amc.edu.au
8
Download