ODESOLVE.TST 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. % Tests and demonstrations for the odesolve package
  2. % First some tests of the testdf module
  3. algebraic procedure showode();
  4. <<write "order is ", odeorder, " and degree is ", odedegree;
  5. write "linearity is ", odelinearity," and highestderiv is ",
  6. highestderiv>>;
  7. depend y,x$
  8. ode1 := df(y,x);
  9. sortoutode(ode1, y, x)$
  10. showode()$
  11. sortoutode(ode1**2,y,x)$
  12. showode() $
  13. sortoutode(e**ode1,y,x) $
  14. showode() $
  15. sortoutode(df(y,x)*df(y,x,2),y,x) $
  16. showode() $
  17. nodepend y,x $
  18. depend z,w $
  19. sortoutode(df(z,w,2)+3*z*df(z,w)+e**z,z,w) $
  20. showode() $
  21. nodepend z,w $
  22. % ******************************************
  23. % Next some tests for first-order differential equations
  24. depend y,x $
  25. % Just to test tracing
  26. on trode $
  27. % First-order quadrature case
  28. ode := df(y,x) - x**2 - e**x;
  29. odesolve(ode, y, x);
  30. % A first-order linear equation, with an initial condition
  31. ode:=df(y,x) + y * sin x/cos x - 1/cos x;
  32. ans:=odesolve(ode,y,x);
  33. % Note that arbconst is declared as an operator
  34. % The initial condition is y = 1 at x = 0 so we do...
  35. arbconst(!!arbconst)
  36. := sub(y=1,x=0,rhs first solve(ans,arbconst(!!arbconst)));
  37. ans;
  38. clear arbconst(!!arbconst) $
  39. % A simple separable case
  40. ans := odesolve(df(y,x) - y**2,y,x);
  41. % We can improve this by
  42. solve(ans,y);
  43. nodepend y,x $
  44. % A separable case, in different variables, with an initial condition
  45. depend z,w $
  46. ode:= (1-z**2)*w*df(z,w)+(1+w**2)*z;
  47. % Assign the answer so we can input the condition (z = 2 at w = 1/2)
  48. ans:=odesolve(ode,z,w);
  49. % To tidy up the answer we will get for the constant we use
  50. for all x let log(x)+log(1/x)=0 $
  51. arbconst(!!arbconst) := sub(z=2,w=1/2,
  52. rhs first solve(ans,arbconst(!!arbconst)));
  53. ans;
  54. clear arbconst(!!arbconst) $
  55. nodepend z,w $
  56. % Now a homogeneous one
  57. depend y,x $
  58. ode:=df(y,x) - (x-y)/(x+y);
  59. % To make this look decent...
  60. for all x,w let e**((log x)/w)=x**(1/w),
  61. (sqrt w)*(sqrt x)=sqrt(w*x) $
  62. ans := odesolve(ode,y,x);
  63. % Reducible to homogeneous
  64. % Note this is the previous example with origin shifted
  65. ode:=df(y,x) - (x-y-3)/(x+y-1);
  66. ans := odesolve(ode,y,x);
  67. % and the special case of reducible to homogeneous
  68. ode:=df(y,x)-(2*x+3*y+1)/(4*x+6*y+1);
  69. ans := odesolve(ode,y,x);
  70. % To tidy up the next one we need
  71. for all x,w let e**(log x + w) = x*e**w,
  72. e**(w*log x)=x**w $
  73. % a Bernoulli equation
  74. ode:=x*(1-x**2)*df(y,x) + (2*x**2 -1)*y - x**3*y**3;
  75. odesolve(ode,y,x);
  76. % and finally, in this set, an exact case
  77. ode:=(2*x**3 - 6*x*y + 6*x*y**2) + (-3*x**2 + 6*x**2*y - y**3)*df(y,x);
  78. odesolve(ode,y,x);
  79. % ******************************************
  80. % Now for higher-order linear equations with constant coefficients
  81. % First, examples without driving terms
  82. % A simple one to start
  83. ode:=6*df(y,x,2)+df(y,x)-2*y;
  84. odesolve(ode,y,x);
  85. % An example with repeated and complex roots
  86. ode:=df(y,x,4)+2*df(y,x,2)+y;
  87. odesolve(ode,y,x);
  88. % A simple right-hand-side using the above example;
  89. % It will need the substitution
  90. for all w let (sin w)**2 + (cos w)** 2 = 1 $
  91. ode:=ode-exp(x);
  92. odesolve(ode,y,x);
  93. ode:=df(y,x,2)+4*df(y,x)+4*y-x*exp(x);
  94. ans:=odesolve(ode,y,x);
  95. % At x=1 let y=0 and df(y,x)=1
  96. ans2 := solve({first ans, 1 = df(rhs first ans, x)},
  97. {arbconst(!!arbconst-1),arbconst(!!arbconst)});
  98. arbconst(!!arbconst -1) := sub(x=1,y=0,rhs first first ans2);
  99. arbconst(!!arbconst) := sub(x=1,y=0,rhs second first ans2);
  100. ans;
  101. clear arbconst(!!arbconst),arbconst(!!arbconst-1), ans, ans2 $
  102. % For simultaneous equations you can use the machine e.g. as follows
  103. depend z,x $
  104. ode1:=df(y,x,2)+5*y-4*z+36*cos(7*x);
  105. ode2:=y+df(z,x,2)-99*cos(7*x);
  106. ode:=df(ode1,x,2)+4*ode2;
  107. y := rhs first odesolve(ode,y,x);
  108. z := rhs first solve(ode1,z);
  109. clear ode1, ode2, ode, y,z $
  110. nodepend z,x $
  111. % A "homogeneous" n-th order (Euler) equation
  112. ode := x*df(y,x,2) + df(y, x) + y/x + (log x)**3;
  113. odesolve(ode, y, x);
  114. % Not yet working
  115. % ode :=6*df(y,x,2)+df(y,x)-2*y + tan x;
  116. % odesolve(ode, y,x);
  117. % To reset the system
  118. !!arbconst := 0 $
  119. clear ode $
  120. off trode$
  121. nodepend y,x $
  122. end $