Math1151 Matlab Lab Test Practice

5
School of Mathematics and Statistics University of New South Wales MATH1151 Mathematics for Actuarial Studies and Finance 1A MATLAB Computing Exercises – 2014 The purpose of this handout is to help you prepare for the MATLAB Computing Test in week 10. Model solutions to all the exercises below will be available on UNSW Moodle from the beginning of week 9. You are expected to be familiar with the following MATLAB commands: [ ] create arrays : colon operator .* element by element multiplication ./ element by element division .^ element by element power sum sum prod product max maximum min minimum rref reduced row echelon form \ matrix left-division operator disp display a text message format change the format of screen output eps relative machine precision @(x) create an anonymous function fzero finds zero(s) of a function fminbnd finds a local minimum of a function plot plots data points grid put gridlines on a plot title, xlabel,ylabel annotations for a plot polar polar plot linspace linearly spaced points help MATLAB’s online help function print -dps save plot as postscript file For the test, you also need to understand how to create and modify M-files using the MATLAB editor. A brief guide explaining how to do this when for the questions during the test will be available on UNSW Moodle. The harder exercises are marked with an asterisk. Each test will contain four routine questions and one harder one. 1

description

a

Transcript of Math1151 Matlab Lab Test Practice

  • School of Mathematics and Statistics

    University of New South Wales

    MATH1151

    Mathematics for Actuarial Studies and Finance 1A

    MATLAB Computing Exercises 2014

    The purpose of this handout is to help you prepare for the MATLAB Computing Test in week10. Model solutions to all the exercises below will be available on UNSW Moodle from thebeginning of week 9.

    You are expected to be familiar with the following MATLAB commands:

    [ ] create arrays: colon operator.* element by element multiplication./ element by element division.^ element by element powersum sumprod productmax maximummin minimumrref reduced row echelon form\ matrix left-division operatordisp display a text messageformat change the format of screen outputeps relative machine precision@(x) create an anonymous functionfzero finds zero(s) of a functionfminbnd finds a local minimum of a functionplot plots data pointsgrid put gridlines on a plottitle, xlabel,ylabel annotations for a plotpolar polar plotlinspace linearly spaced pointshelp MATLABs online help functionprint -dps save plot as postscript file

    For the test, you also need to understand how to create and modify M-files using the MATLABeditor. A brief guide explaining how to do this when for the questions during the test will beavailable on UNSW Moodle.

    The harder exercises are marked with an asterisk. Each test will contain four routinequestions and one harder one.

    1

  • Exercises

    1. Enter the following three vectors a, b and c, and evaluate the vector algebra expressions2a + 13b and 12a 4c, if they exist:

    a = [1, 2, 3, 4, . . . , 11] , b = [1, 3, 5, 7, . . . , 21] , c = [1, 4, 9, 16, 25, . . . , 100] .

    2. Evaluate the sum

    100n=1

    1

    nn

    = 1 +1

    2

    2+

    1

    3

    3+ + 1

    100

    100.

    3. Evaluate the product

    100n=1

    (2n)2

    (2n 1)(2n+ 1) =(2)(2)

    (1)(3) (4)(4)

    (3)(5) (6)(6)

    (5)(7) (200)(200)

    (199)(201).

    4. Find the largest number in the infinite sequence a1, a2, a3, . . . if

    an =n5

    2n.

    5. Define the coefficient matrix A and the right-hand-side vector b for the linear system

    4x1 x2 + 3x3 + 2x4 = 3,+ 3x2 + 4x4 = 4,

    3x1 + 2x3 + 3x4 = 2,18x1 + 3x2 + 5x3 + 8x4 = 3.

    Set up the augmented matrix [A | b], calculate the reduced row-echelon form R anddisplay the appropriate message: Linear systems has no solutions, Linear systemhas a unique solution, Linear system has infinitely many solutions. (Decidewhich case is appropriate just by looking at R. You are not asked to find a solution.)

    6. Solve the linear system

    3x1 x3 + 2x4 + x5 = 8,4x2 + 2x3 x5 = 15,

    x1 + 2x3 + x4 = 4,2x2 x3 + 4x4 = 1,

    x1 + x2 + 7x5 = 32.Then calculate the residual vector Ax b and check if it zero.

    7. Create a plot of the polynomial x5 9x3 + 2x 4 for 3 x 3.8. Create a plot of the parametric curve

    x = sin

    (2t+

    pi

    4

    )y =

    t2

    3 t

    for pi

    2 t pi,

    put gridlines on the plot and then save the plot in a PostScript file myplot.ps (usingprint).

    2

  • 9. Create a plot of the polar curve

    r = 5 4 cos(2) for pi pi,

    and then save the plot in a PostScript file myplot.ps (using print).

    10. * Find all real solutions of the nonlinear equation

    x3 = cosx+ 2.

    Your answer(s) should be correct to machine precision.

    11. * The function

    f : x 7 ex2 sin(

    8x pi3

    ),

    has a global minimum at a unique point xmin and a global maximum at a unique point xmax.Find xmin and xmax, correct to at least 5 significant figures.

    3

  • University of New South Wales

    School of Mathematics and Statistics

    MATH1151 SESSION 1 2014

    MATLAB COMPUTING TEST

    SAMPLE VERSION

    Students may not bring any materials to the test except for some writing paper and apen or pencil for rough working.

    You may attempt the questions in any order, but you must create five files called q1.m,q2.m, q3.m, q4.m, q5.m containing your answers to questions 1, 2, 3, 4, 5 respectively.Each file must be a script M-file. Make sure that each file has the .m extension.You must use small (lower case) letters; Q1.m etc. will not be processed.

    Your test will be marked based on a hard copy of your M-file and of the output it producesafter the workspace has been cleared. For example, to test your answer to the firstquestion you should type clear all followed by q1 in the MATLAB command window.Do not include the clear all command in your file q1.m.

    The test may include one question that requires you to create a postscript file calledmyplot.ps. You can view the contents of this file using the file browser (Dophin), or bytyping okular myplot.ps in a Linux terminal window.

    Make sure you SAVE YOUR WORK REGULARLY. Do not create any folders; justkeep your files in your home (login) directory. Matlab will save files there by default.

    Use semi-colons as appropriate to control what your script produces as output. Ex-cessively long output may be penalised. But make sure your script displays theanswer(s) to the question!

    You may use the built-in MATLAB help facilities.

    The test questions are on the back of this sheet.

    4

  • SAMPLE VERSION

    Time allowed: 45 minutes.

    Use semi-colons where appropriate.

    1. Enter the following three vectors a, b and c, and evaluate the vector algebra expressions2a 3b and 7a 4c, if they exist:

    a = [1, 2, 3, 4, . . . , 11] , b = [20, 18, 16, 14 . . . , 2] , c = [1, 4, 9, 16, 25, . . . , 121] .

    2. Evaluate the sum

    50n=20

    2n+ 1

    1 + n2=

    41

    1 + 202+

    43

    1 + 212+ + 99

    1 + 492+

    101

    1 + 502.

    3. Define the coefficient matrix A and the right-hand-side vector b for the linear system

    6x1 + 4x2 x3 + x4 = 5x1 2x2 + x5 = 32x1 + 2x2 + 9x3 + 2x4 = 8

    3x2 4x5 = 11x1 2x3 2x4 + 5x5 = 3.

    Set up the augmented matrix [A | b], calculate the reduced row-echelon form R anddisplay the appropriate message: Linear systems has no solutions, Linear systemhas a unique solution, Linear system has infinitely many solutions. (Decidewhich case is appropriate just by looking at R. You are not asked to find a solution.)

    4. Create a plot of the polynomial x5 9x3 + 2x 4 for 3 x 3 put gridlines on yourplot and then save the plot in a PostScript file myplot.ps (using print).

    5. * Find the unique point of R2 where the two curves

    y = x3 5x 1 and y = 5 ex

    intersect. The coordinates should be correct to machine precision.

    This test paper and any rough workmust be handed back

    at the end of the test.

    5