12
1 การใช้งานโปรแกรม MATLAB เบื้องต ้น ปรวัฒน์ วิสูตรศักดิ 1. ทดลองใช้คาสั ่งบวกเลขสองจานวนดังนี้ » 3 + 4 ans = 7 โอเปอเรชั่นทางคณิตศาสตร์อื่นๆ ที่ใช้ในการคานวณมีดังนี - บวก สัญลักษณ์ + ตัวอย่าง 5+3 - ลบ สัญลักษณ์ - ตัวอย่าง 5-3 - คูณ สัญลักษณ์ * ตัวอย่าง 5*3 - หาร สัญลักษณ์ / ตัวอย่าง 5/3 - ยกกาลัง สัญลักษณ์ ^ ตัวอย่าง 5^3 (หมายถึง 5 3 = 125) Current directory window เป็นหน้าต่างที่ใช้ แสดงไฟล์ที่กาลังเปิดใช้งานอย่ใน directory ปัจจุบัน คาสั่งต่างๆ จะถูกป้ อนหลัง เครื่องหมาย MATLAB cursor Command window เป็นหน้าต่างที่ใช้ สาหรับกาหนดตัวแปร ป้ อนคาสั่ง และรัน โปรแกรม Command history window เป็นหน้าต่างที่ใช้ แสดงคาสั่งที่เคยถูกป้ อนไปทั ้งหมดในอดีต

MATLAB Tutorial 496

Embed Size (px)

Citation preview

Page 1: MATLAB Tutorial 496

1

การใช้งานโปรแกรม MATLAB เบือ้งต้น

ปรวฒัน์ วิสตูรศกัดิ ์

1. ทดลองใช้ค าสั่งบวกเลขสองจ านวนดังนี ้

» 3 + 4

ans =

7

โอเปอเรชัน่ทางคณิตศาสตร์อื่นๆ ท่ีใช้ในการค านวณมีดงันี ้

- บวก สญัลกัษณ์ + ตวัอยา่ง 5+3 - ลบ สญัลกัษณ์ - ตวัอยา่ง 5-3 - คณู สญัลกัษณ์ * ตวัอยา่ง 5*3 - หาร สญัลกัษณ์ / ตวัอยา่ง 5/3 - ยกก าลงั สญัลกัษณ์ ^ ตวัอยา่ง 5^3 (หมายถงึ 53 = 125)

Current directory window เป็นหน้าตา่งที่ใช้

แสดงไฟล์ที่ก าลงัเปิดใช้งานอยใ่น directory

ปัจจบุนั

ค าสัง่ตา่งๆ จะถกูป้อนหลงั

เคร่ืองหมาย MATLAB cursor

Command window เป็นหน้าตา่งที่ใช้

ส าหรับก าหนดตวัแปร ป้อนค าสัง่ และรัน

โปรแกรม

Command history window เป็นหน้าตา่งที่ใช้

แสดงค าสัง่ที่เคยถกูป้อนไปทัง้หมดในอดีต

Page 2: MATLAB Tutorial 496

2

2. Math build-in function

3. การก าหนดค่าให้กับตวัแปรใช้เคร่ืองหมาย = โดยมีรูปแบบทั่วไปดังนี ้

ตวัอยา่ง » x = [1 2 3 4 5]

x =

1 2 3 4 5

» y = [6;7;8;9;10]

y =

6

7

8

9

10

» y = [6,7,8,9,10]

y =

6 7 8 9 10

» y' (Transpose vector)

ans =

6

ช่ือตวัแปร = ตวัเลข หรือนิพจน์ทางคณิตศาสตร์

Page 3: MATLAB Tutorial 496

3

7

8

9

10

» whos

Name Size Elements Bytes Density Complex

ans 5 by 1 5 40 Full No

x 1 by 5 5 40 Full No

y 1 by 5 5 40 Full No

Grand total is 15 elements using 120 bytes

» x = 15

x =

15

» x = 3*x-12

x =

33

» a = 12

a =

12

» B = 4; (ถ้าใสเ่คร่ืองหมาย ; ปิดท้ายค าสัง่ โปรแกรมจะไมแ่สดงคา่ที่ป้อนให้กบัตวัแปรออกมาทางหน้าจอ) » C = (a-B)+40-a/B*10

C =

18

» A = [1 2 3; 3 4 5; 6 7 8]

A =

1 2 3

3 4 5

6 7 8

» A = [

1 2 3

3 4 5

6 7 8]

A =

1 2 3

3 4 5

6 7 8

» B = [

-1 3 10

-9 5 25

0 14 2]

B =

-1 3 10

-9 5 25

0 14 2

» A-1

ans =

Page 4: MATLAB Tutorial 496

4

0 1 2

2 3 4

5 6 7

» A+B

ans =

0 5 13

-6 9 30

6 21 10

4. Vector functions

max largest component min smallest component length length of a vector sort sort in ascending order sum sum of elements prod product of elements

median median value

mean mean value std standard deviation

ตวัอยา่ง

» z = [0.9347,0.3835,0.5194,0.8310]

z =

0.9347 0.3835 0.5194 0.8310

» max(z)

ans =

0.9347

» min(z)

ans =

0.3835

» sort(z)

ans =

0.3835 0.5194 0.8310 0.9347

» sum(z)

ans =

2.6686

» mean(z)

ans =

0.6671

Page 5: MATLAB Tutorial 496

5

5. Matrix functions

» A = [9,7,0;0,8,6;7,1,-6]

A =

9 7 0

0 8 6

7 1 -6

» size(A)

ans =

3 3

» det(A)

ans =

-192

(Since the determinant is not zero, the matrix is invertible.)

» inv(A)

ans =

0.2812 -0.2187 -0.2187

-0.2187 0.2812 0.2812

0.2917 -0.2083 -0.3750

(We can check our result by verifying that AA-1 = I and A-1

A = I .)

» A*inv(A)

ans =

1.0000 0.0000 0.0000

0.0000 1.0000 0.0000

0.0000 0.0000 1.0000

» inv(A)*A

ans =

1.0000 0.0000 0

0.0000 1.0000 0

0.0000 0 1.0000

6. การเขียน Script file

ที่ command line พิมพ์ edit จะปรากฏหน้าตา่ง text editor ของโปรแกรม MATLAB » edit

หลงัจากนัน้ทดลองพิมพ์ ค าสัง่ดงัตอ่ไปนี ้% ‘Pythagoras’ Theorem for Me

a = input (‘Please input the first side’); b = input (‘Please input the second side’);

c = sqrt (a^2 + b^2)

หาก save file นีด้้วยช่ือ pyta.m เมื่อเรียกใช้ >> pyta

Please input the first side 3

Please input the second side 4

c =

5

Page 6: MATLAB Tutorial 496

6

>> help pyta

Pythagoras' Theorem for Me

7. การเขียน Function file

function c = pyta (a, b)

% Pythagoras’ Theorem for Me

% format is c=pyta(a,b)

% where a and b are sides of the rectangle

% and c^2 =a^2+b^2

c = sqrt(a^2 + b^2)

และเมื่อ Save file ต้องใช้ช่ือ pyta.m ในกรณีเรียกใช้ >> c = pyta(3, 4)

c =

5

หรือ >> Pyta (3,4) ans =

5

หรือ >> x= Pyta (4, 3) x =

5

และถ้าใช้ >> help pyta

จะได้ ‘Pythagoras’ Theorem for Me

format is c=pyta(a,b)

where a and b are sides of the rectangle

and c^2 =a^2+b^2

8. ตัวอย่างการใช้ฟังก์ชั่น disp() แสดงผลลัพธ์ของการค านวณ

เม่ือป้อนคา่ input ครบแล้ว ผลลพัธ์ที่จะแสดงบนหน้าจอคือ

‘The average of points scored in a game is:

77

Page 7: MATLAB Tutorial 496

7

9. ตัวอย่างการใช้ฟังก์ชั่น fprintf() แสดงผลลัพธ์ของการค านวณ

10. การ Plot กราฟ 2 มิต ิ

>> x=[1 2 3 5 7 7.5 8 10];

>> y=[2 6.5 7 7 5.5 4 6 8];

>> plot(x,y)

>>

ผลลพัธ์ที่จะแสดงบนหน้าจอคือ ‘A projectile shot at 30.00 degrees with a velocity of 1548.00

km/h will travel a distance of 16.3229 km.’

