scilab programming - chulapioneer.netserv.chula.ac.th/~ptanapo1/macrophd/2scilab.pdf · statistic...

24
SCILAB Programming

Upload: others

Post on 24-Mar-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

SCILAB Programming

Simple Calculation

->1+1

ans = 2.

-->x = 5;

-->X = 50;

-->y = 6;

-->z = 8

z = 8.

-->m = x*y*z

m = 240.

2

SCILAB is case sensitive.

Vector and Matrix

->a = [1 2 3]

a = 1. 2. 3.

->b = [1; 2; 3]

b =

1.

2.

3.

-->c = [1 2; 3 4]

c = 1. 2.

3 4.

3

--> x=[1 2;3 4];

-->y=[5 6;7 8];

-->x+y

ans =

6. 8.

10. 12.-->c = [1 2; 3 4];

--> inv(c)

ans = - 2. 1

1.5 - 0.5

Matrix: Referring to Rows, Columns,

Elements-->c = [1 2; 3 4]

c = 1. 2.

3. 4.

-->c(1,2)

ans = 2.

--> c(2,2) = 8;

--> c

c = 1. 2.

3. 8.

4

--> c(1,:)

ans = 1. 2.

--> c(:,1)

ans = 1. 3.

--> c = [10 9 8 7 6 5; 1 2

3 4 5 6];

--> c(1,2:4)

ans =

9. 8. 7.

Two Ways of Matrix Multiplication

5

-->x = [1 2; 3 4]

-->x*x

ans =

7. 10.

15. 22.

-->x.*x

ans =

1. 4.

9. 16.

Statistic Commands

-->a=[1 2];

-->sum(a)

ans = 3

-->mean(a)

ans = 1.5

-->stdev(a)

ans = 0.7071068

-->variance(a)

ans = 0.5

6

Graph Plotting

x = [1 2 -1 3 7];

clf;

plot(x);

7

Graph Plotting

--> x = [1 2 3 4 5 6];

--> y = x.^2; y = 1. 4. 9. 16. 25. 36.

--> clf;

--> plot(x,y)

8

“.” is for each element’

3-D Graph Plotting

x = [1 2];

y = [1 2];

z = [3 5; 4 6];

clf;

plot3d(x,y,z);

9

3.0

3.5

4.0

4.5

5.0

5.5

6.0

Z

1.01.2

1.41.61.8

2.0

X

1.0 1.2 1.4 1.6 1.8 2.0

Y

Clear and Comment

--> x = 1;

--> x

x=1.

-->clear;

-->x

!--error 4

undefined variable : x

10

x = 1; // I want to set x equal

to 1.

Loop Command: For (1)

for index = start : end

do something

end

11

//display 1, 2, .. 10

for i = 1 : 10

disp(i);

end

//1 + 2 + 3 .. 99 = ?

s = 0;

for i = 1 : 99

s = s + i;

end

Loop Command: For (2)

for index = start : increment : end

do something

end

12

//1 + 3 + 5.. 99 = ?

s = 0;

for i = 1 : 2 : 99

s = s + i;

end

Solow Model’s Simulation

clear;

alpha=0.5; s=0.1; d=0.1;

k(1)=0.6;

for t=1:1000

y(t) = k(t)^alpha;

i(t) = s*y(t);

c(t) = (1-s)*y(t);

k(t+1) = (1-d)*k(t) + i(t);

end

clf;

plot(y);

13

0 100 200 300 400 500 600 700 800 900 1000

0.75

0.80

0.85

0.90

0.95

1.00

Utility Plotclear;

for i=1:10

x(i)=i;

y(i)=i;

end

for i = 1:10

for j = 1:10

z(i,j) = x(i)*y(j);

w(i,j) = 40;

end

end

clf;

plot3d(x,y,z);

plot3d(x,y,w);

14

0

20

40

60

80

100

Z

1

4

7

10 X

1 2 3 4 5 6 7 8 9 10Y

Conditional Command: if then

if logical-statement then

do something

end

15

x = 0;

if x < 1 then

x = 1;

end

Conditional Command: if then else

if logical-statement then

do something

else

do another thing

end

16

x = 3;

if x < 1 then

x = 1;

else

x = 2;

end

Logical Comparison

• Is x equal y? x == y

• Is x more than y? x > y

• Is x more than or equal y? x >= y

• Is x > 10 and < 100? x > 10 & x < 100

• Is x > 10 or < 100? x > 10 | x < 100

17

Solow Model with technology shock

clear;

alpha=0.5; s=0.1; d=0.1; k(1)=0.1; a = 1;

for t=1:1:1000

if t == 500 then

a = 1.1;

end;

y(t) = a*k(t)^alpha;

i(t) = s*y(t);

c(t) = (1-s)*y(t);

k(t+1) = (1-d)*k(t) + i(t);

end

plot(y);

18

0 100 200 300 400 500 600 700 800 900 1000

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1.0

1.1

1.2

1.3

Your own function

function r=functionname(input1, input2, …)

calculating r

endfunction

19

function r=consumption(y, a, b)

r = a + b*y;

endfunction

consumption(1, 2, 3)

Variable inside and outside function.

Function can see the value of variable

outside it but cannot change its value

20

function r=test()

r = y + 10;y = 8;

endfunctiony = 5;

test()

--> 15

y

--> 5

Conditional Loop: While

21

while conditional_statement

do_something

end

i = 1;

while i < 10

disp(i);

i = i+1;

end

Conditional Loop: While

22

//Find the maximum x such that 1+2+3+4+…+x < 2000

s = 0;

i=0;

while s < 2000

i=i+1;

s = s+i;

end

i-1

Conditional Exit from Loop: Break

i = 1;

while i < 10

disp(i);

if i==3 then

break;

end

i = i+1;

end

23

for i=1:10

disp(i);

if i==3 then

break;

end

end

Scilab Help

• Scilab help can be accessed by [F1].

24