AP3114 Lecture WK2 Matrix Statistics

Click here to load reader

download AP3114 Lecture WK2 Matrix Statistics

of 66

description

Physics

Transcript of AP3114 Lecture WK2 Matrix Statistics

AP3114/Computational Methods for Physicists and Materials Engineers

Dr. Jun FanSep. 7, 2015AP3114/Computational Methods for Physicists and Materials Engineers2. Matrix

WKSyllabus 1 (Aug. 31,2015)Introduction & simple operations2 (Sep. 7, 2015)Matrix, Statistical description and analysis of data3 (Sep. 14, 2015)Flow control (script files; for/done, if/else)User-defined functions4 (Sep. 21, 2015)Plots & Random Numbers5 (Sep. 28, 2015)University Holiday6 (Oct. 5, 2015)Bisection method7 (Oct. 12, 2015)Practical Test 1 (Lectures 1-4)8 (Oct. 19, 2015)Newton-Raphson method9 (Oct. 26, 2015)More programming examples 10 (Nov. 2, 2015)Solving differential equations I11 (Nov. 9, 2015)Solving differential equations II12 (Nov. 16, 2015)Fourier Analysis13 (Nov. 23, 2015)Project PresentationRevision (Nov. 30, 2015)Practical Test 2 (All lectures) Tentative Class Schedule3Assessment Tasks/ActivitiesPractical Tests: 60% (20% PT1 & 35% PT2 & 5% small tests)Assignments and lab reports: 40% (10% project, 30% homework)75% lecture attendance rate must be obtained. Marks will be deducted for students failing to meet the attendance requirement.Homework due: 9 am Monday in class, hard copy Homework late policy: 10% off per day34Recommended ReadingReference Book(s):

Essential MATLAB for Scientists and Engineers, 2nd/3rd/4th/5th Edition, by Brian D. Hahn.

Modeling and Simulation in Scilab/Scicos, Stephen L. Campbell, Jean-Philippe Chancelier and Ramine Nikoukhah, Springer, 2006

http://www.scilab.org/resources/documentation/tutorialsIntroduction to Scilab (introscilab.pdf)Scilab for very beginners (Scilab_beginners.pdf)

4Programmable CalculatorScilabwww.scilab.org

Download (select a version for your own computer, Windows/Mac/Linux) & installhttp://www.scilab.org/download/5.5.1A copy for Windows has been upload to Canvas

WindowsMacLinuxScilab interface

Start -> ScilabDirectory& filesCommand historyVariablesCommand Input InterfacePractice is the most important Ihear, I forgetIsee, I rememberIdo, I learn

7HelpTo get help, type help> help derivative

Editor

>editorThen, save it as *.sce file, for instant, myscript.sceTo run: exec(filename.sce), in this case, exec(myscript.sce)

