0% found this document useful (0 votes)
41 views32 pages

Scilab

Scilab is an open-source scientific software for numerical computations developed by INRIA and ENPC in France. It is primarily used for matrix computations and includes robust plotting functionality. Key features of Scilab include solving problems in areas like signal processing, data analysis, image processing, numerical optimization, and dynamic system simulation. The document provides examples of how to define matrices and vectors, perform basic arithmetic operations, use relational operators, and implement flow control structures like for loops and if/else conditional statements in Scilab. It also introduces how to define functions in Scilab code ("sci files"), plot functions, and solve systems of nonlinear equations.

Uploaded by

耿鈺庭
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views32 pages

Scilab

Scilab is an open-source scientific software for numerical computations developed by INRIA and ENPC in France. It is primarily used for matrix computations and includes robust plotting functionality. Key features of Scilab include solving problems in areas like signal processing, data analysis, image processing, numerical optimization, and dynamic system simulation. The document provides examples of how to define matrices and vectors, perform basic arithmetic operations, use relational operators, and implement flow control structures like for loops and if/else conditional statements in Scilab. It also introduces how to define functions in Scilab code ("sci files"), plot functions, and solve systems of nonlinear equations.

Uploaded by

耿鈺庭
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Introduction of Scilab

林華愷
What’s Scilab?
Scilab是由INRIA(法國國立計算機及自動化研究院)和ENPC(法國國立橋樑學
院)開發的開放原始碼的科學計算自由軟體。

Scilab一詞來源於英文 Scientific Laboratory(科學實驗室)詞頭的合併。

目前全世界使用Scilab的人數估計有1000,000。Scilab也是以矩陣作為主要的資
料類型,同時擁有豐富的繪圖功能。Scilab能處理包括信號處理、資料分析、
圖像增強、數值最佳化、動態系統仿真等方面的問題。

https://zh.wikipedia.org/wiki/Scilab
Where to get Scilab?
可google搜尋”Scilab”或直接上官網 http://www.scilab.org 下載軟體
Download

http://www.scilab.org/
Where to get Scilab?
可google搜尋”Scilab”或直接上官網 http://www.scilab.org 下載軟體

windows

mac

http://www.scilab.org/download/5.5.2/
Scilab interface

Workspace
Command
Window
Current
Folder Command
History
Scalar arithmetic operations
Symbol Operation MATLAB Form
^ exponentiation: ab a^b

* multiplication: ab a*b

/ right division: a/b = a a/b


b
\ left division: a\b = b a\b
a
+ addition: a + b a+b

- subtraction: a – b a-b

• Example:
-->(5*2+3.5)/5
ans =

2.7 6
http://www.che.ntu.edu.tw/che/?p=2149
Order of precedence
Precedence Operation
First Parentheses, evaluated starting with the innermost pair
Second Exponentiation, evaluated from left to right
Third Multiplication and division with equal precedence (left to right)

Fourth Addition and subtraction with equal precedence (left to right)

• Example:
-->5^2*2
ans =

50.

-->5*2^2
ans =

20. 7
http://www.che.ntu.edu.tw/che/?p=2149
Matrix
• 直接列出矩陣內的元素產生矩陣:
– 用逗號 (,) 或空白區隔同一列的元素
– 用分號 (;) 或 Enter 鍵代表一列的結束
– 矩陣的開始和結束,使用中括號 ([]) 表示

• Example:
-->A=[1 2 3]
A =

1. 2. 3.

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

1. 2. 3.
4. 5. 6. 8
http://www.che.ntu.edu.tw/che/?p=2149
Matrix
• 直接列出矩陣內的元素產生矩陣:
– 或使用下列描述法構成:
X=起始值:增加值:結束值

• Example:
>> A=[1:10]
A=
1 2 3 4 5 6 7 8 9 10

>> A=[1:2:10]
A=
1 3 5 7 9

9
http://www.che.ntu.edu.tw/che/?p=2149
Matrix

Ref: http://www.cs.nthu.edu.tw/~jang第九章:矩陣的處理與運算

10
http://www.che.ntu.edu.tw/che/?p=2149
Matrix
Symbol Operation Form Example
+ Scalar-array addition A+b [6,3]+ 2 =[8, 5]
- Scalar-array subtraction A-b [6,3]- 2 =[9,11]
+ Array addition A+B [6,3]+[3,8] =[9,11]
- Array subtraction A-B [6,3]-[3,8] =[3,-5]
* Matrix multiplication A*B [1,2]*[3,4]' =[11]
\ Matrix left division x=A-1B A\B [1,2]\[10] =[0 5]'
.* Array multiplication A.*B [3,2].*[2,4] =[6, 8]
./ Array right division A./B [4,8]./[2,4] =[2, 2]
.\ Array left division A.\B [2,4].\[4,8] =[2, 2]
.^ Array exponent A.^B [3,5].^2 =[9,25]
2.^[3,5] =[8,32]
[3,2].^[2,3] =[9, 8]

11
http://www.che.ntu.edu.tw/che/?p=2149
Variables
• 第一個字母必需是英文字母。
• 字母間不可留空格。
• 字母大小寫有異
• 最多只能有 24 個字母

