1
ENE209 MATLAB TUTORIAL
Chapter 1 เริ่มต้น การใช้ MATLAB (Starting MATLAB)
Current Directory Window : จะแสดง
ไฟล์ที่เปิดใช้อยูใน directory ปัจจุบัน ่
Command window : เป็นหน้าต่าง
หลัก เพื่อใช้ในการ กาหนดตัวแปรต่างๆ และ รันโปรแกรม
Command History Window : ใช้เก็บ
ประวัติคาสังที่ใช้ใน command window ่
- คาสั่ง clc
พิมพ์คาสั่ง clc ที่ command window แล้ว กด enter ใช้ในการ ลบหน้าจอ ใน command window ARITHMATIC OPERATIONS WITH SCALAR โอเปอเรชั่นที่ใช้ในการคานวณมีดังต่อไปนี้ - บวก สัญลักษณ์ + ตัวอย่าง 5+3 - ลบ สัญลักษณ์ - ตัวอย่าง 5-3 - คูณ สัญลักษณ์ * ตัวอย่าง 5*3 - หาร (ขวา) สัญลักษณ์ / ตัวอย่าง 5/3 - หาร (ซ้าย) สัญลักษณ์ \ ตัวอย่าง 5\3 = 3/5 - ยกกาลัง สัญลักษณ์ ^ ตัวอย่าง 5^3 (หมายถึง 53 = 125) ลาดับในการคานวณต่างไๆใน MATLAB จะเหมือนกับการคานวณ ในเครื่องคิดเลขทั่วไปคือ อันดับที่ 1 วงเล็บ จะเริ่มคานวณ จะข้างในสุด
2
อันดันที่ 2 การยกกาลัง อันดับที่ 3 การคูณ และการหาร อันดับที่ 4 การบวก และการลบ 1.1 Using MATLAB as a Calculator วิธีง่ายทีสุด ในการใช้ MATLAB เพื่อการคานวณต่างๆ โดยการ คีย์ ตัวเลขที่ต้องการคานวณ ลงใน command window หลังจากที่พิมพ์พจน์ของการคานวณเสร็จ กด enter ผลของการคานวณจะปรากฎ หลังคาว่า ans =
Tutorial 1-1 Using MATLAB as a calculator
1.2 ELEMENTARY MATH BUILT-IN FUNCTION ใน MATLAB มี built-in function ให้ใช้ มากมาย โดยรูปแบบในการใช้ คือ ตัว function(x) โดย x อาจจะเป็นตัวเลขหรือ ตัวแปร หรือ พจน์ของตัวเลขก็ได้
3
Tutorial 1-2: Elementary math function
1.3 The Assignment Operator ใน MATLAB เครื่องหมาย = เป็นเครื่องหมายที่ใช้ในการ ประกาศค่าตัวแปร หรือ กาหนดค่า ตัวแปรนั่นเอง รูปแบบคือ
Variable_name = ตัวเลข หรือ พจน์ ของสมการ
Tutorial 1-3 : Assignment Operator
4
ใน MATLAB เมื่อ มีการกาหนดตัวแปรมากๆ อาจทาให้เกิดการสับสน เพราะ MATLAB จะจาค่าของตัวแปรนั้นตัวล่าสุดทีกาหนด เพราะฉะนั้นจะมีคาสั่งที่ใช้ในการดูว่า ตัวแปรไหนถูกกานดไปแล้วบ้าง หรือ ต้องการล้างค่าตัวแปรทั้งหมด
Sample Problem 1-1 : Trigonometric identity
2 จาก cos
x tan x sin x 2 2 tan x
5
ให้พิสูจน์ว่า สมการนี้เป็นจริง โดย x
Solution
5
Sample Problem 1-2 : Geometry and trigonometry
Four circle are placed, as shown in the figure. At each point that two circles are in contact they are tangent to each other. Determine the distance between the centers C2 and C4. The radii of the circle are : R1 = 16 mm, R2 = 6.5 mm, R3 = 12 mm, and R4 = 9.5 mm.
C2 C3
C1
C4
Solution
The line that connect the centers of the circle create four triangle. In two of the triangles, C1C2C3 and C1C3C4 the length of all the sides are known. This information is used to calculate the angles 1 and 2 in these triangles by using the law of cosine. For example, 1 is calculated from:
(C2C3 ) 2 (C1C2 ) 2 (C1C3 ) 2 2(C1C2 )(C1C3 ) cos 1
C2 C3
Next, the length of the side C2C4 is calculated by considering the triangle C1C2C4 . This is done, again, by using the law of cosines ( the lengths C1C2 and C1C4 are known and and the angle 3 is the sum of the angles 1 and 2 ).
C1
1 2 3
C4
6
Chapter 2 การสร้าง Arrays (Creating Arrays)
2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR) การกาหนด vector จากตัวเลขที่รู้ค่าแน่นอน Vector จะถูกสร้างขึ้นมาโดยการพิมพ์ ตัวเลขเข้าไปใน วงเล็บสี่เหลี่ยม (square brackets) [ ]
Variable_name = [Array ของตัวเลข]
Tutorial 2-1: Creating vectors from given data
การกาหนด vector โดยการกาหนดระยะห่างของค่าที่คงที่ ซึ่งจะต้องกาหนดค่าเริ่มต้น และ และค่าสุดท้ายที่ต้องการ
7
Variable_name = [m:q:n]
หรือ
Variable_name = m:q:n
2.2 CREATING A TWO-DIMENSIONAL ARRAY (MATRIX) รูปแบบในการสร้าง matrix คือ
Variable_name = [1st row elements; 2nd row elements; 3rd row elements; …..; last row elements]
Tutorial 2-2: Creating matrices
8
- Transpose operator
9
2.3 BUILT-IN FUNCTION FOR HANDLING ARRAYS Tutorial 2.3: Built-in function for handling arrays
10
Sample Problem 2-1 : Matrix manipulation
Given are a 5x6 matrix A, a 3x6 matrix B, and a 9 element long vector v.
11
2 3 A 4 5 6
11 14 17 6 9 12 15 18 7 10 13 16 19 8 11 14 17 20 9 12 15 18 21 5 8
5 10 15 20 25 30 B 30 35 40 45 50 55 55 60 65 70 75 80
v 99 98 97 96 94 93 92 91
Create the matrix array in command window
Solution
Chapter3
12
Operation with Arrays
3.1 ADDITION AND SUBTRACTION Tutorial 3.1: Addition and Subtraction
3.2 MULTIPLICATION OF ARRAYS Tutorial 3.2: Multiplication of arrays
Sample Problem 3-1: Solving three linear equations (array division)
13
ให้หาคาตอบของสมการเชิงเส้นดังต่อไปนี้
4x 2 y 6z 8 2x 8 y 2z 4 6 x 10 y 3z 0
Solution
ใช้ matrix ในการแก้สมการเชิงเส้น จาก AX = B เขียนในรูป matrix ได้เป็น
4 2 6 x 8 2 8 2 y 4 6 10 3 z 0
3.3 ELEMENT-BY-ELEMENT OPERATIONS
ในการที่จะ คูณ , หาร หรือ ยกกาลัง สมาชิกแต่ละตัวใน matrix ทาได้โดยการพิมพ์จุด ก่อนเครื่องหมาย นั้นๆ เช่น
.* (a.*b), ./ (a./b)
14
Sample Problem 3-2 : Friction experiment (element-by-element calculation)
The coefficient of friction, µ, can be determined in an experiment by measuring the force F required to move a mass m . When F is measured and m is known, the coefficient of friction can be calculated by:
F /(mg )( g 9.81m / s 2 )
F m
friction Result from measuring F in six tests are given in the table below. Determine the coefficient of friction in each test, and the average from all tests.
Solution
15
Chapter4 Script Files
4.1 NOTES ABOUT SCRIPT FILES
การใช้ script file (.m file) จะสะดวกในการ บันทึก และ แก้ไข และเรียกมาใช้ได้หลายครั้ง Script file สามารถที่จะพิมพ์ และ แก้ไข จาก text editor และสามารถ คัดลอกมาวาง ที่ MATLAB editor ได้ การสร้าง Script file ทาได้โดยการพิมพ์ edit ลงใน command window หน้าต่าง text editor จะปรากฏดังรูป
Line Number
16
4.2 RUNNING A SCRIPT FILE
คลิกเพือเริ่มรั น .m file ่
ส่วนของคำสั่งต่ำงๆใน โปรแกรมทีเรำเขีย นขึ้น ่
คำอธิบำยต่ำงๆ โดยกำรใส่ เครื่องหมำย % ไว้ข้ำงหน้ำ
จากโปรแกรมข้างบนที่ใช้ชื่อไฟล์ว่า
Chapter4example1.m เมื่อคลิกรัน จะปรากฏผลที่ หน้า Command window ดังต่อไปนี้
Remark: เราสามารถ พิมพ์ชื่อของ m file ที่ command window เพื่อรัน m file ตัวนั้นได้เช่นกัน
4.3 INPUT COMMAND FOR SCRIPT FILE
1. Input command โดยการเรียกให้ใส่ค่าที่ command window จะใช้คาสั่ง
Variable_name = input(‘string with a message that is displayed in the command window’
17
Example2
เมื่อพิมพ์ m file เสร็จแล้วทาการ save สามารถพิมพ์ชื่อของ m file บน command window เพื่อให้ประมวลผลได้เลย
2. Input command โดยการกาหนดตัวแปรอยู่ ภายใต้ m file เลย
Example3
18
4.4 OUTPUT COMMAND 1. The disp Command : รูปแบบของคาสั่งจะเป็น
disp (name of a variable) or disp(‘text as string’)
Example4
19
2. The fprintf command : ใช้ในการแสดง ตัวอักษร (text) หรือ ข้อมูล (data) รูปแแบบจะเป็น
fprintf(‘text typed in as a string’)
Example5
คาสั่ง fprintf สามารถเขียนในอีกรูปแบบได้เป็น
fprintf(‘….text….%g…….%g….%f……..’, variable1,variable2,variable3)
Example6
20
Sample Problem: Voltage divider
When several resistors are connected in an electrical circuit in series, the voltage across each of them is given by the voltage divider rule:
vn
Rn vs Req
Where vn and Rn are the voltage across resistor n and its resistance, respectively, Req Rn is the equivalent resistance, and v s is the source voltage. The power dissipated in each resistor given by:
21
Pn
Rn 2 vs 2 Req
R2 R3
The figure below shows, for example, a circuit with seven resistors connected in series.
R1
+
-
R4
Write the script file that calculates the voltage across each resistor, and power dissipated in each resistor. Run the file and enter the following data for v s and the R’s
v s =24V, R1=20 , R2=14 , R3=12 , R4=18 , R5=8 , R6=15 , R7=10
R7
R6
R5
Solution
22
23
Chapter5 Two Dimension Plot
5.1 The plot command : รูป แบบในการใช้คาสั่ง plot คือ
Plot ( x , y )
X และ y เป็น vector
Tutorial 5-1 : Simple plot
24
รูปแบบของการใช้คาสั่ง plot เขียนได้ในรูปแบบดังต่อไปนี้
Plot ( x , y , ‘line specifiers’,’PropertyName’,PropertyValue )
Vector Vector เป็น optional ใช้ในการ กาหนด ชนิด และสี ของ เส้นกราฟ และ maker เป็น optional ใช้ในการกาหนด ขนาดของ เส้นกราฟ ขนาดของ maker และขนาดของ ขอบกราฟ และ สีของพืนทีว่างในกราฟ ้ ่
รูปแบบของตัวแปรต่างๆที่ ใช้ในการกาหนดลักษณะของกราฟ, ขนาด ,สี
Remark: สามารถที่จะดูรายละเอียดของการกาหนดลักษณะของกราฟเพิ่มเติมจาก Help
25
Tutorial 5-2 : Plot of Given data
YEAR SALES (millions)
ใน case นี้กาหนดข้อมูลของยอดขายในแต่ละปีดังตารางข้างล่าง
1988 8
1989 12
1990 20
1991 22
1992 18
1993 24
1994 27
กาหนด vector ของปีเป็น yr กาหนด vector ยอดขายเป็น sle จากข้อมูลสามารถ plot กราฟได้เป็น
เมื่อสั่งตามคาสั่งดังข้างบนจะได้รูปกราฟข้างล่างนี้
26
Tutorial 5-3: Plot of a function
หลังจากรัน m file จะได้กราฟดังรูปข้างล่างนี้
รายละเอียดของการ plot กราฟ จะมีรายละเอียดย่อยเยอะมาก ให้ดูเพิ่มเติมการ plot ใน menu help
27
Sample Problem 5-1 : Plotting a function and its derivatives
Plot the function y 3x3 26 x 10 , and its first and second derivatives, for 2 x 4 ,all in the same plot.
Solution
The first derivative of the function is: y 9 x 26 The second derivative of the function is: y 18 x
ผลของการรัน script file จะได้กราฟดังรูปข้างล่างนี้
28
Chapter 6 Programming in MATLAB
6.1 RELATIONAL AND LOGICAL OPERATORS
เป็นการใช้ MATLAB ช่วยใน ตรรกะศาสตร์ ผลของการคานวน เป็น 1 หมายถึง true และ 0 หมายถึง false
Relational Operators :
Relational operators ประกอบไปด้วย
Relational Operators
= = = เท่ากับ ~=
Description
น้อยกว่า มากกว่า น้อยกว่าหรือเท่ากับ มากกว่าหรือเท่ากับ ไม่เท่ากับ
Tutorial 6-1 Relational Operators
Logical Operators: ประกอบไปด้วย AND (A&B) , OR (A|B) และ NOT ( ~A)
29
Tutorial 6-1 Logical Operators (cont.)
6.2 CONDITIONAL STATEMENTS : การเขียนรูปแแบของการกาหนด เงื่อนไข
if conditional expression consisting of relational and/or logical operators
ตัวอย่าง
if if if if if
a= 5 a == b a ~= 0 (d7)
30
if (x~=13) | (y<0)
พจน์ของเงื่อนไข อาจเป็นส่วนหนึ่งใน .m file หรือ เป็น function file ก็ได้ ในการใช้ เงื่อนไข if จะต้องมี end ปิดท้ายเสมอ
Tutorial 6-2 Conditional statement (if-end) Sample Problem 6-2 : คานวณค่าแรงของพนักงาน
ให้เขียน script file เพื่อคานวณค่าแรงของพนักงาน จานวนชั่งโมงปกติคือ 40 ชม. ถ้ามากว่านั้น ถึงเป็น overtimes ให้ค่าแรงต่อชั่วโมงเพิ่มขึ้น 50%
Solution เราจะต้องใช้ if ในการกาหนด เงื่อนไขของเวลาที่เกินกว่า 40 ชั่วโมง เพื่อคิดเป็น overtimes
เมื่อรัน .m file แล้วจะได้ผลลัพธ์ ดังข้างล่างนี้
31
6.3 LOOP for-end Loops
Loop index ที่ส่วนมากใช้กันอยู่ได้แก่ complex number. รูปแบบของการใช้คาสั่ง loop จะเป็น
i, j, k, m และ n แต่ i และ j ไม่นิยมใช้ถ้าในการใช้ MATLAB นั้นมีการใช้
32
Tutorial 6-3 for-end Loops
Sample Problem 6-3 : Sum of series
( 1) k k 6-3-1. ให้ใช้คาสั่ง for-loop ใน .m file เพื่อใช้ในการคานวณผลรวม n เทอมของ 2 k โดยให้รัน m file k 1 ที่ n = 4 และ n = 20
n
Solution
หลังจากนั้นรัน m file โดยการพิมพ์ชื่อ file ที่ command window จะได้ผลลัพธ์เป็น
33
6-3-2. The function sin(x) can be written as a Taylor series by:
(1) k x 2 k 1 sin x k 0 (2 k 1)!
Calculate sin(x) โดยใช้ Taylor’s series. ในการป้อนข้อมูลเข้า โดย x เป็นมุม (degree) และ n คือ จานวนเทอมใน series ให้คานวณที่ sin(150°) และ n เท่ากับ 3 และ 7 เทอม
34
Solution
หลังจากคีย์ เสร็จให้ save file