Upload
hoanganh
View
228
Download
4
Embed Size (px)
Citation preview
1
ENE209 MATLAB TUTORIAL
เรมตน การใช MATLAB (Starting MATLAB)
Chapter 1
- คาสง 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 วงเลบ จะเรมคานวณ จะขางในสด
Current Directory Window : จะแสดง
ไฟลทเปดใชอยใน directory ปจจบน
Command History Window : ใชเกบ
ประวตคาสงทใชใน command window
Command window : เปนหนาตาง
หลก เพอใชในการ กาหนดตวแปรตางๆ
และ รนโปรแกรม
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 เครองหมาย = เปนเครองหมายทใชในการ ประกาศคาตวแปร หรอ กาหนดคา ตวแปรนนเอง
รปแบบคอ
Tutorial 1-3 : Assignment Operator
Variable_name = ตวเลข หรอ พจน ของสมการ
4
ใน MATLAB เมอ มการกาหนดตวแปรมากๆ อาจทาใหเกดการสบสน เพราะ MATLAB จะจาคาของตวแปรนนตวลาสดท
กาหนด เพราะฉะนนจะมคาสงทใชในการดวา ตวแปรไหนถกกานดไปแลวบาง หรอ ตองการลางคาตวแปรทงหมด
จาก
Sample Problem 1-1 : Trigonometric identity
2 tan sincos2 2 tanx x x
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.
Solution
The line that connect the centers of the circle create four triangle. In two of the triangles,
1 2 3C C C∆ and 1 3 4C C C∆ 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: 2 2 2
2 3 1 2 1 3 1 2 1 3 1( ) ( ) ( ) 2( )( ) cosC C C C C C C C C C γ= + −
Next, the length of the side 2 4C C is calculated by considering the triangle 1 2 4C C C∆ .
This is done, again, by using the law of cosines ( the lengths 1 2C C and 1 4C C are known and
and the angle 3γ is the sum of the angles 1γ and 2γ ).
C1
C2
C3
C4
C1
C2
C3
C4
1γ2γ 3γ
6
การสราง Arrays (Creating Arrays)
Chapter 2
2.1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)
Vector จะถกสรางขนมาโดยการพมพ ตวเลขเขาไปใน วงเลบสเหลยม (square brackets) [ ]
การกาหนด vector จากตวเลขทรคาแนนอน
Tutorial 2-1: Creating vectors from given data
การกาหนด vector โดยการกาหนดระยะหางของคาทคงท ซงจะตองกาหนดคาเรมตน และ และคาสดทายทตองการ
หรอ
Variable_name = [Array ของตวเลข]
Variable_name = [m:q:n] Variable_name = m:q:n
7
2.2 CREATING A TWO-DIMENSIONAL ARRAY (MATRIX)
รปแบบในการสราง matrix คอ
Tutorial 2-2: Creating matrices
- Transpose operator
Variable_name = [1st row elements; 2nd row elements; 3rd row
elements; …..; last row elements]
8
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.
2 5 8 11 14 173 6 9 12 15 184 7 10 13 16 195 8 11 14 17 206 9 12 15 18 21
A
=
5 10 15 20 25 3030 35 40 45 50 5555 60 65 70 75 80
B =
[ ]99 98 97 96 94 93 92 91v =
Create the matrix array in command window
Solution
11
Operation with Arrays
Chapter3
3.1 ADDITION AND SUBTRACTION
Tutorial 3.1: Addition and Subtraction
3.2 MULTIPLICATION OF ARRAYS
Tutorial 3.2: Multiplication of arrays
12
ใหหาคาตอบของสมการเชงเสนดงตอไปน
Sample Problem 3-1: Solving three linear equations (array division)
4 2 6 82 8 2 46 1 03 0
x y zx y zx y z
− + =+ + =+ + =
ใช matrix ในการแกสมการเชงเสน จาก AX = B เขยนในรป matrix ไดเปน
Solution
4 2 6 82 8 2 46 10 3 0
xyz
− =
3.3 ELEMENT-BY-ELEMENT OPERATIONS
ในการทจะ คณ , หาร หรอ ยกกาลง สมาชกแตละตวใน matrix ทาไดโดยการพมพจด กอนเครองหมาย นนๆ
เชน .* (a.*b), ./ (a./b)
13
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: 2/( )( 9.81 / )F mg g m sµ = =
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.
mF
friction
14
Solution
15
Script Files
Chapter4
4.1 NOTES ABOUT SCRIPT FILES
• การใช script file (.m file) จะสะดวกในการ บนทก และ แกไข และเรยกมาใชไดหลายครง
• Script file สามารถทจะพมพ และ แกไข จาก text editor และสามารถ คดลอกมาวาง ท MATLAB editor ได การสราง Script
file ทาไดโดยการพมพ edit ลงใน command window หนาตาง text editor จะปรากฏดงรป
4.2 RUNNING A SCRIPT FILE
Line Number
คลกเพอเรมรน .m file
คาอธบายตางๆ โดยการใส เครองหมาย % ไวขางหนา
สวนของคาสงตางๆในโปรแกรมทเราเขยนขน
16
จากโปรแกรมขางบนทใชชอไฟลวา Chapter4example1.m เมอคลกรน จะปรากฏผลท หนา Command window ดงตอไปน
Remark: เราสามารถ พมพชอของ m file ท command window เพอรน m file ตวนนไดเชนกน
4.3 INPUT COMMAND FOR SCRIPT FILE
1. Input command โดยการเรยกใหใสคาท command window จะใชคาสง
Example2
เมอพมพ m file เสรจแลวทาการ save สามารถพมพชอของ m file บน command window เพอใหประมวลผลไดเลย
Variable_name = input(‘string with a message that is displayed in the command window’
17
2. Input command โดยการกาหนดตวแปรอย ภายใต m file เลย
Example3
18
4.4 OUTPUT COMMAND
1. The disp Command : รปแบบของคาสงจะเปน
Example4
2. The fprintf command : ใชในการแสดง ตวอกษร (text) หรอ ขอมล (data) รปแแบบจะเปน
Example5
disp (name of a variable) or disp(‘text as string’)
fprintf(‘text typed in as a string’)
19
คาสง fprintf สามารถเขยนในอกรปแบบไดเปน
Example6
fprintf(‘….text….%g…….%g….%f……..’, variable1,variable2,variable3)
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:
nn s
eq
Rv vR
=
Where nv and nR are the voltage across resistor n and its resistance, respectively, eq nR R=∑ is the equivalent resistance,
and sv is the source voltage. The power dissipated in each resistor given by:
22n
n seq
RP vR
=
The figure below shows, for example, a circuit with seven resistors connected in series.
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 sv and the R’s
sv =24V, R1=20Ω , R2=14Ω , R3=12Ω , R4=18Ω , R5=8Ω , R6=15Ω , R7=10Ω
+
-
1R 2R3R
4R
5R6R
7R
21
Solution
22
Two Dimension Plot
Chapter5
5.1 The plot command : รป แบบในการใชคาสง plot คอ
X และ y เปน vector
Tutorial 5-1 : Simple plot
Plot ( x , y )
23
รปแบบของการใชคาสง plot เขยนไดในรปแบบดงตอไปน
รปแบบของตวแปรตางๆท ใชในการกาหนดลกษณะของกราฟ , ขนาด ,ส
Remark:
สามารถทจะดรายละเอยดของการกาหนดลกษณะของกราฟเพมเตมจาก Help
Plot ( x , y , ‘line specifiers’,’PropertyName’,PropertyValue )
Vector Vector เปน optional ใชในการ
กาหนด ชนด และส ของ
เสนกราฟ และ maker
เปน optional ใชในการกาหนด ขนาดของ
เสนกราฟ ขนาดของ maker และขนาดของ
ขอบกราฟ และ สของพนทวางในกราฟ
24
Tutorial 5-2 : Plot of Given data
ใน case นกาหนดขอมลของยอดขายในแตละปดงตารางขางลาง
YEAR 1988 1989 1990 1991 1992 1993 1994SALES(millions)
18 24 278 12 20 22
กาหนด vector ของปเปน yr กาหนด vector ยอดขายเปน sle จากขอมลสามารถ plot กราฟไดเปน
เมอสงตามคาสงดงขางบนจะไดรปกราฟขางลางน
25
Tutorial 5-3: Plot of a function
หลงจากรน m file จะไดกราฟดงรปขางลางน
รายละเอยดของการ plot กราฟ จะมรายละเอยดยอยเยอะมาก ใหดเพมเตมการ plot ใน menu help
26
Sample Problem 5-1 : Plotting a function and its derivatives
Plot the function 33 26 10y x x= − + , and its first and second derivatives, for 2 4x− ≤ ≤ ,all in the same plot.
Solution
The first derivative of the function is: 9 26y x′ = −
The second derivative of the function is: 18y x′′ =
ผลของการรน script file จะไดกราฟดงรปขางลางน
27
Programming in MATLAB
Chapter 6
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)
28
Tutorial 6-1 Logical Operators (cont.)
6.2 CONDITIONAL STATEMENTS : การเขยนรปแแบของการกาหนด เงอนไข
ตวอยาง if a < b
if c >= 5
if a == b
if a ~= 0
if (d<h) & (x>7)
if conditional expression consisting of relational and/or logical operators
29
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 แลวจะไดผลลพธ ดงขางลางน
30
6.3 LOOP
• Loop index ทสวนมากใชกนอยไดแก i, j, k, m และ n แต i และ j ไมนยมใชถาในการใช MATLAB นนมการใช
complex number.
for-end Loops
รปแบบของการใชคาสง loop จะเปน
31
Tutorial 6-3 for-end Loops
Sample Problem 6-3 : Sum of series
6-3-1. ใหใชคาสง for-loop ใน .m file เพอใชในการคานวณผลรวม n เทอมของ 1
( 1)2
kn
kk
k=
−∑ โดยใหรน m file
ท n = 4 และ n = 20
Solution
หลงจากนนรน m file โดยการพมพชอ file ท command window จะไดผลลพธเปน
32
6-3-2. The function sin(x) can be written as a Taylor series by:
2 1
0
( 1)sin(2 1)!
k k
k
xxk
+∞
=
−=
+∑
Calculate sin(x) โดยใช Taylor’s series. ในการปอนขอมลเขา โดย x เปนมม (degree) และ n คอ จานวนเทอมใน series
ใหคานวณท sin(150°) และ n เทากบ 3 และ 7 เทอม
33
Solution
หลงจากคย เสรจให save file