Upload
apirakq
View
97
Download
2
Embed Size (px)
Citation preview
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
1
Mathcad programming
รูปแบบที่ใชในการเขียนโปรแกรมของ Mathcad1. Add program line กําหนด ตัวแปร ฟงกชัน หรือการทํางานอื่นๆ
ตัวอยาง
ตัวอยาง
2. Local definition กําหนดคาใหกับตัวแปร
ตัวอยาง←
a 11←
a2
121=
x 6:=
x 1+x 1−
1.4=
value x 11←
x 1+x 1−
:= Lacal value in this statement x = 11
value 1.2=
x 1+x 1−
1.4= use original value of x outside program
f x y,( )sin x2 y2+( )
x2 y2+x2 y2++:= f 1 2,( ) 2.588=
can be written as
f x y,( ) r x2 y2+←
sin r( )r
r+
:=
f 1 2,( ) 2.588=
Page 1 of 8
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
2
3. If statement กําหนดใหทํางาน A เม่ือเงื่อนไขเปนจริง
otherwise statement กําหนดการทํางาน
ตัวอยาง
ตัวอยาง กําหนดฟงกชันนิยามเปนชวง
if A conditionif
otherwise
Abs x( ) x− x 0<if
x otherwise
:=
Abs 4−( ) 4=
Abs 5( ) 5=
f t( ) 4− t 4−<if
t t 4−≥ t 4≤∧if
4 t 4>if
:=
10 8 6 4 2 0 2 4 6 8 10
642
246
f t( )
t
Page 2 of 8
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
3
4. For Loop กําหนดการทํางานเปน loop
ตัวอยาง การหาผลบวก 1 + 2 + 3 + ... + n
ตัวอยาง การกําหนดเมทริกซเอกลักษณ
ตัวอยาง ลําดับของจํานวนเต็มบวก 1, 2, ... , ท่ีหารดวย a หรือ b ลงตัว
∈for
sum n( ) s 0←
s s i+←
i 1 n..∈for
:=
sum 5( ) 15=
I n( )
mi j, 1← i jif
mi j, 0← otherwise
j 0 n 1−..∈for
i 0 n 1−..∈for
m
:=
I 3( )
1
0
0
0
1
0
0
0
1
⎛⎜⎜⎜⎝
⎞⎟⎠
=
sequence n a, b,( ) j 0←
vj k←
j j 1+←
mod k a,( ) 0( ) mod k b,( ) 0( )∨if
k 1 n..∈for
v
:=
sequence 12 3, 5,( )T 3 5 6 9 10 12( )=
Page 3 of 8
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
4
ตัวอยาง การหาผลคูณของสมาชิกใน v
ตัวอยาง การแปลงแถวเมทริกซ
v2
4
3
5⎛⎜⎝
⎞⎠
:=
prod v( ) p 1←
p p i⋅←
i v∈for
p
:=
prod v( ) 120=
ORIGIN 1:= i 1 3..:= j 1 3..:= A
3
1
6
5
4
1
7
2
6
⎛⎜⎜⎜⎝
⎞⎟⎠
:=
change A i1, i2,( ) a A←
tempj ai1 j,←
ai1 j, ai2 j,←
ai2 j, tempj←
j 1 3..∈for
a
:=
change A 1, 2,( )
1
3
6
4
5
1
2
7
6
⎛⎜⎜⎜⎝
⎞⎟⎠
=
add A i1, k, i2,( ) a A←
ai1 j, k ai2 j,⋅ ai1 j,+←
j 1 3..∈for
a
:=
add A 1, 10, 2,( )
13
1
6
45
4
1
27
2
6
⎛⎜⎜⎜⎝
⎞⎟⎠
=
multipleA i1, k,( ) a A←
ai1 j, k ai1 j,⋅←
j 1 3..∈for
a
:=
multipleA 2, 10,( )
3
10
6
5
40
1
7
20
6
⎛⎜⎜⎜⎝
⎞⎟⎠
=
Page 4 of 8
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
5
5. While Loop กําหนดการทํางาน statement เม่ือเงื่อนไข condition เปนจริง
ตัวอยาง การหา n!
ตัวอยาง การหารากที่สองของ a
while
statement
conditionwhile
factorialn( ) n_fac 1←
i 1←
n_fac n_fac i⋅←
i i 1+←
i n≤while
n_fac
:=
factorial4( ) 24=factorial5( ) 120=
square_root_type1 a ε,( ) estsqrt 1←
estsqrt12
estsqrta
estsqrt+⎛⎜
⎝⎞⎠
⋅←
estsqrt2 a− ε≥while
:=
square_root_type1 3 0.5,( ) 1.75=
square_root_type1 3 0.05,( ) 1.73214285714286=
square_root_type2 a ε,( ) estsqrt 1←
estsqrt12
estsqrta
estsqrt+⎛⎜
⎝⎞⎠
⋅←
break estsqrt2 a− ε<if
1while
estsqrt
:=
square_root_type2 3 0.5,( ) 1.75=
square_root_type2 3 0.05,( ) 1.732142857143=
Page 5 of 8
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
6
6. Return statement สงคาท่ีกําหนด ออกจากการทํางานของโปรแกรมตัวอยาง การหา n!
ตัวอยาง การตรวจสอบวา A เปน Singular matrix หรือ Non-singular matrix
factorialn( ) "undefine"return( ) n 0<if
n_fac 1←
i 1←
n_fac n_fac i⋅←
i i 1+←
i n≤while
n_fac
:=
factorial5( ) 120= factorial 2−( ) "undefine"=
Type_of_matrixA( ) "Non-singular matrix"return A 0if
"Singula matrix"return A 0≠if
:=
Type_of_matrix1
3
2
4⎛⎜⎝
⎞⎠
⎛⎜⎝
⎞⎠
"Singula matrix"=
Type_of_matrix1
2
2
4⎛⎜⎝
⎞⎠
⎛⎜⎝
⎞⎠
"Non-singular matrix"=
Page 6 of 8
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
7
7. Break statement หยุดการทํางานของโปรแกรมตัวอยาง
8. Continue statement ขามการทํางานของ loopตัวอยาง
9. Error staement แสดงผลของ error ท่ีกําหนดไว
ตัวอยาง
factorialn( ) "undefine"return( ) n 0<if
n_fac 1←
i 1←
n_fac n_fac i⋅←
i i 1+←
break i n>if
1while
n_fac
:=
factorial5( ) 120= factorial 2−( ) "undefine"=
f n( ) s 0←
continue mod k 2,( ) 0if
s s k2+←
k 0 n..∈for n 0≥if
s s k+←
k 0 n..∈for otherwise
s
:=
f 6( ) 35= 12 32+ 52+ 35= odd positive integers only
f 6−( ) 21=
0
6
j
j∑=
21= all nonnegative integers included
Page 7 of 8
Mathcad programming
เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics
8
Recursive functionตัวอยาง การหา n!
ตัวอยาง การหา ห.ร.ม.
ตัวอยาง การหา Fibonacci sequence
ตัวอยาง การเขียนกราฟของฟงกชันเปนคาบ
F n( ) n F n 1−( )⋅ n 1>if
1 otherwise
:=
F 4( ) 24=
GCD x y,( ) y x 0if
GCD mod y x,( ) x,( ) otherwise
:=
GCD 424 484,( ) 4= GCD 315 145,( ) 5=
fib n( ) fib n 1−( ) fib n 2−( )+ n 1>if
1 otherwise
:=
k 0 5..:= fib k( )11
2
3
5
8
=
f x( ) 1 1− x≤ 0≤if
x 0 x≤ 1<if
f x 2−( ) x 1>if
f x 2+( ) x 1−<if
:=
4 3 2 1 0 1 2 3 4
1
1
f t( )
t
Page 8 of 8