You are on page 1of 41

AP3114/Computational Methods for

Physicists and Materials Engineers


3. Basic Programming
Dr. Jun Fan
Jan. 26, 2015

Review of Lecture 2
Matrix

Functions to generate matrix (eye, ones, zeros, etc)


Empty matrix []
Query matrices size
Accessing elements of matrix A(i,j)
a:b:c
$: the end of the matrix
+,-*,/,\
XB=A

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

real number/matrix => transpose


Complex number/matrix =>
Elementwise operations: .
transpose and conjugates
Conjugate transpose:
Elementary single quote . only
Sum, mean, median, st_deviation
transpose, no conjugating the
matrix, be it real or complex.

Example
Suppose we have invested some money

that draws 10% interests per year,


compounded. We want to know how long it
will take for the investment to double. More
specifically, we want a statement of the
account each year until the balance has
doubled.

Outline
Looping and branching statements
if
select/case
for
while
break
Continue
return

Function
User-defined functions
User-defined library
Debugging: pause, abort, resume
4

Order of commands
Sequence: one by one consecutively
Command 1

Command 2

Command 3
5

Branching: if
Selection/Decision Among Multiple Cases

Commands
Conditi
on?

True

True Block

Commands
6

Branching: if
Selection/Decision Among Multiple Cases

Commands
False

False Block

Commands

Conditi
on?

True

True Block

Branching: select/case
Multiple

possibilitie
s
Test
expression

Case 1

Case 2

Case 3

Command

Case 4

The select/case statement

10

Flowchart
Command 1

Command 2

Command 3

11

if
False

Conditi
on?

False Block

select
case
Case 1

True

True Block

Test
expression

Case 2

Case 3

Command

Case 4

The for statement


The for statement allows to perform loops

12

The for statement

13

The while statement


while: allows to perform a loop while a

boolean expression is true

Alternativ
ely
14

for

while

Given a number
of repetitions

Condition is True

Block 1

Block 1

Conditi
on?

False
Block 2
15

Block 2

The break statement


The break statement allows to interrupt a

loop

16

The continue statement


The continue statement allows to go on to

the next loop, so that the statements in the


body of the loop are not executed this time.
For instance, we want to calculate the sum
of odd number smaller than 10:
1+3+5+7+9

17

Example
Suppose we have invested some money

that draws 10% interests per year,


compounded. We want to know how long it
will take for the investment to double. More
specifically, we want a statement of the
account each year until the balance has
doubled.

18

Example

19

Functions

y=f(x);
internal function y=sin(x)

20

Functions (2)

21

Functions (3)

How about the following?


>>clear

>>z=myfunction(9)
22

Functions(4)
Multiple output arguments

When there is no output argument, the

value of the first output will be store in the


ans variable (different from Matlab)

23

Scilab/MATLAB:
function
f=simplef(x1,x2)
f(1)=2*x1;
f(2)=3*x2;
endfunctioin/end
>simplef(1,2)
>ans= 2 6

Functions(5)
A function

can call
another
function

24

Functions(6)return
statement
return statement allow to immediately

return, i.e., quits the current function.


Suppose we want to calculate the sum of
integers from istart to iend only if
istart<=iend, otherwise, the output is 0.

25

Function Libraries
A function library is a collection of functions

defined in the Scilab language and stored


in a set of files
Like C/C++/Fortran, compile the code, you

get a binary file which can execute


You can generate your own Scilab function
library similarly
Procedure
Create *.sci file/scripts containing the

26

function (*.c/*.f90 file)


Use genlib to generate binary version of
the script (compile)

Example-genlib

Library
name
27

Directory containing
*.sci files

Exampleload library
Location: Operation System
dependent

The startup file is *.scilab or scilab.ini


Add the following lines to this file (or create

scilab.ini file), such that our library


available at startup

28

Home Work-3
Due: 9am, Sep 21, 2015
Late policy: 10% off per day
To be submitted online to Canvas.
Both answers and your codes (*.sec files)
are required to submit; Do use // to add
comments clearly which question you are
referring to. when we execute your codes,
answers shall be shown on the screen.

HW3 - Exercise (1)


What will you get by executing the

following?

30

HW3 - Exercise (2)


What will you get by executing the

following?

31

HW3 - Exercise (3)


What will you get by executing the

following?

32

HW3 - Exercise(4)
What will you get by executing the

following?

33

HW3 - Exercise(5)
What will you get by executing the

following?

34

HW3 - Exercise(6)
What will you get by executing the

following?

35

HW3 - Exercise(7)
What will you get by executing the

following?

36

HW3 - Exercise (8)


Use forend

Use whileend

37

HW3 - Exercise(9)
Write some Scilab statement on the

command line that use logical vectors to


count how many elements of a vector x are
negative, zero, or positive. Check that they
work, for example, with the vector
[-4 0 5 -3 0 3 7 -1 6]

38

HW3 - Exercise(10)
The Internal Revenue Service (US) decides

to use the following table to determine tax.


Write a Scilab statement to calculate the
tax, and test out on the following incomes:
$5000, $10000, $15000, $22000, $30000,
$38000, and $50000.

39

HW3 - Exercise(11)
The electricity accounts of residents in a very small rural

community are calculated as follows:


If 500 or fewer units are used, the cost is 2 cents per unit
If more than 500, but not more than 1000, units are used,

the cost is $10 for the first 500 units and 5 cents for every
units in excess of 500
If more than 1000 units are used, the cost is $35 for the first
1000 units plus 10 cents for every units in excess of 1000
A basic service fee of $5 is charged no matter how much
electricity is used.
Five resides use the following units of electricity in a

certain month: 200, 500, 700, 1000, and 1500. Write a


program that uses logical vectors to calculate how much
they must pay.
40

HW3 - Exercise(12)

(help
grand)

41

You might also like