Scilab 簡介 -...

23
Scilab 簡介 2011/09/22 [email protected] or [email protected] Chih-Han Lin 林致翰 Chih-Han Lin [email protected] High-Field Physics and Ultrafast Technology Laboratory

Transcript of Scilab 簡介 -...

Page 1: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Scilab 簡介

2011/09/22

[email protected]

or [email protected]

Chih-Han Lin 林致翰

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

Page 2: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

What is Scilab?

命令列模式

console (command line)

逐行編譯 scinote

與撰寫一般直譯式程式

語言類似

執行

呈現結果

呼叫進階編輯器,撰寫函數,主程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

Page 3: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

命令列模式

直接輸入計算式

按 Enter 開始計算

計算結果

Page 4: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

宣告變數

等號代表把等號右邊(變數或數值)的值存

到等號左邊的變數

disp 的指令代表把某個變數印到螢幕上

指令名(參數) ex. cos(13)

disp(a)

Page 5: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

雙等號(==) 用於判定邏輯真偽

若左式等於右式,輸出 T (ture) 或 1

若左式等於右式,輸出 F (fulse) 或 0

Page 6: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

%加特定變數名稱可調用內建常數

%pi 圓周率

%i 虛數單位(與數字結合時不要

忘了)

這裡的 . 代表小數點

Page 7: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

宣告陣列(矩陣)

變數不一定只能儲存一個值,也可以是一個

有序數組(向量、矩陣等等)

用逗點(,)或空格分開行中元素

用分號(;)代表換列

Page 8: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

:的用法(increment)

起始值:增量:終止值

起始值:終止值 與 起始值:1:終止值 等價

Q1: 假如終止值減去起始值不等於增量的整數倍會如何?

Q2: 增量可以是負值嗎?

Page 9: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

這裡的 : 代表全部的意思

g(:,2)

g

g(:,1) g(:,4)

g

g(1,:)

g(2,:)

g(4,:)

Page 10: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

運算元前加句點(比如 .*, .+, .-, .^)代表陣列元素直接運算

二維陣列變數(矩陣)的基本運算規則與矩陣相同

1*1+2*3 1*2+2*4

3*1+4*3 3*2+4*4

1*1 2*2

3*3 4*4

Page 11: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

Q3: 變數除了數值以外還可以是什麼類型的資料?

Q4: 變數名稱 VARIA 跟 varia 代表同一個變數嗎?

(scilab 是否會區分大小寫?)

Page 12: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

二維畫圖

plot(x1, y1,’-bx’)

數據點橫軸座標

數據點縱軸座標

(假如 x1 是行向量,y1

假如 x1 是行向量,y1 也要是 行向量,同時

兩者的長度要相等)

也要是 行向量

引號內放置繪圖參數,可任意搭配

x : 標記點要叉叉

o : 標記點要圈圈

- : 要畫折線

b : 藍色

r : 紅色

Page 13: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

plot(1:10, 1:10,’-bx’) plot(1:10, 1:10,’ro’)

Page 14: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

plot(1:0.2:8, cos(1:0.2:8),'-ro')

Page 15: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

a=a+1 ?

a=a+1

temp=a

a=temp+1

取出 a 之值,運算後再存回 a

Page 16: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

Scinote

會逐執行程式碼,自動幫你將指令套色,較好辨識

按此鈕執行

Page 17: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

剛剛的按鈕等於是在命令列輸入入這行指令

你會在命令列看到程式輸出結果

(假如你有用 disp, pirinf 之類的指令作輸出)

Page 18: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

for 迴圈

迴圈變數夾在 for 迴圈中的變數會依照

陣列宣告依序帶入

終止迴圈提示字元

想要重複執行的程式碼區塊

這裡用將迴圈變數印到螢幕

上為例

Page 19: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

輸出結果i=1disp(i)

i=2disp(i)

i=3disp(i)

disp(i)

for

end

i=1:3disp(i)

disp(i)

for

end

i=[1,2,3]disp(i)

disp(i)

效果相同

Page 20: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

印出前四個奇數

for

end

i=[1, 3, 5, 7]disp(i)

for

end

i=1:3disp(2*i+1)

for

end

i=1:2:7disp(i)

disp(1)disp(3)disp(5)disp(7)

Page 21: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

實習內容

http://home.hit.no/~finnh/scilab_scicos/

快速瀏覽 Finn Haugen 所撰寫之網頁版簡易指引

參考助教的講義

回答這份講義提到的幾個問題

Page 22: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

用命令列一步一步作

用 scinote 寫成完整的程式

應該是 a=0.9

Page 23: Scilab 簡介 - ncuphyexp1009.weebly.comncuphyexp1009.weebly.com/uploads/8/4/4/0/8440048/week2.pdf · What is Scilab? 命令列模式 console (command line) 逐行編譯 scinote 與撰寫一般直譯式程式

Chih-Han [email protected] High-Field Physics and Ultrafast Technology Laboratory

讓助教使用你寫的程式畫出類似這樣的圖就可以了