2
$\begingroup$

i am trying to implement a recursive difference equation step by step, by implementing rules, similar to Rojo's method in this thread: How can I evaluate only a single step of a recursive function?

But the rules are not applied to my function, I think because it contains more than one variable. This is important for the answer, since one of the variables is integrated over. The other variable is decremented, which is what I want to iterate step-wise:

Clear[f, frules];
frules = {f[n_, x_] -> (n - 1) x^n Cos[x] f[n - 1, x] + Integrate[Sin[x] f[n - 1, x], x]};
f[3] /. frules
f[3] /. frules /. frules
f[m] /. frules

This just gives $f[3]$, or $f[m]$, without implementing the recursion.

I'm specifically interested in the symbolic expressions after 1,2,...p steps. How to start from a given integer value $m$ and write the expression after $p$ decrements of 1?

The mathematical expression should be well posed because I can incrementally increment the function, with a given starting value:

f[n_, x_] := (n - 1) x^n Cos[x] f[n - 1, x] + Integrate[Sin[x] f[n - 1, x], x]
f[0, x] = 1;

Gives well defined functions for f[1], f[2], f[3], etc. E.g. $f[1] = -Cos[x]$ , $f[2] = \frac{1}{2} (1 - 2 x^2) Cos[x]^2 $ , etc.

$\endgroup$

1 Answer 1

3
$\begingroup$

The rule won't fire because of the signature issue (f[3] vs. f[3,x]). And there will be no end to the recursion as currently configured.

It's not clear what are wanted for the edge cases where n is less than 2 or the number of steps to take is 0. Taking a couple of guesses, it could be set up like so. I use a third argument to give the number of steps for the recursion. Also this uses memoization to avoid reevaluations.

myf[2, x_, _] = x^2* Cos[x];

myf[n_Integer, x_, k_Integer] /; n > 2 && k >= 1 := 
 With[{next = myf[n - 1, x, k - 1]}, 
  myf[n, x, k] = (n - 1)* x^n* Cos[x]* next + 
    Integrate[Sin[x]*next, x]]

Here are a few steps for the case n=5. More steps will not change the result since the last one bottomed out on the recursion.

In[59]:= Table[myf[5, x, j], {j, 0, 3}]

(* Out[59]= {myf[5, x, 
  0], \[Integral]myf[4, x, 0] Sin[x] \[DifferentialD]x + 
  4 x^5 Cos[x] myf[4, x, 
    0], \[Integral](\[Integral]myf[3, x, 0] Sin[
           x] \[DifferentialD]x + 3 x^4 Cos[x] myf[3, x, 0]) Sin[
      x] \[DifferentialD]x + 
  4 x^5 Cos[
    x] (\[Integral]myf[3, x, 0] Sin[x] \[DifferentialD]x + 
     3 x^4 Cos[x] myf[3, x, 0]), 
 30 x + (3 x^2)/32 - 5 x^3 + x^5/4 + x^6/32 - (13 Cos[x]^2)/54 - (1/
  1296)(99 + 1341200 x - 54 x^2 - 913440 x^3 + 183600 x^5 - 
     17496 x^7 + 972 x^9) Cos[2 x] - (3367 Cos[4 x])/442368 - (
  626425 x Cos[4 x])/663552 + (751 x^2 Cos[4 x])/6144 + (
  73585 x^3 Cos[4 x])/27648 - 87/512 x^4 Cos[4 x] - 
  1685/768 x^5 Cos[4 x] + 3/64 x^6 Cos[4 x] + 27/32 x^7 Cos[4 x] - 
  3/16 x^9 Cos[4 x] + 6365/9 x^4 Cos[x] Sin[x] + 
  244195/486 Sin[2 x] - 11/72 x Sin[2 x] - 28345/27 x^2 Sin[2 x] - 
  189/4 x^6 Sin[2 x] + 27/8 x^8 Sin[2 x] + 
  4 x^5 Cos[
    x] (-(1/16) (-7 + 960 x + 2 x^2 - 160 x^3 + 8 x^5) Cos[
       x] - ((57 + 320 x - 54 x^2 - 480 x^3 + 216 x^5) Cos[3 x])/
     1296 + 60 Sin[x] + 3/8 x Sin[x] - 30 x^2 Sin[x] + 
     5/2 x^4 Sin[x] + 
     3 x^4 Cos[
       x] (2 x^5 Cos[x]^2 - 1/8 (-1 + 2 x^2) Cos[2 x] + 
        1/4 x Sin[2 x]) + 20/243 Sin[3 x] - 5/72 x Sin[3 x] - 
     10/27 x^2 Sin[3 x] + 5/18 x^4 Sin[3 x]) + (1797355 Sin[4 x])/
  7962624 - (1933 x Sin[4 x])/36864 - (215635 x^2 Sin[4 x])/110592 + 
  87/512 x^3 Sin[4 x] + (24955 x^4 Sin[4 x])/9216 - 
  15/128 x^5 Sin[4 x] - 189/128 x^6 Sin[4 x] + 27/64 x^8 Sin[4 x]} *)

I hope this is along the lines of what you are seeking.

$\endgroup$
4
  • $\begingroup$ Thanks Daniel, I think that basically solves it. I wish I checked my syntax before posting, because my code actually worked fine after I changed f[3] to f[3,x]. Your code did the decrement and left the answer in terms of an integral which is what I was after (which my code also does after fixing). Your code also automates the decrement procedure a variable $k$ times, unlike my manual implementation. Thanks and I'll accept. As a side note, I noticed that the answer your code gives, and the answer Bob Hanlon's code gives below are different, though so far as I can tell the input is the same?.. $\endgroup$
    – StevieP
    Jul 13, 2022 at 20:29
  • $\begingroup$ The code from Bob Hanlon has a different initial condition. I had missed this condition in my original reading of the question. $\endgroup$ Jul 13, 2022 at 21:16
  • $\begingroup$ I think there's an issue here! Here is your code, and @BobHanlon 's code, for the same initial (boundary) condition, and the same n=2: myf[0, x_, _] = 1; myf[n_Integer, x_, k_Integer] /; k >= 1 := With[{next = myf[n - 1, x, k - 1]}, myf[n, x, k] = (n - 1)*x^n*Cos[x]*next + Integrate[Sin[x]*next, x]]; myf[2, x, 2] Part[(seq = RecurrenceTable[{f[n] == (n - 1) x^n Cos[x] f[n - 1] + Integrate[Sin[x] f[n - 1], x], f[0] == 1}, f, {n, 2, 2}] // FullSimplify) , 1] They really do give different results for the function, and not by a constant... $\endgroup$
    – StevieP
    Jul 14, 2022 at 2:04
  • $\begingroup$ I.e. one gives $Cos[x]^2/2 - x^2 Cos[x]^2$ , and the other gives $-(-1 + x^2) Cos[x]^2$ $\endgroup$
    – StevieP
    Jul 14, 2022 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.