dfpart.tst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. depend y,x;
  2. generic_function f(x,y);
  3. df(f(),x);
  4. df(f(x,y),x);
  5. df(f(x,x**3),x);
  6. df(f(x,z**3),x);
  7. df(a*f(x,y),x);
  8. dfp(a*f(x,y),x);
  9. df(f(x,y),x,2);
  10. df(dfp(f(x,y),x),x);
  11. df(dfp(f(x,x**3),x),x);
  12. % using a generic fucntion with commutative derivatives
  13. generic_function u(x,y);
  14. dfp_commute u(x,y);
  15. df(u(x,y),x,x);
  16. % explicitly declare 1st and second derivative commutative
  17. generic_function v(x,y);
  18. let dfp(v(~a,~b),{y,x}) => dfp(v(a,b),{x,y});
  19. df(v(),x,2);
  20. % substitute expressions for the arguments
  21. w:=df(f(),x,2);
  22. sub(x=0,y=x,w);
  23. % composite generic functions
  24. generic_function g(x,y);
  25. generic_function h(y,z);
  26. depend z,x;
  27. w:=df(g()*h(),x);
  28. sub(y=0,w);
  29. % substituting g*h for f in a partial derivative of f,
  30. % inheriting the arguments of f. Here no derivative of h
  31. % appears because h does not depend of x.
  32. sub(f=g*h,dfp(f(a,b),x));
  33. % indexes.
  34. % in the following total differential the partial
  35. % derivatives wrt i and j do not appear because i and
  36. % j do not depend of x.
  37. generic_function m(i,j,x,y);
  38. df(m(i,j,x,y),x);
  39. % computation with a differential equation.
  40. generic_function f(x,y);
  41. operator y;
  42. let df(y(~x),x) => f(x,y(x));
  43. % some derivatives
  44. df(y(x),x);
  45. df(y(x),x,2);
  46. df(y(x),x,3);
  47. sub(x=22,ws);
  48. % taylor expansion for y
  49. load_package taylor;
  50. taylor(y(x0+h),h,0,3);
  51. clear w;
  52. %------------------------ Runge Kutta -------------------------
  53. % computing Runge Kutta formulas for ODE systems Y'=F(x,y(x));
  54. % forms corresponding to Ralston Rabinowitz
  55. load_package taylor;
  56. operator alpha,beta,w,k;
  57. % s= order of Runge Kutta formula
  58. s:=3;
  59. generic_function f(x,y);
  60. operator y;
  61. % introduce ODE
  62. let df(y(~x),x)=>f(x,y(x));
  63. % formal series for solution
  64. y1_form := taylor(y(x0+h),h,0,s);
  65. % Runge-Kutta Ansatz:
  66. let alpha(1)=>0;
  67. for i:=1:s do
  68. let k(i) => h*f(x0 + alpha(i)*h,
  69. y(x0) + for j:=1:(i-1) sum beta(i,j)*k(j));
  70. y1_ansatz:= y(x0) + for i:=1:s sum w(i)*k(i);
  71. y1_ansatz := taylor(y1_ansatz,h,0,s);
  72. % compute y1_form - y1_ans and collect coeffients of powers of h
  73. y1_diff := num(taylortostandard(y1_ansatz)-taylortostandard(y1_form))$
  74. cl := coeff(y1_diff,h);
  75. % f_forms: forms of f and its derivatives which occur in cl
  76. f_forms :=q := {f(x0,y(x0))}$
  77. for i:=1:(s-1) do
  78. <<q:= for each r in q join {dfp(r,x),dfp(r,y)};
  79. f_forms := append(f_forms,q);
  80. >>;
  81. f_forms;
  82. % extract coefficients of the f_forms in cl
  83. sys := cl$
  84. for each fr in f_forms do
  85. sys:=for each c in sys join coeff(c,fr);
  86. % and eliminate zeros
  87. sys := for each c in sys join if c neq 0 then {c} else {};
  88. end;