Beruflich Dokumente
Kultur Dokumente
ECNG 1006
MATLAB EXERCISE 2
Flow Constructs, Scripts and Functions
Contents
1.
2.
3.
In-Lab ...................................................................................................................................... 4
ECNG 1006
Lab and Project Design I
http://myelearning.sta.uwi.edu/
Semester II; 2008 / 2009
1. GENERAL INFORMATION
Lab #:
Name of the Lab:
Lab Weighting:
5%
Delivery mode:
Lab:
SHARED LAB
Lab Dependencies
Recommended
prior knowledge
and skills:
Course Staff
Yes
Position/Role
Course Lecturer
Ronald.DeFour@sta.uwi.edu
Juliet Romeo-Joseph
Juliet.Romeo@sta.uwi.edu
Adelle Joseph
Course Coordinator
Adelle.Joseph@sta.uwi.edu
Azim Abdool
Lab Demonstrator
Azim.Abdool@sta.uwi.edu
Cognitive
Level
Application
Application
3. IN-LAB
Allotted Completion 3 hours
Time:
1 PC with MATLAB version 6.5 or higher
Required lab
Equipment:
b. >> n=10;
>> while(n ~= 0)
>> n=n-1
>> end
c. >> if (n<0)
>> -n
>> elseif (n==0)
>> n=365
>> else
>> n
>> end
Scripts, which do not accept input arguments or return output arguments. They operate
on data in the workspace.
Functions, which can accept input arguments and return output arguments. Internal
variables are local to the function.
MATLAB has an Editor/Debugger for creating and debugging these files. To start the
Editor/Debugger, opening it to a particular file, select Open from the File menu in the Command
Window, or click the Open File (folder icon) button on the toolbar.
The MATLAB Debugger helps you identify programming errors in your MATLAB code.
Using the Debugger, you can:
1. View the contents of the workspace at any time during function execution
2. View the function call stack
3. Set breakpoints in code
4. Execute M-file code line by line
2.1 Scripts
Scripts are M-files that do not accept input arguments or return output arguments. They operate
on data in the workspace. When you invoke a script, MATLAB simply executes the commands
found in the file. Scripts can operate on existing data in the workspace, or they can create new
data on which to operate. Although scripts do not return output arguments, any variables that
they create remain in the workspace, to be used in subsequent computations.
a. Use the MATLAB editor to create a file called magicrank.m that contains these MATLAB
commands:
10 3
8
9
B=
12 14
A
D
where n is the number of plates, is the permittivity of the dielectric (8.85 x 10-12 F/m), A is the
area and d the separation of plates. Given that d = 4 mm and A = 20 cm2, write an M-file to
display a table to select the number of plates needed to obtain a desired capacitance value.
Assume that no more than 10 plates will be used.
2.2 Functions
Functions are M-files that can accept input arguments and return output arguments. The name of
the M-file and of the function should be the same. Functions operate on variables within their
own workspace, separate from the workspace you access at the MATLAB command prompt.
A good example is provided by rank. The M-file rank.m is available in the directory:
toolbox/matlab/matfun
You can see the file with type rank
Here is the file.
function r = rank(A,tol)
% RANK Matrix rank.
% RANK(A) provides an estimate of the number of linearly
% independent rows or columns of a matrix A.
% RANK(A,tol) is the number of singular values of A
% that are larger than tol.
% RANK(A) uses the default tol = max(size(A)) * norm(A) * eps.
s = svd(A);
if nargin==1
tol = max(size(A)') * max(s) * eps;
end
r = sum(s > tol);
The first line of a function M-file starts with the keyword function. It gives the function name
and order of arguments. In this case, there are up to two input arguments and one output
argument.
The next several lines, up to the first blank or executable line, are comment lines that provide the
help text. These lines are printed when you type help rank.
The first line of the help text is the H1 line, which MATLAB displays when you use the look for
command or request help on a directory.
The rest of the file is the executable MATLAB code defining the function. The variable s
introduced in the body of the function, as well as the variables on the first line, r, A and tol, are
all local to the function; they are separate from any variables in the MATLAB workspace.
For a further explanation of functions type:
b. Statistical analysis of a recent inning of a West Indian batsman showed that his scoring
rate (runs per 100 balls faced) was modeled such that:
f ( x) = x
for
0 < x 25
f ( x) =
( x 25) 2
14
f ( x) =
( x 50)
+ 45 for
2
for
25 < x 50
50 < x 100
Where f(x) is the scoring rate (%) and x is the number of runs scored.
Write a function in MATLAB to determine the scoring rate for the batsman at any
particular instant (run scored) in the inning. (Use the appropriate condition statements in
your script). Test the function by finding out the scoring rate at (i) 15 runs, (ii) 48 runs
and (iii) 90 runs.
c. Modify the script above to display the scoring rate versus the runs scored for the entire
inning of the batsman. Label the axis appropriately.
3.1 MATLAB provides the diff function for computing derivative estimates. See MATALB help
on how the diff function works.
X(3)-X(2)
...
X(n)-X(n-1)]
t
dt
a. Write an M-file to solve the differential equation for the case where y ' = ry using Eulers
Method. Given that r = -10 and the initial condition is y(0) = 2. Find the true solution by
hand. Use MATLAB to plot the true solution and the Eulers solution (use o for Euler
plot) on the same plot for the range 0 < t < 0.5. Use a step size t = 0.02.
b. Write an M-file to solve the differential equation for the case where y '= sin t using
Eulers Method. Given that the initial condition is y(0) = 0. Find the true solution by
hand. Use MATLAB to plot the true solution and the Eulers solution (use o for Euler
plot) on the same plot for the range 0 < t < 4. Use a step size t = 2 / 13.
c. Use MATLAB help to find out on the ode23 function. Modify the script of part a to find
the solution of the differential equation and plot the results of the Eulers method, ode23
function and the true results on the same plot.
d. Figure 3.2d shows the series formation of an RLC circuit. For the following parameters:
C = 0.04 F, L = 1H, R = 6 ohms, iL(0) = 4A and vc(0) = -4V.
i.
ii.
Find the equation for the current in the circuit and determine its solution by
hand.
Find the equation for the current in the circuit and determine its solution by
using the Taylors series approximation for numerical differentiation. The
Taylor series approximation is given such that :
Use MATLAB to plot the Use MATLAB to plot the true Current Time
response solution of the system and the Taylors series approximation (use o
for Taylors series approximation plot) on the same plot for the range 0 < t <
3s. Use a step size t = 0.01.
Figure 3.2d
Due Date:
Submission
Procedure:
Deliverables: