odesolve.rlg 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. Sun Aug 18 16:52:40 2002 run on Windows
  2. % Tests and demonstrations for the odesolve package
  3. % First some tests of the testdf module
  4. algebraic procedure showode();
  5. <<write "order is ", odeorder, " and degree is ", odedegree;
  6. write "linearity is ", odelinearity," and highestderiv is ",
  7. highestderiv>>;
  8. showode
  9. depend y,x$
  10. ode1 := df(y,x);
  11. ode1 := df(y,x)
  12. sortoutode(ode1, y, x)$
  13. showode()$
  14. order is 1 and degree is 1
  15. linearity is 1 and highestderiv is df(y,x)
  16. sortoutode(ode1**2,y,x)$
  17. showode() $
  18. order is 1 and degree is 2
  19. linearity is 2 and highestderiv is df(y,x)
  20. sortoutode(e**ode1,y,x) $
  21. showode() $
  22. order is 1 and degree is 0
  23. df(y,x)
  24. linearity is e and highestderiv is df(y,x)
  25. sortoutode(df(y,x)*df(y,x,2),y,x) $
  26. showode() $
  27. order is 2 and degree is 1
  28. linearity is 2 and highestderiv is df(y,x,2)
  29. nodepend y,x $
  30. depend z,w $
  31. sortoutode(df(z,w,2)+3*z*df(z,w)+e**z,z,w) $
  32. showode() $
  33. order is 2 and degree is 1
  34. z
  35. linearity is e and highestderiv is df(z,w,2)
  36. nodepend z,w $
  37. % ******************************************
  38. % Next some tests for first-order differential equations
  39. depend y,x $
  40. % Just to test tracing
  41. on trode $
  42. % First-order quadrature case
  43. ode := df(y,x) - x**2 - e**x;
  44. x 2
  45. ode := df(y,x) - e - x
  46. odesolve(ode, y, x);
  47. This first-order ODE can be solved by quadrature
  48. x 3
  49. 3*arbconst(1) + 3*e + x
  50. {y=---------------------------}
  51. 3
  52. % A first-order linear equation, with an initial condition
  53. ode:=df(y,x) + y * sin x/cos x - 1/cos x;
  54. cos(x)*df(y,x) + sin(x)*y - 1
  55. ode := -------------------------------
  56. cos(x)
  57. ans:=odesolve(ode,y,x);
  58. This is a first-order linear ODE solved by the integrating factor method
  59. ans := {y=arbconst(2)*cos(x) + sin(x)}
  60. % Note that arbconst is declared as an operator
  61. % The initial condition is y = 1 at x = 0 so we do...
  62. arbconst(!!arbconst)
  63. := sub(y=1,x=0,rhs first solve(ans,arbconst(!!arbconst)));
  64. arbconst(2) := 1
  65. ans;
  66. {y=cos(x) + sin(x)}
  67. clear arbconst(!!arbconst) $
  68. % A simple separable case
  69. ans := odesolve(df(y,x) - y**2,y,x);
  70. This is a first-order separable ODE
  71. arbconst(3)*y - x*y - 1
  72. ans := {-------------------------=0}
  73. y
  74. % We can improve this by
  75. solve(ans,y);
  76. 1
  77. {y=-----------------}
  78. arbconst(3) - x
  79. nodepend y,x $
  80. % A separable case, in different variables, with an initial condition
  81. depend z,w $
  82. ode:= (1-z**2)*w*df(z,w)+(1+w**2)*z;
  83. 2 2
  84. ode := - df(z,w)*w*z + df(z,w)*w + w *z + z
  85. % Assign the answer so we can input the condition (z = 2 at w = 1/2)
  86. ans:=odesolve(ode,z,w);
  87. This is a first-order separable ODE
  88. 2 2
  89. 2*arbconst(4) - 2*log(w) - 2*log(z) - w + z
  90. ans := {-----------------------------------------------=0}
  91. 2
  92. % To tidy up the answer we will get for the constant we use
  93. for all x let log(x)+log(1/x)=0 $
  94. arbconst(!!arbconst) := sub(z=2,w=1/2,
  95. rhs first solve(ans,arbconst(!!arbconst)));
  96. - 15
  97. arbconst(4) := -------
  98. 8
  99. ans;
  100. 2 2
  101. - 8*log(w) - 8*log(z) - 4*w + 4*z - 15
  102. {-------------------------------------------=0}
  103. 8
  104. clear arbconst(!!arbconst) $
  105. nodepend z,w $
  106. % Now a homogeneous one
  107. depend y,x $
  108. ode:=df(y,x) - (x-y)/(x+y);
  109. df(y,x)*x + df(y,x)*y - x + y
  110. ode := -------------------------------
  111. x + y
  112. % To make this look decent...
  113. for all x,w let e**((log x)/w)=x**(1/w),
  114. (sqrt w)*(sqrt x)=sqrt(w*x) $
  115. ans := odesolve(ode,y,x);
  116. This is a first-order ODE of algebraically homogeneous type
  117. solved by change of variables y = vx method
  118. 2 2
  119. ans := {arbconst(5) + sqrt( - x + 2*x*y + y )=0}
  120. % Reducible to homogeneous
  121. % Note this is the previous example with origin shifted
  122. ode:=df(y,x) - (x-y-3)/(x+y-1);
  123. df(y,x)*x + df(y,x)*y - df(y,x) - x + y + 3
  124. ode := ---------------------------------------------
  125. x + y - 1
  126. ans := odesolve(ode,y,x);
  127. This is a first-order ODE reducible to homogeneous type
  128. solved by shifting the origin
  129. 2 2
  130. ans := {arbconst(6) + sqrt( - x + 2*x*y + 6*x + y - 2*y - 7)=0}
  131. % and the special case of reducible to homogeneous
  132. ode:=df(y,x)-(2*x+3*y+1)/(4*x+6*y+1);
  133. 4*df(y,x)*x + 6*df(y,x)*y + df(y,x) - 2*x - 3*y - 1
  134. ode := -----------------------------------------------------
  135. 4*x + 6*y + 1
  136. ans := odesolve(ode,y,x);
  137. This is a first-order ODE reducible to homogeneous type
  138. belonging to the special case where top and bottomare parallel lines
  139. solved by new variable and separation
  140. 49*arbconst(7) - 3*log(14*x + 21*y + 5) - 21*x + 42*y
  141. ans := {-------------------------------------------------------=0}
  142. 49
  143. % To tidy up the next one we need
  144. for all x,w let e**(log x + w) = x*e**w,
  145. e**(w*log x)=x**w $
  146. % a Bernoulli equation
  147. ode:=x*(1-x**2)*df(y,x) + (2*x**2 -1)*y - x**3*y**3;
  148. 3 3 3 2
  149. ode := - df(y,x)*x + df(y,x)*x - x *y + 2*x *y - y
  150. odesolve(ode,y,x);
  151. This is a first-order ODE of Bernoulli type
  152. 5
  153. 1 5*arbconst(8) + 2*x
  154. {----=----------------------}
  155. 2 4 2
  156. y 5*x - 5*x
  157. % and finally, in this set, an exact case
  158. ode:=(2*x**3 - 6*x*y + 6*x*y**2) + (-3*x**2 + 6*x**2*y - y**3)*df(y,x);
  159. 2 2 3 3 2
  160. ode := 6*df(y,x)*x *y - 3*df(y,x)*x - df(y,x)*y + 2*x + 6*x*y - 6*x*y
  161. odesolve(ode,y,x);
  162. This is an exact first order ODE
  163. 4 2 2 2 4
  164. {4*arbconst(9) + 2*x + 12*x *y - 12*x *y - y =0}
  165. % ******************************************
  166. % Now for higher-order linear equations with constant coefficients
  167. % First, examples without driving terms
  168. % A simple one to start
  169. ode:=6*df(y,x,2)+df(y,x)-2*y;
  170. ode := 6*df(y,x,2) + df(y,x) - 2*y
  171. odesolve(ode,y,x);
  172. This is a linear ODE with constant coefficients of order 2
  173. (7*x)/6
  174. arbconst(11) + e *arbconst(10)
  175. {y=--------------------------------------}
  176. (2*x)/3
  177. e
  178. % An example with repeated and complex roots
  179. ode:=df(y,x,4)+2*df(y,x,2)+y;
  180. ode := df(y,x,4) + 2*df(y,x,2) + y
  181. odesolve(ode,y,x);
  182. This is a linear ODE with constant coefficients of order 4
  183. {y= - arbconst(15)*sin(x)*x + arbconst(14)*cos(x)*x - arbconst(13)*sin(x)
  184. + arbconst(12)*cos(x)}
  185. % A simple right-hand-side using the above example;
  186. % It will need the substitution
  187. for all w let (sin w)**2 + (cos w)** 2 = 1 $
  188. ode:=ode-exp(x);
  189. x
  190. ode := df(y,x,4) + 2*df(y,x,2) - e + y
  191. odesolve(ode,y,x);
  192. This is a linear ODE with constant coefficients of order 4
  193. {y=( - 4*arbconst(19)*sin(x)*x + 4*arbconst(18)*cos(x)*x - 4*arbconst(17)*sin(x)
  194. x
  195. + 4*arbconst(16)*cos(x) + e )/4}
  196. ode:=df(y,x,2)+4*df(y,x)+4*y-x*exp(x);
  197. x
  198. ode := df(y,x,2) + 4*df(y,x) - e *x + 4*y
  199. ans:=odesolve(ode,y,x);
  200. This is a linear ODE with constant coefficients of order 2
  201. 3*x 3*x
  202. 27*arbconst(21)*x + 27*arbconst(20) + 3*e *x - 2*e
  203. ans := {y=---------------------------------------------------------}
  204. 2*x
  205. 27*e
  206. % At x=1 let y=0 and df(y,x)=1
  207. ans2 := solve({first ans, 1 = df(rhs first ans, x)},
  208. {arbconst(!!arbconst-1),arbconst(!!arbconst)});
  209. 2*x x 2 x x
  210. e *(9*e *x - 6*e *x + 2*e - 54*x*y - 27*x + 27*y)
  211. ans2 := {{arbconst(20)=-------------------------------------------------------,
  212. 27
  213. 2*x x x
  214. e *( - 3*e *x + e + 18*y + 9)
  215. arbconst(21)=----------------------------------}}
  216. 9
  217. arbconst(!!arbconst -1) := sub(x=1,y=0,rhs first first ans2);
  218. 2
  219. e *(5*e - 27)
  220. arbconst(20) := ---------------
  221. 27
  222. arbconst(!!arbconst) := sub(x=1,y=0,rhs second first ans2);
  223. 2
  224. e *( - 2*e + 9)
  225. arbconst(21) := -----------------
  226. 9
  227. ans;
  228. 3*x 3*x 3 3 2 2
  229. 3*e *x - 2*e - 6*e *x + 5*e + 27*e *x - 27*e
  230. {y=-----------------------------------------------------}
  231. 2*x
  232. 27*e
  233. clear arbconst(!!arbconst),arbconst(!!arbconst-1), ans, ans2 $
  234. % For simultaneous equations you can use the machine e.g. as follows
  235. depend z,x $
  236. ode1:=df(y,x,2)+5*y-4*z+36*cos(7*x);
  237. ode1 := 36*cos(7*x) + df(y,x,2) + 5*y - 4*z
  238. ode2:=y+df(z,x,2)-99*cos(7*x);
  239. ode2 := - 99*cos(7*x) + df(z,x,2) + y
  240. ode:=df(ode1,x,2)+4*ode2;
  241. ode := - 2160*cos(7*x) + df(y,x,4) + 5*df(y,x,2) + 4*y
  242. y := rhs first odesolve(ode,y,x);
  243. This is a linear ODE with constant coefficients of order 4
  244. y := arbconst(25)*sin(x) + arbconst(24)*cos(x) - arbconst(23)*sin(2*x)
  245. + arbconst(22)*cos(2*x) + cos(7*x)
  246. z := rhs first solve(ode1,z);
  247. z := (4*arbconst(25)*sin(x) + 4*arbconst(24)*cos(x) - arbconst(23)*sin(2*x)
  248. + arbconst(22)*cos(2*x) - 8*cos(7*x))/4
  249. clear ode1, ode2, ode, y,z $
  250. nodepend z,x $
  251. % A "homogeneous" n-th order (Euler) equation
  252. ode := x*df(y,x,2) + df(y, x) + y/x + (log x)**3;
  253. 2 3
  254. df(y,x,2)*x + df(y,x)*x + log(x) *x + y
  255. ode := ------------------------------------------
  256. x
  257. odesolve(ode, y, x);
  258. This equation is of the homogeneous (Euler) type
  259. 3
  260. {y=( - 2*arbconst(27)*sin(log(x)) + 2*arbconst(26)*cos(log(x)) - log(x) *x
  261. 2
  262. + 3*log(x) *x - 3*log(x)*x)/2}
  263. % Not yet working
  264. % ode :=6*df(y,x,2)+df(y,x)-2*y + tan x;
  265. % odesolve(ode, y,x);
  266. % To reset the system
  267. !!arbconst := 0 $
  268. clear ode $
  269. off trode$
  270. nodepend y,x $
  271. end $
  272. Time for test: 3956 ms, plus GC time: 482 ms