Page 8: MATLAB Tutorial 496

8

11. การ Plot กราฟ แบบก าหนดเอง ตวัอยา่ง การ plot กราฟเพื่อแสดงยอดขายในแตล่ะปี ดงัตารางด้านลา่ง

ปี 2001 2002 2003 2004 2005 2006 2007 ยอดขาย (ล้านบาท)

8 12 20 22 18 24 27

>> yr=[2001:1:2007];

>> sale=[8 12 20 22 18 24 27];

>> plot(yr,sale,’-r*’,’linewidth’,2,’markersize’,12)

>>

กราฟแบบเส้นประ สแีดง marker เป็น *

สีอ่ืนๆ เชน่ g-green, b-blue, m-

magenta เป็นต้น สว่น marker แบบอ่ืนๆ

เชน่ +, s-square, d-diamond, p-five

points star เป็นต้น

ก าหนดขนาดของเส้น = 2 และ marker =12

Page 9: MATLAB Tutorial 496

9

12. การ Plot กราฟจากฟังก์ชั่น

13. การ Plot กราฟ 3 มิติ

ทดลองป้อนค าสัง่ตอ่ไปนีบ้น command line

>> [x,y] = meshgrid(-3:.1:3,-3:.1:3);

>> z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...

- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...

- 1/3*exp(-(x+1).^2 - y.^2);

>> surf(z)

>> xlabel('x')

>> ylabel('y')

Page 10: MATLAB Tutorial 496

10

>> zlabel('z')

>> title('Peaks')

14. การโปรแกรมบน MATLAB และการใช้ค าสั่งควบคุม

For-loop

>> for j=1:4

j+2

end

j =

3

j =

4

j =

5

j =

6

>> x = 1:10

x =

1 2 3 4 5 6 7 8 9 10

>> for i=1:10

x2(i) = x(i)^2;

end

>> x2

x2 =

1 4 9 16 25 36 49 64 81 100

>> A = [1,5,-3;2,4,0;-1,6,9]

A =

1 5 -3

2 4 0

-1 6 9

>> for i=1:3

ระบคุา่เร่ิมต้น และคา่สดุท้าย นอกจากนีย้งัสามารถระบคุา่

เร่ิมต้น คา่สดุท้าย และก าหนดการเพิ่มขึน้ในแตล่ะรอบได้ดงันี ้

for j=1:2:10 เร่ิมจาก 1 ถงึ 10 โดยแตล่ะรอบ

เพิ่มขึน้ครัง้ละ 2

Page 11: MATLAB Tutorial 496

11

for j=1:3

A2(i,j) = A(i,j)^2;

end

end

>> A2

A2 =

1 25 9

4 16 0

1 36 81

While-loop

function [a] = exple(n)

% [a] = exple(n)

%

a = 0;

while 2^a < n

a = a + 1;

end

% End of function

>> a = exple(4)

a =

2

การท างานใน while จะต้องตรวจสอบเง่ือนไขก่อนทกุครัง้ ซึง่โอเปอเรเตอร์ทีใ่ช้ในการตรวจสอบเง่ือนไข มีดงันี ้

< less than > greater than <= less than or equal >= greater than or equal == equal ~= not equal & and | or ~ not

If statement

จงเขยีนโปรแกรมเพื่อค านวณคา่แรงของพนกังาน จ านวนชัว่โมงปรกติคือ 40 ชม. ถ้าท างานมากกวา่นัน้ให้คดิ OT โดยให้

คา่แรงตอ่ ชม. เพิ่มขึน้เป็น 50 %

Page 12: MATLAB Tutorial 496

12

>> workerpay

Please enter the number of hours worked 35

Please enter the wage in $ 8

The worker’s pay is $280.00>>

รูปแบบทัว่ไปคือ

if เง่ือนไข ชดุค าสัง่ end

นอกจากนีย้งัมี if-elseif-else ซึง่มีรูปแบบดงันี ้if เง่ือนไขที ่1 ชดุค าสัง่ที ่1 elseif เง่ือนไขที ่2 ชดุค าสัง่ที ่2 elseif เง่ือนไขที ่3 ชดุค าสัง่ที ่3 ................ else เง่ือนไขที ่n ชดุค าสัง่ที ่n end