You are on page 1of 66

AP3114/Computational Methods for

Physicists and Materials Engineers


2. Matrix
Dr. Jun Fan
Sep. 7, 2015

Tentative Class Schedule


WK
1 (Aug. 31,2015)
2 (Sep. 7, 2015)
3 (Sep. 14, 2015)
4 (Sep. 21, 2015)
5 (Sep. 28, 2015)
6 (Oct. 5, 2015)
7 (Oct. 12, 2015)
8 (Oct. 19, 2015)
9 (Oct. 26, 2015)
10 (Nov. 2, 2015)
11 (Nov. 9, 2015)
12 (Nov. 16, 2015)
13 (Nov. 23, 2015)
Revision (Nov. 30, 2015)

Syllabus
Introduction & simple operations
Matrix, Statistical description and analysis of data
Flow control (script files; for/done, if/else)
User-defined functions
Plots & Random Numbers
University Holiday
Bisection method
Practical Test 1 (Lectures 1-4)
Newton-Raphson method
More programming examples
Solving differential equations I
Solving differential equations II
Fourier Analysis
Project Presentation
Practical Test 2 (All lectures)

Assessment Tasks/Activities
Practical 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 day

Recommended Reading
Reference Book(s):
Essential MATLAB for Scientists and Engineers, 2nd/3rd/4 th/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/tutori

als
Introduction to Scilab (introscilab.pdf)
Scilab for very beginners (Scilab_beginners.pdf)

Programmable Calculator
Scilab
www.scilab.org

Download (select a version for your own

computer, Windows/Mac/Linux) & install


http://www.scilab.org/download/5.5.1
A copy for Windows has been upload to

Canvas

Windows

Mac

Linux

Scilab interface

Start -> Scilab

Variable
s
Director
y
&
files

Comma
nd Input
Interfac
e
Comma
nd
history

Practice is the most


important

Ihear, I forget
Isee, I remember
Ido, I learn
7

Help

To get help, type help


> help derivative

Editor
1)
2)
3)
4)

>editor
Type
:

Then, save it as *.sce file, for instant,


myscript.sce
To run: exec(filename.sce), in this
case,
exec(myscript.sce)
In the command box, you will see the
following:

Review of lecture 1
Creating a variable
Comments (//)/continuation(..)
Elementary mathematical functions
Boolean (%T, %F)
Complex numbers
Integer
Strings (test, +)
Dynamic variables

Variable names
Variable names may be as long as the user

wants, but only the first 24 characters are


taken into account in Scilab.
Case sensitive
a to z,
A to Z,
0 to 9,
%, _, #, !, $, ?
Starting with % has special meaning, such

as %pi

Pre-defined mathematical variables


In Scilab, several mathematical variables

are pre-defined, whose name starts with a


percent % character.

Creating real variables

Elementary mathematical operators

Comments and continuation


lines

Comments:

Any line which begins with two slashes


//

Continuation lines:
any line which ends with two dots
..

List of Elementary Mathematical


Functions
Trigonometry

Other functions

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

Complex numbers
In Scilab, %i represents the mathematical
imaginary number i

i 1?
2

is used
to compute
the
conjugate

real
returns real
part
imag
returns
imaginary

Integers
Integer 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

The Scilab circular way allows for a greater


flexibility in the processing of integers, since it
allows to write algorithms with fewer if
statements

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 limit

The 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 variable

Strings
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 1
Creating a variable
Comments (//)/continuation(..)
Elementary mathematical functions
Boolean (%T, %F)
Complex numbers
Integer
Strings (test, +)
Dynamic variables

Create a matrix of real


values
Square brackets [ ] mark the beginning

and the end of the matrix


Commas , separate the values in
different columns
Semicolons ; separate the values of
different rows.

Create a Matrix
Or, 2nd way: blank space separates the

columns, while the new line separates the


rows

Functions generating
matrices

Empty matrix
A=[ ] create a 0X0/empty matrix, this

syntax allows to delete the content of a


matrix, so that the associated memory is
freed.

Query matrices
size returns the two output arguments nr

and nc, which are the number of rows and


the number of columns

Accessing the elements of a


matrix
The whole matrix, with the A syntax
Element by element with the A(i,j) syntax

The colon : operator:


A range of index values with the colon : operator
v=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


operator

s and t are the steps

Hilbert
matrix

The eye matrix


The 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 dynamic


The size of a matrix can grow or reduce

dynamically

The dollar $ operator


The 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 operations
If a dot . is written before an operator, it

is associated with an elementwise operator,


i.e., the operation is performed element-byelement

Elementwise operators

Conjugate transpose and


non-conjugate transpose
real number/matrix => transpose
Complex number/matrix => transpose and

conjugates
Elementary 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 statistics

Function: sum

he sum of all element of the matrix a


-->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 column

The sum of all


element of
each row

summary of sample statistics


Functions: mean, median:
x=[-2,3,5,8,9]

Average value of X
mean(x)=
=4.6

x= [-2,3,5,8,9]
m ean(x)
m edian(x)

Median--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

summary of sample statistics


Functions: mean, median:
Mean values in rows
-->mean(a,'r')
ans =
9. 10. 11. 12.

Median in rows
-->median(a,'r')
ans =
9. 10. 11.

Mean values in columnsm ean(a, 'r')


-->mean(a,'c')
ans =
m ean(a, 'c')
2.5
6.5
m edian(a, 'r')
10.5
14.5
m edian(a, 'c')
18.5

Median in columns
-->median(a,'c')
ans =
2.5
6.5
10.5
14.5
18.5

12.

summary of sample statistics


Functions: st_deviation:
Standard
deviation:

a=[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.
a=[1:4;5:8;9:12;13:16;
17. 18. 19. 20.
17:20];
m ean(a)
m edian(a)

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

Normal distribution

689599.7 rule

robability density function

The parameter in this definition is


themean.
The parameter is its standard
deviation;

summary of sample statistics

Functions: 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:

summary of sample statistics

Example
A 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 2015
Late 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

You might also like