Title: ????????
1????
2???????
- ????
- ???????????????????????
- ????
- ?????????????????
- ?????????????????????????
?) ???????????????????? ???????????
3????(1)????
41.??????????
- ???????
- ???????
- ????
- 0.1?2???? ??????????
- ???
- ????
- ?????
- Taylor?????????????
5???????
- ???????????,??????????????
- ??????????????????,
- ???????,??????????? (???????)??????
IEEE 754 ??????????
??
??
??
??????????1.0101010?2?????????????????
????(?????????????????)
6???????
IEEE 754 ??????????
- ??? 32?????????
- ??? 64?????????
???????????0????0(0)????? ????????????????? ????
?????????????
7????
- 10???2??????????????????????????????????
?)
0.1 (10) 2-4 2-5 2-8 2-9
0.000110011 (2) x 20 1.10011 (2) x 2-4
0.1(10)?????????????????? (10??????1/3?1/7????????
)
1/3 0.333333 1/7 0.142857
8??????
Ruby
irb????????? (0.130.3) ????????????????????
??? (0.13-0.3).zero?
???0.1?3??0.3?????????????????????? ??????(true)??
?????????????????(false)???
???
???
???
0?1?
???????
"0", "01111111011", "10011001100110011010"
0?1?
"0", "01111111101", "00110011001100110100"
??
"0", "01111111101", "00110011001100110011"
?????
(??)irb????????? 0.13-0.3 2-54
0?52?
9- ..0110011010
- ..1100110100
- -------------------
- ..0011001110
- ..00110100
10?????
?????????????24??? ???10???7????????????????? 1.0
00 (2) 1 ?? 1.111(2) 2-2-232-1.2x10-7 2
???????? 1.2 x 10-38 ??1.7 x 1038 200000001 (2)
-127 2-126 1.2 x 10-38 211111110 (2) -127
2127 1.7 x 1038
200000000 (2) -127 ?? 211111111 (2) -127
? ?????????? ??????
??????? 2 x 21272128 3.4 x 1038 ???
11?????
?????????????53??? ???10???16?????? 1.000(2) 1
?? 1.111(2) 2-2-52 2-2.2x10-16 2
???????? 2.2 x 10-308 ??9.0 x 10307 200001 (2)
-1023 2-1022 2.2 x 10-308 211110 (2) -1023
21023 9.0 x 10307
2000000 (2) -127 ? 2111111 (2) -127
? ?????????? ??????
??????? 2 x 2102321024 1.7 x 10308 ???
12???
- ???????????????????????
- ???????????????
- ?????
- ?????????????
- ????????????????????????
- ??????
- ???????????????????
- ?????????????????????????????????????????????
13???????
Ruby
h ?????????????? ??????
?????
h ????x1??????????????
?????
def f(x) return Math.log(x)
f(x)ln(x) end x1.0 15.times i h 0.1i
h0.1, 0.01, , 1e-15 df(f(xh)-f(x))/h
err(df-1.0).abs print "h",h,"\t
f'(",x,")",df," err",err,"\n"
?????????? ????????????? ????????????????
? ???????? ?????????
????????????????
??????????? ??????
14????????1
Java
1?n???n??????1???????????
public class err2 public static void
main(String args) int n 1 for
(int i 0 i lt 10 i) float sum
0.0f float d 1.0f / n for
(int j 0 j lt n j) sum d
System.out.println("n " n "\t d "
d "\t sum "sum) n 10
err2.java
n 1 d 1.0 sum 1.0 n 10 d 0.1 sum
1.0000001 n 100 d 0.01 sum 0.99999934 n
1000 d 0.0010 sum 0.9999907 n 10000 d
1.0E-4 sum 1.0000535 n 100000 d 1.0E-5
sum 1.0009902 n 1000000 d 1.0E-6 sum
1.0090389 n 10000000 d 1.0E-7 sum
1.0647675 n 100000000 d 1.0E-8 sum 0.25 n
1000000000 d 1.0E-9 sum 0.03125
????
????
??????
15????????2
Ruby
1?n???n??????1???????????
err2.rb
7.times m n10m
n1,10,100,,1000000 sum0.0 d1.0/n
n.times sumsumd ??sumd???? print
"n",n,"\t d",d,"\t sum",sum,"\n"
Ruby????????????????? ????????????????????? Java??
???????????????? ???????????????
????
n1 d1.0 sum1.0 n10 d0.1
sum1.0 n100 d0.01 sum1.0 n1000 d0.001
sum1.0 n10000 d0.0001
sum0.999999999999906 n100000 d1.0e-05
sum0.999999999998084 n1000000
d1.0e-06 sum1.00000000000792
????
16?????
- ????????????????????????????????????????????????
(???,?????????????????)
?)?????????????????
??????????????????????(n?)? ?????????
????12?? ?????????? ????????????
????????
172. ????
- ????
- ???????
18????
- ???????(piecewise linear)???????
- ??????n????????
- ????????????????????
19????(??)
????
?
??
???
?????????????
?????
??????????????
20???????
????(1????)
???????(2????)
21????????
22???????(??)
??
????
??
??????????
??????
??
???
?????????
233.?????????
- Euler Method
- Runge-Kutta (Gill) Method
24???????????
- ??(n?)??????????? n????1?????????????
- 1?????????????????
- (1?)?????????????Taylor????????????
?????
??????
?????
????????
?Taylor??????
25Euler?
?????
?????
??????
??????????
????????????
(Euler???)
????
??????????????????
??????????????
?????????????????
26????
Ruby
- def euler(n)
- y 1.0
- h 1.0/n
- for i in 0..n-1
- y y (1.0/(2y))h
- end
- y
- end
27Taylor?????????
- ????Euler?(1?)??????????????????????
- ????????????????????????????????
?)
????????
28Runge-Kutta?
- ?????Taylor???????????????????
- ??????????????????????????????????
??????? ???????? ???
????
????
?2???????
?????
Taylor??(???)????????
29Runge-Kutta?(??)
??????
???
?????
???
?
(Taylor?? 2???)
????
??????????????
???
30Runge-Kutta?(??)
- ????????????????? ?2??????Taylor???????
2?? Runge-Kutta?
???2??Runge-Kutta????
?????????????? ???Runge-Kutta??????????
31????
Ruby
- def runge_kutta(n)
- y 1.0
- h 1.0/n
- for i in 0..n-1
- k1 ...
- k2 ...
- y y (k1k2)/2.0
- end
- y
- end
32??????????
- Euler
- h??????????
- ?????????h2???
- ??????1/h???
- ?????????????h???????
- Runge-Kutta
- h2??????????
- ?????????h3???
- ??????1/h???
- ????????????h2???????
33Runge-Kutta?(4?)
- Runge-Kutta?????????????????????
- 4?????? ????????? ???? ?
?????????????(Taylor??????????????????????)
(???????????? ?????????)
4?? Runge-Kutta?
34Runge-Kutta-Gill?(4?)
- 4?????? ? ???????????Runge-Kutta-Gill????
????????????????
????????????3????????????????
35Gill????
- ??Runge-Kutta??? ???????5????
?????????? - Gill?????????3?????????
??????
step1
step2
step3
step4
36???????
??? 2??????
?
???????????????1?????? (???????1?????????)??????
?)???????????
????
????
???
??????????????1????????????
37????????????
- ??????????????Runge-Kutta???????
38Runge-Kutta???
Ruby
??M???????m???? ??? ???????????
???
???????????
4?1?????????????????4??Runge-Kutta????????
39Runge-Kutta???(??)
Ruby
?????????
h0.1 ????t??? t0 ???????? u1,0,0,1
??u?4??u0,u1,u2,u3???(???????)
100.times ????????100?????
urk(t,u,h,func) Runge-Kutta?????1???????
th ?????h??????? print "t ", t0, " u0
",u0," u1 ",u1," u2 ",u2," u3
",u3," \n"
??u ?????t?????????????????new_u????? Runge-Kutta?
?????????????
funclambda t,u new_u0,0,0,0
r(u02u12)1.5 new_u0u2
new_u1u3 new_u2-u0/r
new_u3-u1/r return new_u
?????????? ?????
40Runge-Kutta???(??)
Ruby
(??)?????????????????????????????????
???????Ruby?????????????????????
Runge-Kutta????(????32????4??????????????)
u?4?????????????????1??????? 1???Runge-Kutta?????
???
def rk(t,u,h,f) k1muladd(f.call(t,u),h)
k2muladd(f.call(th/2.0,muladd(u,1.0,k1,1.0
/2.0)),h) k3muladd(f.call(th/2.0,mulad
d(u,1,k2,1.0/2.0)),h)
k4muladd(f.call(th,muladd(u,1.0,k3,1.0))
,h) new_umuladd(u,1,k1,1.0/6.0,k2,1.0/3
.0,k3,1.0/3.0,k4,1.0/6.0) return new_u end
??Runge-Kutta??????????????????
??a???k???(a,k)????pairs?????? ???(a,k)??????a????
k?????? ???????????? (???????????????)
def muladd(pairs) rArray.new(pairs.first.first)
r.fill(0) pairs.eacha,k r.size.timesi
rikai return r end
41Runge-Kutta???(??)
Ruby
????
t 0.1 u0 0.995004158961472 u1
0.0998333321663404 u2 -0.0998334893797951
u3 0.995004158786358 t 0.2 u0
0.980066566148056 u1 0.19866916135847 u2
-0.19866948092869 u3 0.980066565047173 t 0.3
u0 0.955336472286007 u1 0.295519952547433
u2 -0.295520442490704 u3 0.955336467918296
t 0.4 u0 0.921060971123942 u1
0.389418004245675 u2 -0.389418675043335 u3
0.921060958695548 t 0.5 u0 0.877582530631772
u1 0.479425117337853 u2 -0.479425981331435
u3 0.877582502133123
??????
?????????
?gnuplot?excel?????? ????????????????? ????
???
???????????????????
???????????????? ??????????
42Ruby
??????????????? lin(v11,v12,v21,v22,v31,v32
,k1,k2,k3) ? v11k1v21k2v31k3,
v12k1v22k2v32k3
- def lin(vs,xs)
- r Array.new(vs0.size)
- for i in 0..vs0.size-1
- sum 0
- for j in 0..vs.size-1
- sum vsjixsj
- end
- ri sum
- end
- r
- end
- def vx(v,x)
- lin(v,x)
- end
43Ruby
- func lambdat,u
- r (u02u12)1.5
- u2, u3, -u0/r, -u1/r
-
- def runge_kutta(f,h,t,u,n)
- for i in 0..n-1
- k1 vx(f.call(t,u),h)
- k2 vx(f.call(th/2,lin(u,k1,1,1.0/2)),h)
- k3 vx(f.call(th/2,lin(u,k2,1,1.0/2)),h)
- k4 vx(f.call(th,lin(u,k3,1,1)),h)
- u lin(u,k1,k2,k3,k4,1,1.0/6,1.0/3,1.0/3,1
.0/6) - t th
- print u0, "\t", u1, "\n"
- end
- u
- end
- runge_kutta(func,0.1,0,1,0,0,1,100)
f ?? h ??????? t ???????? u ??? n ?????
44????(1) ???
- ???????????????
- ????
- ????
- ?????
- ???
- ????
- ?????????
- ????
- ???????
- ???????????
- Euler?
- Runge-Kutta (Gill)?
45backup
46???????
Ruby