Passing functions as parameters



On 7/5/2012 12:36 PM, Jorge Calvo wrote:
> Hello there:
>
> I am trying to define a function which would take the name of another function as a parameter and use it somehow.
There is a newton iteration ; try load(newton1);
It looks like this..

newton(exp,var,x0,eps):=
     block([xn,s,numer],
     numer:true,
     s:diff(exp,var),
     xn:x0,
   loop, if abs(subst(xn,var,exp))<eps then return(xn),
     xn:xn-subst(xn,var,exp)/subst(xn,var,s),
     go(loop) )$

so it is using expressions and subst, rather than functions.

>   My goal is to implement the bisection, secant, and Newton's method procedures, but at the moment
....

using apply is fine.

You can pass a function like this, too..

w:  lambda([x], sin(x)^2*exp(x)

and pass it to  your functions.   apply will still work.

Some people use funny names for parameters and hope for the best...
e.g.

secant(%%f, ....) :=    $$f(x)....