From: Richard F. <fa...@gm...> - 2023-06-20 20:51:08
|
This looks very nice! I was curious to see the definition of "iterate" in Maxima and was pleased that it was so simple. iterate(u,x,xo,n) := block( [l:[xo], numer:true, val:xo], for i thru n do (val: subst(val,x,u), l: cons(val,l)), reverse(l))$ Here are some alternatives! it3(u,x,x0,n):= cons(x0,makelist(x0:subst(x0,x,u),i,1,n-1)); or if you really need to set numer to true ... it3(u,x,x0,n):= block([numer:true], cons(x0,makelist(x0:subst(x0,x,u),i,1,n-1))); Another technique using more "functional" style programming requires that instead of providing both u=f(x), and x separately, you just use f, if it is the name of a function of one argument. This shortens the calling sequence. it4(fun,y0,n):= cons(y0, makelist(y0:apply(fun,[y0]),i,1,n-1)); or using recursion(!) it5(fun,y0,n):=if n=1 then [y0] else cons(y0, map(fun,it5(fun,y0,n-1)))$ compare it5(f,x0,4) to iterate(f(x),x,x0,4) Each of these returns [y0,f(y0),f(f(y0)),f(f(f(y0))),f(f(f(f(y0))))] maybe you would need ... it6(f,y0,n):= block([numer:true], it5(f,y0,n)); Here's another, using global variable names, which is considered bad style.. . it7(fun,y0,n):=block([numer:true],it7x(n))$ it7x(n):=if n = 0 then [y0] else cons(y0,map(fun,it7x(n-1)))$ As I assume you know, if there is not a name for the function f, but just an expression, one might need to do something like define(fun(x), f) or fun: lambda([x], f) Thanks for writing this book! I taught this subject from Boyce & diPrima in 1972 or so, and I think some of the methods built into the program ode2 were initially taken from that book. Regards Richard Fateman |