0% found this document useful (0 votes)
390 views8 pages

003 Mcad Mathcad Programming

Uploaded by

apirakq
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
390 views8 pages

003 Mcad Mathcad Programming

Uploaded by

apirakq
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mathcad programming 1

Mathcad programming

รูปแบบที่ใชในการเขียนโปรแกรมของ Mathcad
1. Add program line กําหนด ตัวแปร ฟงกชัน หรือการทํางานอื่นๆ

ตัวอยาง a ← 11 = 121
2
a
ตัวอยาง

f ( x , y) :=
sin( x +y
2 )+
2
2
x +y
2
f ( 1 , 2) = 2.588
2 2
x +y
can be written as

2 2
f ( x , y) := r← x +y
sin( r)
+r
r
f ( 1 , 2) = 2.588

2. Local definition กําหนดคาใหกับตัวแปร



ตัวอยาง x := 6
x+ 1
= 1.4
x− 1
value := x ← 11 Lacal value in thistatement
s x = 11
x+ 1
x− 1
value = 1.2

x+ 1
= 1.4 use original value of x outside program
x− 1

เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics

Page 1 of 8
2 Mathcad programming

3. If statement กําหนดใหทํางาน A เมื่อเงื่อนไขเปนจริง if A if condition

otherwise statement กําหนดการทํางาน otherwise

ตัวอยาง Abs( x) := −x if x < 0


x otherwise
Abs( −4) = 4
Abs( 5) = 5

ตัวอยาง กําหนดฟงกชันนิยามเปนชวง

f ( t) := −4 if t < −4
t if t ≥ −4 ∧ t ≤ 4
4 if t > 4

6
4
2
f ( t)
10 8 6 4 2 0 2 4 6 8 10
2
4
6

เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics

Page 2 of 8
Mathcad programming 3

4. For Loop กําหนดการทํางานเปน loop for ∈

ตัวอยาง การหาผลบวก 1 + 2 + 3 + ... + n


sum( n) := s←0
for i ∈ 1 .. n
s←s+ i
sum( 5) = 15

ตัวอยาง การกําหนดเมทริกซเอกลักษณ
I ( n) := for i ∈ 0 .. n − 1
for j ∈ 0 .. n − 1
mi , j ← 1 if i j
mi , j ← 0 otherwise
m

⎛⎜ 1 0 0 ⎞
I ( 3) = ⎜ 0 1 0 ⎟
⎜0 0 1
⎝ ⎠

ตัวอยาง ลําดับของจํานวนเต็มบวก 1, 2, ... , ที่หารดวย a หรือ b ลงตัว

sequence( n , a , b) := j←0
for k ∈ 1 .. n
if ( mod( k , a) 0) ∨ ( mod( k , b) 0)
vj ← k
j← j+ 1
v
T
sequence( 12 , 3 , 5) = ( 3 5 6 9 10 12 )

เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics

Page 3 of 8
4 Mathcad programming

ตัวอยาง การหาผลคูณของสมาชิกใน v

⎛2 3 ⎞
v := ⎜
⎝4 5 ⎠
prod ( v) := p←1
for i ∈ v
p ← p ⋅i
p

prod ( v) = 120

ตัวอยาง การแปลงแถวเมทริกซ
⎛⎜ 3 5 7 ⎞
ORIGIN := 1 i := 1 .. 3 j := 1 .. 3 A := ⎜ 1 4 2 ⎟
⎜6 1 6
⎝ ⎠
change( A , i1 , i2) := a←A
for j ∈ 1 .. 3
⎛⎜ 1 4 2 ⎞
tempj ← ai1 , j
change( A , 1 , 2) = ⎜ 3 5 7 ⎟
ai1 , j ← ai2 , j ⎜6 1 6
⎝ ⎠
ai2 , j ← tempj
a

add ( A , i1 , k , i2) := a←A


for j ∈ 1 .. 3 ⎛⎜ 13 45 27 ⎞
ai1 , j ← k ⋅ai2 , j + ai1 , j add ( A , 1 , 10 , 2) = ⎜ 1 4 2 ⎟
⎜6 1 6
a ⎝ ⎠
( , i1 , k) :=
multipleA a←A
⎛⎜ 3 5 7 ⎞
for j ∈ 1 .. 3
( , 2 , 10) = ⎜ 10 40 20 ⎟
multipleA
ai1 , j ← k ⋅ai1 , j ⎜6 1 6
⎝ ⎠
a

เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics

Page 4 of 8
Mathcad programming 5

5. While Loop กําหนดการทํางาน statement เมื่อเงื่อนไข condition เปนจริง


while while condition
statement

ตัวอยาง การหา n!
factorial( n) := n_fac ← 1
i← 1
while i ≤ n
n_fac ← n_fac⋅i
i← i+ 1
n_fac
factorial( 4) = 24
factorial( 5) = 120

ตัวอยาง การหารากที่สองของ a
square_root_type1( a , ε ) := estsqrt ← 1
2
while estsqrt − a ≥ ε
1 ⎛ a ⎞
estsqrt ← ⋅⎜ estsqrt +
2 ⎝ estsqrt ⎠

square_root_type1( 3 , 0.5) = 1.75

square_root_type1( 3 , 0.05) = 1.73214285714286

square_root_type2( a , ε ) := estsqrt ← 1
while 1
1 ⎛ a ⎞
estsqrt ← ⋅⎜ estsqrt +
2 ⎝ estsqrt ⎠
2
break if estsqrt − a < ε
estsqrt

square_root_type2( 3 , 0.5) = 1.75

square_root_type2( 3 , 0.05) = 1.732142857143

เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics

Page 5 of 8
6 Mathcad programming

6. Return statement สงคาที่กําหนด ออกจากการทํางานของโปรแกรม


ตัวอยาง การหา n!
factorial( n) := ( return "undefine") if n < 0
n_fac ← 1
i← 1
while i ≤ n
n_fac ← n_fac⋅i
i← i+ 1
n_fac
factorial( 5) = 120 factorial( −2) = "undefine"

ตัวอยาง การตรวจสอบวา A เปน Singular matrix หรือ Non-singular matrix

( ) :=
Type_of_matrixA return "Non-singular matrix"if A 0
return "Singula matrix"if A ≠ 0

⎛⎛ 1 2 ⎞⎞
Type_of_matrix⎜ ⎜ = "Singula matrix"
⎝⎝ 3 4 ⎠⎠
⎛⎛ 1 2 ⎞⎞
Type_of_matrix⎜ ⎜ = "Non-singular matrix"
⎝⎝ 2 4 ⎠⎠

เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics

Page 6 of 8
Mathcad programming 7

7. Break statement หยุดการทํางานของโปรแกรม


ตัวอยาง factorial( n) := ( return "undefine") if n < 0
n_fac ← 1
i← 1
while 1
n_fac ← n_fac⋅i
i← i+ 1
break if i > n
n_fac

factorial( 5) = 120 factorial( −2) = "undefine"


8. Continue statement ขามการทํางานของ loop
ตัวอยาง
f ( n) := s←0
for k ∈ 0 .. n if n ≥ 0
continue if mod( k , 2) 0
2
s←s+ k
for k ∈ 0 .. n otherwise
s←s+ k
s

f ( 6) = 35 2 2 2
1 + 3 + 5 = 35 odd positive integers only
6
f ( −6) = 21 ∑ j = 21 all nonnegative integers included

j= 0
9. Error staement แสดงผลของ error ที่กําหนดไว

ตัวอยาง

เอกสารประกอบการสอนวิชา 2301266 Computational Mathematics

Page 7 of 8
8 Mathcad programming

Recursive function
ตัวอยาง การหา n!
F ( n) := n⋅F ( n − 1) if n > 1
1 otherwise

F ( 4) = 24
ตัวอยาง การหา ห.ร.ม.
GCD( x , y) := y if x 0
GCD( mod( y , x) , x) otherwise

GCD( 424 , 484) = 4 GCD( 315 , 145) = 5

ตัวอยาง การหา Fibonacci sequence


fib( n) := fib( n − 1) + fib( n − 2) if n > 1
1 otherwise

k := 0 .. 5 fib( k) =
1
1
2
3
5
8

ตัวอยาง การเขียนกราฟของฟงกชันเปนคาบ
f ( x) := 1 if −1 ≤ x ≤ 0
x if 0 ≤ x < 1
f ( x − 2) if x > 1
f ( x + 2) if x < −1

f ( t)
4 3 2 1 0 1 2 3 4

1
เอกสารประกอบการสอนวิชา 2301266t Computational Mathematics

Page 8 of 8

You might also like