12
http://www.che.ntu.edu.tw/che/?p=2149
Relational Operator
Relational Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to

• Example:

13
http://www.che.ntu.edu.tw/che/?p=2149
Example

A = [1:3:10]; B = [2,5,9,11];
C = B-A;
D = (A ~= C)
?????

A = [1:2:5]; B = [2:2:6];
C = A*B’
?????
D = A.*B
?????

14
http://www.che.ntu.edu.tw/che/?p=2149
Flow Control
• 程式的流程控制(Flow Control):
– 迴圈指令(Loop Command):
• for …… end
• while …… end

– 條件指令(Branching Command):
• if … elseif … else … end
• select … case … else … end

15
http://www.che.ntu.edu.tw/che/?p=2149
Flow Control
for 迴圈 while 迴圈
加 ; 使之不輸出

16
http://www.che.ntu.edu.tw/che/?p=2149
Flow Control
if 判斷 select 判斷

17
http://www.che.ntu.edu.tw/che/?p=2149
Example
一雙成份液體混合物,液體分率如下
x1=0.2, x2=0.8

而各成分的純物質蒸氣壓如下
−1211 −1344
𝑜 (6.7+ ) 𝑜 (6.9+ )
𝑃1 = 10 220+𝑇 ; 𝑃2 = 10 219+𝑇

總壓為 𝑃𝑇 = 𝑥1 ∙ 𝑃1 𝑜 + 𝑥2 ∙ 𝑃2 𝑜

求出該物質的沸點 (T=?,PT=760)

Trial and error method

18
Exercise 1

1. 兩個3x3矩陣Ai,j= i+j-1,Bi,j= i-j+1,求出A=?,B=?,C=A+B。


Sci file

• scinote 的特性:
– 是純文字文件(只要延伸檔名維持 .sci)
– 任何 Scilab 指令都可以出現在 sci-file 中

• scinote 的種類:
– Function (函式)
– 用來定義一個自訂函式
– 除了全域變數(global variables),所有執行中使用
或產生的變數皆不存在於 workspace,因此不會
和 Scilab 基本工作空間的變數相互覆蓋
– 可以有輸出與輸入變數
20
Sci file
啟動Scinote

21
Function
[Output1, Output2, …] = CommandName(input1, input2, …)

CommandName: built-in or user-defined function

Example: 解一元二次方程式 𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 =0

儲存 儲存與執行

22
Function

顯示

儲存與執行
Function

程式碼
Plot

[起始:間隔:終點]

[1st函數,2nd函數,3rd函數]

25
Exercise 2
1
2. 利用scinote,寫出兩個函數f x = 𝑒 −𝑥 與g x = ,
2x+1
並畫出f x 與g x 。(x=0~10)
Nonlinear system
‘deff’ define function

deff(‘[s1,s2,…]=newfunction(e1,e2,…)’, text)
函數名稱 變數 函數式子

‘fsolve’ find a zero of a system of n nonlinear equations

xsol=fsolve(x0,function)
解 初始猜值 函數

27
Nonlinear system
解 x  cos(x)

1.令 y  x  cos(x)
deff('y=func(x)','y=x-cos(x)') y  cos(x)
2.給定初始的x猜值

x0=0
yx
3.解 y  x  cos(x)  0 時的x值
xsol=fsolve(x0,func)

x=0.7390851
28
Nonlinear system
一雙成份液體混合物,液體分率如下
x1=0.2, x2=0.8

而各成分的純物質蒸氣壓如下
−1211 −1344
𝑜 (6.7+ ) 𝑜 (6.9+ )
𝑃1 = 10 220+𝑇 ; 𝑃2 = 10 219+𝑇

總壓為 𝑃𝑇 = 𝑥1 ∙ 𝑃1 𝑜 + 𝑥2 ∙ 𝑃2 𝑜

求出該物質的沸點 (T=?,PT=760)

−1211 −1344
(6.7+220+𝑇 ) (6.9+219+𝑇 )
解 760 = 0.2 ∙ 10 + 0.8 ∙ 10

deff('y=func (x)','y(1)=0.2*10^(6.7+-1211/(220+x(1)))+0.8*10^(6.9+-1344/(219+x(1)))-760');
x0=0;
xsol=fsolve(x0,func);
disp(xsol)
Nonlinear system
解 x1  cos( x1)  x 2
x 2  sin( x 2)  x1

1.令 f (1)  x1  cos( x1)  x 2 第一條式子

f (2)  x 2  sin( x 2)  x1 第二條式子

deff('f=func(x)',['f(1)=x(1)-cos(x(1))-x(2)';'f(2)=x(2)-sin(x(2))+x(1)']);

[‘第一條式子’ ; ’第二條式子’]

2.給定初始的x猜值
x0=[0;0];

3.解
xsol=fsolve(x0,func);
disp(xsol) 30
Exercise 3
3. 解 x=y+z, y=2x+y2-2, z=x3-3z
Summary
• 定義變數(英文字母開頭)
• 了解基本代數算式的程式碼
• 開啟sci檔,定義函數
• 善用for, if, while
• 用scilab解非線性方程式
• 程式碼查詢help

help

You might also like