Type:In the command box, you will see the following: 1)2)3) 4) Review of lecture 1Creating a variableComments (//)/continuation(..)Elementary mathematical functionsBoolean (%T, %F)Complex numbersIntegerStrings (test, +)Dynamic variables

Variable namesVariable names may be as long as the user wants, but only the first 24 characters are taken into account in Scilab.Case sensitivea to z, A to Z, 0 to 9, %, _, #, !, $, ?Starting with % has special meaning, such as %piPre-defined mathematical variablesIn Scilab, several mathematical variables are pre-defined, whose name starts with a percent % character.

Creating real variablesElementary mathematical operators

Comments and continuation linesComments: Any line which begins with two slashes//Continuation lines: any line which ends with two dots..

List of Elementary Mathematical FunctionsTrigonometry

Other functions

BooleansBoolean variables can store true (%t, or %T) or false (%f, or %F) values.

Complex numbers

In Scilab, %i represents the mathematical imaginary number i?

is used to compute the conjugate real returns real part imag returns imaginary part

IntegersInteger type:

Number of bits used to store an integer and the range of value:n-bit signed integer: [-2n-1, 2n-1-1]n-bit unsigned integer: [0, 2n-1]

Circular integers and portability issues

Different from other mathematical packages, such as Octave or Matlab: if an integer is at the upper limit, the next integer stays at the upper limitThe Scilab circular way allows for a greater flexibility in the processing of integers, since it allows to write algorithms with fewer if statementsThe ans variable

When we make a computation and do not store the result into an output variable, the result is stored in the default ans variableStrings

Strings can be stored in variables, provided that they are delimited by double quotes .+ can operate strings.Dynamic type of variables

Scilab allows to change the type of a variable dynamically. The following example shows that we first create a real value, and then put a string variable in it:Review of lecture 1Creating a variableComments (//)/continuation(..)Elementary mathematical functionsBoolean (%T, %F)Complex numbersIntegerStrings (test, +)Dynamic variables

Create a matrix of real valuesSquare brackets [ ] mark the beginning and the end of the matrixCommas , separate the values in different columnsSemicolons ; separate the values of different rows.

Create a MatrixOr, 2nd way: blank space separates the columns, while the new line separates the rows

Functions generating matrices

Empty matrixA=[ ] create a 0X0/empty matrix, this syntax allows to delete the content of a matrix, so that the associated memory is freed.

Query matricessize returns the two output arguments nr and nc, which are the number of rows and the number of columns

Accessing the elements of a matrixThe whole matrix, with the A syntaxElement by element with the A(i,j) syntax

The colon : operator:A range of index values with the colon : operatorv=i:j returns a vector of from i to j

v=i:s:j returns a vector with values from i to j, step s

Access to a matrix with the colon operators and t are the steps

Hilbert matrixThe eye matrixThe eye matrix create the identity matrix with the size which depends on the context

eye(m,n) creates an identity matrix with m rows and n columns

Matrices are dynamicThe size of a matrix can grow or reduce dynamically

The dollar $ operatorThe dollar $ operator allows to reference elements from the end of the matrix. $ signifies the index corresponding to the last row or column.

$+1: to add at the end

Low-level operations+, -, *, /

XB=A => X=A/B=AB-1 AX=B => X=A\B=A-1B

Elementwise operationsIf a dot . is written before an operator, it is associated with an elementwise operator, i.e., the operation is performed element-by-element

Elementwise operators

Conjugate transpose and non-conjugate transposereal number/matrix => transpose Complex number/matrix => transpose and conjugatesElementary single quote . only transpose, no conjugating the matrix, be it real or complex.

Multiplication of two vectors

Compare two matrix

Elementary functions

Elementary functions

Common functions for linear algebra

summary of sample statisticsFunction: sum-->a=[1:3;4:6;7:9] a = 1. 2. 3. 4. 5. 6. 7. 8. 9. -->sum(a) ans = 45.

-->sum(a, 1) or-->sum(a,c') ans = 12. 15. 18.

-->sum(a,r') ans = 6. 15. 24.The sum of all elements of each columnThe sum of all element of each rowThe sum of all element of the matrix aa=[1:3;4:6;7:9]sum(a)sum(a, 1)sum(a,'r')sum(a,'c')

45Median--Mid value of x

If the number of elements in x is odd then median(x)=xn+1/2 median(x)=5

If the number of elements in X is even then median(x)=(xn/2 + xn/2+1)/2

x=[-2,3,5,8,9]

mean(x)

median(x)summary of sample statisticsFunctions: mean, median:x=[-2,3,5,8,9]x=[-2,3,5,8,9]

mean(x)

median(x)

46Median in rows-->median(a,'r') ans = 9. 10. 11. 12. Median in columns-->median(a,'c') ans = 2.5 6.5 10.5 14.5 18.5 Mean values in rows-->mean(a,'r') ans = 9. 10. 11. 12.

Mean values in columns-->mean(a,'c') ans = 2.5 6.5 10.5 14.5 18.5 mean(a, 'r')

mean(a, 'c')

median(a, 'r')

median(a, 'c')Functions: mean, median:summary of sample statisticsmean(a, 'r')

mean(a, 'c')

median(a, 'r')

median(a, 'c')

47a=[1:4;5:8;9:12;13:16;17:20] a = 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. a=[1:4;5:8;9:12;13:16;17:20];mean(a)

median(a)

stdev(a)-->mean(a) ans = 10.5 -->median(a) ans = 10.5

-->stdev(a) ans = 5.9160798 Functions: st_deviation:summary of sample statistics

Standard deviation:

a=[1:4;5:8;9:12;13:16;17:20]mean(a)median(a)stdev(a)48Normal distribution

The parameter in this definition is themean. The parameter is its standard deviation; Probability density function

689599.7 rulesummary of sample statisticsFunctions: mean, median, and st_deviation:

If we apply the functions mean, median, and st_deviation without the r qualifier, the functions provide the statistics of the entire matrix:

M = int(100*rand(20,4))mean(M,'r')median(M,'r')st_deviation(M,'r')mean(M)median(M)st_deviation(M)

50

summary of sample statisticsM = Int(100*rand(4,6))mean(M,'c')median(M,'c')st_deviation(M,'c')51ExampleA ready-mix concrete company has three factories (S1, S2, and S3), which must supple three building sites (D1, D2, D3). The costs of transporting a load of concrete from any factory to any site, in some suitable currency, are given by the following cost table:

Suppose the factory manager proposes the following transportation scheme. What is the total cost?

Solution

Home Work-2

Due: 9am, Sep. 14 2015Late policy: 10% off per day To be submitted online to Canvas.HW-2 Exercises (1)

HW-2 Exercises (2)

HW-2 Exercises (3)What will you get by executing the following?

HW-2 Exercises (4)What will you get by executing the following?

HW-2 Exercises (5)What will you get by executing the following?

HW-2 Exercises (6)What will you get by executing the following?

HW-2 Exercises (7)What will you get by executing the following?

HW-2 Exercises (8)What will you get by executing the following?

HW-2 Exercises (9)What will you get by executing the following?

HW-2 Exercises (10)What will you get by executing the following?

HW-2 Exercises (11)Solve the problem

Learn Sort/length/size/sum/prod

HW-2 Exercises (12)find