• Tidak ada hasil yang ditemukan

SCILAB Programming

N/A
N/A
Protected

Academic year: 2024

Membagikan "SCILAB Programming"

Copied!
24
0
0

Teks penuh

(1)

SCILAB Programming

(2)

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.

(3)

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

(4)

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.

(5)

Two Ways of Matrix Multiplication

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

-->x*x ans = 7. 10.

15. 22.

-->x.*x ans =

(6)

Statistic Commands

-->a=[1 2];

-->sum(a) ans = 3

-->mean(a) ans = 1.5 -->stdev(a)

ans = 0.7071068 -->variance(a)

ans = 0.5

(7)

Graph Plotting

x = [1 2 -1 3 7];

clf;

plot(x);

(8)

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’

(9)

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

(10)

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.

(11)

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

(12)

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

(13)

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

(14)

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

(15)

Conditional Command: if then

if logical-statement then do something

end

x = 0;

if x < 1 then x = 1;

(16)

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;

(17)

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

(18)

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

(19)

Your own function

function r=functionname(input1, input2, …) calculating r

endfunction

function r=consumption(y, a, b) r = a + b*y;

endfunction

(20)

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

(21)

Conditional Loop: While

while conditional_statement do_something

end

i = 1;

while i < 10

(22)

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

(23)

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

(24)

Scilab Help

• Scilab help can be accessed by [F1].

Gambar

Graph Plotting
Graph Plotting

Referensi

Dokumen terkait

A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope

So, from the interpretation above the researcher can conclude that the implementation of classroom action research by using inside outside circle technique can improve

Karakterisitik arsitektur Venturi seperti prinsip both-and, double function, ambiguity, inside outside, kemudian digunakan dalam menemukan hasil perancangan berupa

IMPORTANT: If you give each value in a program a meaningful, descriptive variable name, it will help you think more clearly about the problem you are solving and it will help you

results achievement of learning activities that have been done inside and outside the classroom. In addition, teacher work performance can be reviewed from the ability

If you may have noticed, in Python, the variable declared outside of the methods, but inside of the class right under the class header, is by default a class variable,

This will facilitate portability of code between Java and C++ (you can cut and paste and do minimum changes to code). The code from Java's function body can be copied into C++

learning outside the classroom can help students make objects more lively and attractive to students and increase understanding.47 Based on some of the opinions above, it can be