0% found this document useful (0 votes)
26 views3 pages

Lab 3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

ELEC2103 Simulation & Numerical Solutions in Engineering - 2015

Laboratory 3 More Programming in MATLAB


This laboratory continues the basics of programming in MATLAB started in laboratory 2. It is
designed to give you more practice in MATLAB programming. Mainly, the exercises go
beyond those in the previous laboratory in that you are now being asked to devise a solution
and then translate this solution into MATLAB code. Topics discussed include M-files, control
flow, structures and cell arrays, and more on graphics.

Exercises
Exercise 1
Test your understanding problems: T4.5-3, T4.6-2, T4.7-1 (Intro Matlab 7 ed3); T4.5-
1, T4.5-3, T4.6-1 (Intro Matlab 7 ed2); or T4.4-1, T4.4-3, T4.5-1 (Intro Matlab 6).

Easy Plotting
MATLAB has a number of functions for use when data points do not have to be
specified exactly. The function fplot plots functions defined by a M-file name or
function handle. Thus
fplot('sin', [0 2*pi])
or, equivalently,
fplot(@sin, [0 2*pi])
plot the sine function over the range shown. MATLAB does the job of working out
numbers of data points, etc. The function title, axis, etc can be used as with plot.
The function ezplot plots functions defined by string expressions. Using
ezplot('sin(x)/x', [-10 10])
it plots sin( x) / x over the range specified. It can also be used to plot an implicit
function. For example,
ezplot('x^2+(y/3)^2-1', [-2 2 -4 4])

plots the ellipse x 2  ( y / 3) 2  1 .


Exercise 2
Plot the function f ( x, y )  sin( x) *cos( y ) for 2  x  2 and 2  y  2 .
Exercise 3
Honours were awarded on the basis of a “grand wam”, an average mark over all units
of study, weighted according to credit point value and according to the nominal year
of each unit of study. The formula is:
GWAM =  M C Y C Y
i i i i i

Where M is the mark, C the credit point value and Y the year (values of 1, 2, 3 or 4).
Honours are awarded as follows: H1 for GWAM 75, H2(1) for 75 >GWAM 70,
H2(2) for 70 >GWAM 65. Write a function which will accept a 3-column matrix
containing a mark, credit point value and year in each row and will return a value for
the GWAM (it could consist of just one line).

Laboratory 3 1
ELEC2103 Simulation & Numerical Solutions in Engineering - 2015

Ming obtained the following results :

1st Year 2nd Year 3rd Year 4th Year


Mark CP Mark CP Mark CP Mark CP
66 6 68 4 76 4 74 12
54 6 77 4 70 4
70 4 69 4
What grade of Honours will he be awarded?
Answer: Second class, division 1.
Exercise 4
Legendre polynomials, Pn ( x), n  0,1, are defined recursively as follows:
nPn ( x)  (2n  1) xPn 1 ( x)  (n  1) Pn  2 ( x) n  2, 3,  with P0 ( x)  1, P1 ( x)  x .
Write a function that takes an integer n and returns the coefficients of Pn ( x ) in
descending order of powers. Use only one for loop.
Use the function polyval to evaluate the Legendre polynomial of degree 6 at 0.1.
Answer: P6 0.1  - 0.2488
Exercise 5
The elliptic integral

2
dt
K (k 2 )   , 0  k 1
0 1  k 2 sin 2 (t )
cannot be evaluated in terms of elementary functions. Gauss devised an algorithm to
solve this integral which uses a sequence of arithmetic means {a n } and geometric
means {bn } , where

a 0  1, b0  1  k 2
a n  a n 1  bn 1  / 2, bn  a n 1bn 1 , n  1, 2, 


Both sequences have the same limit g and K (k 2 )  . Also, a n  bn for all n.
2g
2
Write a MATLAB function that computes the elliptic integral K ( k ) . Use a while
loop to generate the sequences and continue looping until a n  bn  eps , where eps is
the relative floating point accuracy value returned by the MATLAB function eps.
Write your code so that the function takes either a single number or an array of
numbers as k2-values. Your code should use only one loop, the while loop. Return the
answer in the same format (row or column vector) as the input.
What is the value of the elliptic integral for k2 = 0.4, 0.5, 0.6?
Answer: 1.7775, 1.8541, 1.9496

Laboratory 3 2
ELEC2103 Simulation & Numerical Solutions in Engineering - 2015

Exercise 6
Chapter 4, Problem 26 in IM7ed3, 23 in IM7ed2; 16 in IM6
Exercise 7
Chapter 4, Problem 37 in IM7ed3; 31 in IM7ed2; 24 in IM6

Optional problems, Ex 8 is worth looking at

Exercise 8
Chapter 4, Problem 38 in IM7ed3; 32 in IM7ed2; 25 in IM6
Exercise 9
Chapter 4, Problem 28 in IM7ed3; 25 in IM7ed2; 18 in IM6

Laboratory 3 3

You might also like