SCILAB Programming
Simple Calculation
->1+1
ans = 2.
-->x = 5; -->X = 50;
-->y = 6; -->z = 8 z = 8.
-->m = x*y*z m = 240.
SCILAB is case sensitive.
Vector and Matrix
->a = [1 2 3]
a = 1. 2. 3.
->b = [1; 2; 3]
b = 1.
2.
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
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.
--> 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
-->x = [1 2; 3 4]
-->x*x ans = 7. 10.
15. 22.
-->x.*x ans =
Statistic Commands
-->a=[1 2];
-->sum(a) ans = 3
-->mean(a) ans = 1.5 -->stdev(a)
ans = 0.7071068 -->variance(a)
ans = 0.5
Graph Plotting
x = [1 2 -1 3 7];
clf;
plot(x);
Graph Plotting
--> x = [1 2 3 4 5 6];
--> y = x.^2;
y = 1. 4. 9. 16. 25. 36.
--> clf;
--> plot(x,y)
“.” is for each element’
3-D Graph Plotting
x = [1 2];
y = [1 2];
z = [3 5; 4 6];
clf;
plot3d(x,y,z);
3.0 3.5 4.0 4.5 5.0 5.5 6.0
Z
1.2 1.0 1.6 1.4
2.0 1.8
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
x = 1; // I want to set x equal to 1.
Loop Command: For (1)
for index = start : end do something
end
//display 1, 2, .. 10 for i = 1 : 10
//1 + 2 + 3 .. 99 = ? s = 0;
for i = 1 : 99
Loop Command: For (2)
for index = start : increment : end do something
end
//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); 0 100 200 300 400 500 600 700 800 900 1000
0.75 0.80 0.85 0.90 0.95 1.00
Utility Plot
clear;
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);
0 20 40 60 80 100
Z
1 4 7
10 X
1 2 3 4 5 6 7 8 9 10
Y
Conditional Command: if then
if logical-statement then do something
end
x = 0;
if x < 1 then x = 1;
Conditional Command: if then else
if logical-statement then do something
else
do another thing end
x = 3;
if x < 1 then x = 1;
else
x = 2;
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
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);
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
function r=consumption(y, a, b) r = a + b*y;
endfunction
Variable inside and outside function.
Function can see the value of variable outside it but cannot change its value
function r=test() r = y + 10;
y = 8;
endfunction y = 5;
test() --> 15 y
Conditional Loop: While
while conditional_statement do_something
end
i = 1;
while i < 10
Conditional Loop: While
//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
for i=1:10 disp(i);
if i==3 then break;
end end
Scilab Help
• Scilab help can be accessed by [F1].