odesolve.rlg 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. Tue Feb 10 12:26:51 2004 run on Linux
  2. % Tests and demonstrations for the ODESolve 1+ package --
  3. % an updated version of the original odesolve test file.
  4. % Original Author: M. A. H. MacCallum
  5. % Maintainer: F.J.Wright@Maths.QMW.ac.uk
  6. ODESolve_version;
  7. ODESolve 1.065
  8. on trode, combinelogs;
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. % First-order differential equations
  11. % (using automatic variable and dependence declaration).
  12. % First-order quadrature case:
  13. odesolve(df(y,x) - x^2 - e^x);
  14. *** Dependent var(s) assumed to be y
  15. *** Independent var assumed to be x
  16. *** depend y , x
  17. This is a linear ODE of order 1.
  18. It is solved by quadrature.
  19. x 3
  20. 3*arbconst(1) + 3*e + x
  21. {y=---------------------------}
  22. 3
  23. % First-order linear equation, with initial condition y = 1 at x = 0:
  24. odesolve(df(y,x) + y * tan x - sec x, y, x, {x=0, y=1});
  25. This is a linear ODE of order 1.
  26. It is solved by the integrating factor method.
  27. General solution is {y=arbconst(2)*cos(x) + sin(x)}
  28. Applying conditions {{x=0,y=1}}
  29. {y=cos(x) + sin(x)}
  30. odesolve(cos x * df(y,x) + y * sin x - 1, y, x, {x=0, y=1});
  31. This is a linear ODE of order 1.
  32. It is solved by the integrating factor method.
  33. General solution is {y=arbconst(3)*cos(x) + sin(x)}
  34. Applying conditions {{x=0,y=1}}
  35. {y=cos(x) + sin(x)}
  36. % A simple separable case:
  37. odesolve(df(y,x) - y^2, y, x, explicit);
  38. This is a nonlinear ODE of order 1.
  39. It is separable.
  40. Solution before trying to solve for dependent variable is
  41. arbconst(4)*y - x*y - 1=0
  42. 1
  43. {y=-----------------}
  44. arbconst(4) - x
  45. % A separable case, in different variables, with the initial condition
  46. % z = 2 at w = 1/2:
  47. odesolve((1-z^2)*w*df(z,w)+(1+w^2)*z, z, w, {w=1/2, z=2});
  48. *** depend z , w
  49. This is a nonlinear ODE of order 1.
  50. It is separable.
  51. 2 2
  52. General solution is {4*arbconst(5) - 2*log(w*z) - w + z =0}
  53. 1
  54. Applying conditions {{w=---,z=2}}
  55. 2
  56. 2 2
  57. { - 8*log(w*z) - 4*w + 4*z - 15=0}
  58. % Now a homogeneous one:
  59. odesolve(df(y,x) - (x-y)/(x+y), y, x);
  60. This is a nonlinear ODE of order 1.
  61. It is of algebraically homogeneous type
  62. solved by a change of variables of the form `y = vx'.
  63. 2 2
  64. {arbconst(6) + sqrt( - x + 2*x*y + y )=0}
  65. % Reducible to homogeneous:
  66. % (Note this is the previous example with origin shifted.)
  67. odesolve(df(y,x) - (x-y-3)/(x+y-1), y, x);
  68. This is a nonlinear ODE of order 1.
  69. It is quasi-homogeneous if the result of shifting the origin is homogeneous ...
  70. It is of algebraically homogeneous type
  71. solved by a change of variables of the form `y = vx'.
  72. 2 2
  73. {arbconst(7) + sqrt( - x + 2*x*y + 6*x + y - 2*y - 7)=0}
  74. % and the special case of reducible to homogeneous:
  75. odesolve(df(y,x) - (2x+3y+1)/(4x+6y+1), y, x);
  76. This is a nonlinear ODE of order 1.
  77. 2
  78. It is separable after letting y + ---*x => y
  79. 3
  80. {49*arbconst(8) - 3*log(14*x + 21*y + 5) - 21*x + 42*y=0}
  81. % A Bernoulli equation:
  82. odesolve(x*(1-x^2)*df(y,x) + (2x^2 -1)*y - x^3*y^3, y, x);
  83. This is a nonlinear ODE of order 1.
  84. It is of Bernoulli type.
  85. 5
  86. 1 5*arbconst(9) + 2*x
  87. {----=----------------------}
  88. 2 4 2
  89. y 5*x - 5*x
  90. % and finally, in this set, an exact case:
  91. odesolve((2x^3 - 6x*y + 6x*y^2) + (-3x^2 + 6x^2*y - y^3)*df(y,x), y, x);
  92. This is a nonlinear ODE of order 1.
  93. It is exact and is solved by quadrature.
  94. 4 2 2 2 4
  95. {4*arbconst(10) + 2*x + 12*x *y - 12*x *y - y =0}
  96. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  97. % Now for higher-order linear equations with constant coefficients
  98. % First, examples without driving terms
  99. % A simple one to start:
  100. odesolve(6df(y,x,2) + df(y,x) - 2y, y, x);
  101. This is a linear ODE of order 2.
  102. It has constant coefficients.
  103. (7*x)/6
  104. e *arbconst(12) + arbconst(11)
  105. {y=--------------------------------------}
  106. (2*x)/3
  107. e
  108. % An example with repeated and complex roots:
  109. odesolve(ode := df(y,x,4) + 2df(y,x,2) + y, y, x);
  110. This is a linear ODE of order 4.
  111. It has constant coefficients.
  112. {y=arbconst(16)*sin(x) + arbconst(15)*cos(x) + arbconst(14)*sin(x)*x
  113. + arbconst(13)*cos(x)*x}
  114. % A simple right-hand-side using the above example:
  115. odesolve(ode = exp(x), y, x);
  116. This is a linear ODE of order 4.
  117. It has constant coefficients.
  118. Constructing particular integral using `D-operator method'.
  119. {y=(4*arbconst(20)*sin(x) + 4*arbconst(19)*cos(x) + 4*arbconst(18)*sin(x)*x
  120. x
  121. + 4*arbconst(17)*cos(x)*x + e )/4}
  122. ode := df(y,x,2) + 4df(y,x) + 4y - x*exp(x);
  123. x
  124. ode := df(y,x,2) + 4*df(y,x) - e *x + 4*y
  125. % At x=1 let y=0 and df(y,x)=1:
  126. odesolve(ode, y, x, {x=1, y=0, df(y,x)=1});
  127. This is a linear ODE of order 2.
  128. It has constant coefficients.
  129. Constructing particular integral using `D-operator method'.
  130. 3*x 3*x
  131. 27*arbconst(22) + 27*arbconst(21)*x + 3*e *x - 2*e
  132. General solution is {y=---------------------------------------------------------
  133. 2*x
  134. 27*e
  135. }
  136. Applying conditions {{x=1,y=0,df(y,x)=1}}
  137. 3*x 3*x 3 3 2 2
  138. 3*e *x - 2*e - 6*e *x + 5*e + 27*e *x - 27*e
  139. {y=-----------------------------------------------------}
  140. 2*x
  141. 27*e
  142. % For simultaneous equations you can use the machine, e.g. as follows:
  143. depend z,x;
  144. ode1 := df(y,x,2) + 5y - 4z + 36cos(7x);
  145. ode1 := 36*cos(7*x) + df(y,x,2) + 5*y - 4*z
  146. ode2 := y + df(z,x,2) - 99cos(7x);
  147. ode2 := - 99*cos(7*x) + df(z,x,2) + y
  148. ode := df(ode1,x,2) + 4ode2;
  149. ode := - 2160*cos(7*x) + df(y,x,4) + 5*df(y,x,2) + 4*y
  150. y := rhs first odesolve(ode, y, x);
  151. This is a linear ODE of order 4.
  152. It has constant coefficients.
  153. Constructing particular integral using `D-operator method'.
  154. y := arbconst(26)*sin(x) + arbconst(25)*cos(x) + arbconst(24)*sin(2*x)
  155. + arbconst(23)*cos(2*x) + cos(7*x)
  156. z := rhs first solve(ode1,z);
  157. z := (4*arbconst(26)*sin(x) + 4*arbconst(25)*cos(x) + arbconst(24)*sin(2*x)
  158. + arbconst(23)*cos(2*x) - 8*cos(7*x))/4
  159. clear ode1, ode2, ode, y, z;
  160. nodepend z,x;
  161. % A "homogeneous" n-th order (Euler) equation:
  162. odesolve(x*df(y,x,2) + df(y, x) + y/x + (log x)^3, y, x);
  163. This is a linear ODE of order 2.
  164. It has non-constant coefficients.
  165. It is of the homogeneous (Euler) type and is reducible to a simpler ODE ...
  166. It has constant coefficients.
  167. Constructing particular integral using `D-operator method'.
  168. 3
  169. {y=(2*arbconst(28)*sin(log(x)) + 2*arbconst(27)*cos(log(x)) - log(x) *x
  170. 2
  171. + 3*log(x) *x - 3*log(x)*x)/2}
  172. % The solution here remains symbolic (because neither REDUCE nor Maple
  173. % can evaluate the resulting integral):
  174. odesolve(6df(y,x,2) + df(y,x) - 2y + tan x, y, x);
  175. This is a linear ODE of order 2.
  176. It has constant coefficients.
  177. Constructing particular integral using `D-operator method'.
  178. But cannot evaluate the integrals, so ...
  179. Constructing particular integral using `variation of parameters'.
  180. 7
  181. The Wronskian is --------
  182. x/6
  183. 6*e
  184. (7*x)/6 (7*x)/6 sin(x)
  185. {y=(7*e *arbconst(30) + 7*arbconst(29) - e *int(-------------,x)
  186. x/2
  187. e *cos(x)
  188. (2*x)/3
  189. e *sin(x) (2*x)/3
  190. + int(-----------------,x))/(7*e )}
  191. cos(x)
  192. end;
  193. Time for test: 510 ms, plus GC time: 40 ms