Sie sind auf Seite 1von 12

THE UNIVERSITY OF THE WEST INDIES

ST. AUGUSTINE, TRINIDAD & TOBAGO, WEST INDIES


FACULTY OF ENGINEERING
Department of Electrical & Computer Engineering
B. Sc. in Electrical & Computer Engineering

ECNG 1006
MATLAB EXERCISE 2
Flow Constructs, Scripts and Functions

Contents
1.

General Information ................................................................................................................ 3

2.

Lab Learning Outcomes .......................................................................................................... 4

3.

In-Lab ...................................................................................................................................... 4

Lab # 2: MATLAB EXERCISE 2

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:

Venue for the Lab:

SHARED LAB

Lab Dependencies

The theoretical background to this lab is provided in ECNG 1000,


ECNG 1009, MATH 1180

Recommended
prior knowledge
and skills:

To undertake this lab, students should be able to:

Course Staff

MATLAB EXERCISE 2: Flow Constructs, Scripts and Functions


Estimated total
study hours:

Yes

Perform basic matrix operations

Identify simple methods for solving differential equations for


electrical based problems.

Position/Role

E-mail

Dr. Ronald De Four

Course Lecturer

Ronald.DeFour@sta.uwi.edu

Juliet Romeo-Joseph

Engineering Practice Coordinator

Juliet.Romeo@sta.uwi.edu

Adelle Joseph

Course Coordinator

Adelle.Joseph@sta.uwi.edu

Azim Abdool

Lab Demonstrator

Azim.Abdool@sta.uwi.edu

Lab # 2: MATLAB EXERCISE 2

2. LAB LEARNING OUTCOMES


Upon successful completion of the lab assignment, students will be able to:
1. Apply basic laboratory tools and a knowledge of the fundamental
principles of electrical and computer engineering to:
(a) The behavior of energy storage electronic components under ac as
well as dc excitation
(b) Three-phase systems
2. Apply mathematical simulation tools for the numerical solution of
differential equations. (Programming in Matlab and use of Eulers method
for the numerical solution of differential solutions)

Cognitive
Level
Application

Application

3. IN-LAB
Allotted Completion 3 hours
Time:
1 PC with MATLAB version 6.5 or higher
Required lab
Equipment:

1. Flow Control Constructs


MATLAB has several flow control constructs:
 if statements
 switch statements
 for loops
 while loops
 continue statements
 break statements
Use MATLAB help to find out more on these commands.
1.1 To quickly get acquainted with these constructs, perform the following in the MATLAB
workspace.
a. >> for x=1:1:10
>> y(x)=x;
>> end
>> y

Lab # 2: MATLAB EXERCISE 2

b. >> n=10;
>> while(n ~= 0)
>> n=n-1
>> end

c. >> if (n<0)
>> -n
>> elseif (n==0)
>> n=365
>> else
>> n
>> end

Lab # 2: MATLAB EXERCISE 2

2. Scripts and Functions


MATLAB is a powerful programming language as well as an interactive computational
environment. Files that contain code in the MATLAB language are called M-files. You create
M-files using a text editor, then use them as you would any other MATLAB function or
command. There are two kinds of M-files:



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:

Lab # 2: MATLAB EXERCISE 2

% Investigate the rank of magic squares (% symbol denotes comment)


r = zeros(1,32);
for n = 3:32
r(n) = rank(magic(n));
end
r
bar(r)

Typing the statement


>> magicrank
in the MATLAB workspace causes MATLAB to execute the commands, compute the rank of the
first 30 magic squares, and plot a bar graph of the result. After execution of the file is complete,
the variables n and r remain in the workspace.

b. Write an M-file to find and display a vector C = A*B where:


6 2
A=

10 3

8
9
B=

12 14

c. For a parallel plate capacitor, the capacitance C, is given by the formula:


C = (n 1)

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.

Lab # 2: MATLAB EXERCISE 2

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.

Lab # 2: MATLAB EXERCISE 2

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:

>> help function


in the MATLAB workspace or use MATLAB help.

a. An object thrown vertically with a speed vo reaches a height h at a time t, where:


1
h = vo t gt 2
2
Write a function that computes the time t required to reach a specific height h, for a given
value of vo. The functions inputs should be h, vo and g. Test your function for the case where
h = 100 meters, vo = 50 meters per second and g = 9.81 meters per second2. Interpret both
results.

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.

Lab # 2: MATLAB EXERCISE 2

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. Introduction to Differential Equations


In MATLAB numerical methods are used to convert a differential equation into a difference
equation that can be programmed.

3.1 MATLAB provides the diff function for computing derivative estimates. See MATALB help
on how the diff function works.

Y = diff(X) calculates differences between adjacent elements of X. If X is a vector, then diff(X)


returns a vector, one element shorter than X, of differences between adjacent elements:
[X(2)-X(1)

X(3)-X(2)

...

X(n)-X(n-1)]

a. Find the diff(x) where x = [5 7 12 20]


b. For the function y = sin(x) where x = [0:pi/50:pi], write an M-file to compute the
derivative of y. Plot the true derivative (cos(x)) with a straight line and the results of the
M-file with o markers on the same plot.
3.2 The Euler Method is the simplest algorithm for numerical solution of a differential equation.
Consider the equation:
dy
= r (t ) y
dt

where r(t) is a known function.

From the definition a the derivative:


dy y (t + t ) y (t )

t
dt

Lab # 2: MATLAB EXERCISE 2

replacing in the original equation gives:


y (t k +1 ) = y (t k ) + r (t k ) y (t k )t
This technique of replacing a differential equation with a difference equation is the Euler
method.

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.

Lab # 2: MATLAB EXERCISE 2

Figure 3.2d

Due Date:
Submission
Procedure:
Deliverables:

February 27, 2009 @ 4PM


Submit to Ms. Adelle Joseph in the Power Simulation Lab

Signed Plagiarism Declaration Form


Typewritten responses to the in-lab questions ONLY, bound in a
folder.

End of Lab # 6: Matlab Exercise 2


Flow Constructs, Scripts and Functions

Das könnte Ihnen auch gefallen