2019年2月19日 星期二

Julia語言 求解常微分方程式

 Julia語言 求解常微分方程式

常微分方程:

dudt=f(u)
要解決此問題,請使用名為DifferentialEquations的包裝。 http://docs.juliadiffeq.org/latest/要使用此軟件包,請啟動Julia並按]進入軟件包模式並添加DifferentialEquations。 例如

 


當我們想要從t = 0 到t = 1 求解微分方程時,初始值為u(t = 0)= 0.5 ,正確的解是u = 0.5 exp(1.01 t),所以我們輸出它的比較。

=====程式======
using DifferentialEquations
f(u,p,t) = 1.01*u u0=1/2 tspan = (0.0,1.0) prob = ODEProblem(f,u0,tspan) #用微分f和初始值u0求解微分方程du / dt = f(u)。 @time sol = solve(prob,Tsit5(),reltol=1e-8,abstol=1e-8) #Tsit5指定如何解決 nt = 50 t = range(0.0, stop=1.0, length=nt) #產生從0.0到1.0的nt點 for i=1:nt println("t= $(t[i]), solution: $(sol(t[i])), exact solution $(0.5*exp(1.01t[i]))") end


輸出結果
4.665368 seconds (9.82 M allocations: 502.253 MiB, 5.39% gc time)
t= 0.0, solution: 0.5, exact solution 0.5
t= 0.02040816326530612, solution: 0.5104130721711381, exact solution 0.5104130721695938
t= 0.04081632653061224, solution: 0.521043008483123, exact solution 0.521043008483206
t= 0.061224489795918366, solution: 0.5318943253888136, exact solution 0.5318943253848016
t= 0.08163265306122448, solution: 0.5429716333785455, exact solution 0.5429716333784603
t= 0.10204081632653061, solution: 0.5542796390000768, exact solution 0.5542796389872846
t= 0.12244897959183673, solution: 0.5658231467495112, exact solution 0.5658231467531065
t= 0.14285714285714285, solution: 0.5776070613117801, exact solution 0.5776070612778401
t= 0.16326530612244897, solution: 0.5896363893061385, exact solution 0.5896363893073463
t= 0.1836734693877551, solution: 0.6019162418595128, exact solution 0.6019162418586985
t= 0.20408163265306123, solution: 0.6144518364609074, exact solution 0.614451836391749
t= 0.22448979591836735, solution: 0.6272484990157159, exact solution 0.6272484990259225
t= 0.24489795918367346, solution: 0.6403116668043455, exact solution 0.6403116668031752
t= 0.2653061224489796, solution: 0.6536468901140217, exact solution 0.6536468899980838
t= 0.2857142857142857, solution: 0.6672598344918761, exact solution 0.667259834476045
t= 0.30612244897959184, solution: 0.6811562840751787, exact solution 0.6811562841005855
t= 0.32653061224489793, solution: 0.6953421433017668, exact solution 0.6953421431908089
t= 0.3469387755102041, solution: 0.7098234391678016, exact solution 0.7098234390300208
t= 0.3673469387755102, solution: 0.7246063243662845, exact solution 0.7246063244265986
t= 0.3877551020408163, solution: 0.7396970803354674, exact solution 0.739697080328195
t= 0.40816326530612246, solution: 0.7551021187367859, exact solution 0.7551021184903857
t= 0.42857142857142855, solution: 0.7708279843120078, exact solution 0.7708279842008927
t= 0.4489795918367347, solution: 0.7868813589620731, exact solution 0.7868813590605455
t= 0.46938775510204084, solution: 0.8032690638491494, exact solution 0.8032690638221565
t= 0.4897959183673469, solution: 0.8199980616406988, exact solution 0.8199980612885207
t= 0.5102040816326531, solution: 0.8370754593880126, exact solution 0.8370754592707694
t= 0.5306122448979592, solution: 0.8545085134765903, exact solution 0.8545085136083342
t= 0.5510204081632653, solution: 0.8723046312870144, exact solution 0.8723046312518059
t= 0.5714285714285714, solution: 0.8904713738588615, exact solution 0.8904713734099978
t= 0.5918367346938775, solution: 0.9090164589586237, exact solution 0.9090164587625491
t= 0.6122448979591837, solution: 0.9279477665697787, exact solution 0.9279477667394351
t= 0.6326530612244898, solution: 0.9472733409000371, exact solution 0.9472733408687773
t= 0.6530612244897959, solution: 0.9670013927005262, exact solution 0.9670013921943749
t= 0.673469387755102, solution: 0.9871403031344421, exact solution 0.9871403027644102
t= 0.6938775510204082, solution: 1.0076986290220444, exact solution 1.0076986291928112
t= 0.7142857142857143, solution: 1.0286851062777065, exact solution 1.0286851062947822
t= 0.7346938775510204, solution: 1.0501086512464233, exact solution 1.05010865079805
t= 0.7551020408163265, solution: 1.0719783657477793, exact solution 1.0719783651313994
t= 0.7755102040816326, solution: 1.094303541239772, exact solution 1.0943035412921123
t= 0.7959183673469388, solution: 1.1170936646361636, exact solution 1.117093664793946
t= 0.8163265306122449, solution: 1.1403584189339906, exact solution 1.1403584186973368
t= 0.8367346938775511, solution: 1.1641076885491357, exact solution 1.1641076877235352
t= 0.8571428571428571, solution: 1.1883515627049834, exact solution 1.1883515624544234
t= 0.8775510204081632, solution: 1.2131003433437266, exact solution 1.2131003436197982
t= 0.8979591836734694, solution: 1.238364546558309, exact solution 1.2383645464739423
t= 0.9183673469387755, solution: 1.2641549060830048, exact solution 1.2641549052633412
t= 0.9387755102040817, solution: 1.290482378481133, exact solution 1.2904823777874475
t= 0.9591836734693877, solution: 1.3173581498691262, exact solution 1.3173581500544267
t= 0.9795918367346939, solution: 1.3447936409428072, exact solution 1.3447936410338652
t= 1.0, solution: 1.3728005076225722, exact solution 1.3728005075084582





沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...