Re: Axiom programs which include functions?
Martin Rubey <[email protected]>
| Newsgroups | gmane.comp.mathematics.axiom.user |
|---|---|
| Message-ID | <[email protected]> |
"Alasdair McAndrew" <[email protected]> writes: > Hi, > > I can write (simple) Axiom programs for which the parameters are all > numbers, but what if the parameters include a function? Suppose I wished to > write a program to, say, solve the equation f(x)=0 by the bisection method, > and I wanted to call it as > > bisect(f,a,b) > > where [a,b] brackets a solution. How do I do this? And in what form does > Axiom like its functions in such a program? I don't think there is anything that could go wrong. You can specify an anonymous function for example with x +-> x^2-3*x+1 panAxiom doesn't like to do type inference on anonymous functions too much, so the code will often be interpreted, if you do not specify types: (1) -> bisect(f, a, b, eps) == (output [a,b]; if abs(fa := f a) < eps then a else if abs(fb := f b) < eps then b else if sign fa = sign fb then error "same sign" else if abs(fz := f(z := ((a+b)/2))) < eps then z else if sign fz = sign fa then bisect(f, z, b, eps) else bisect(f, a, z, eps)) Type: Void (2) -> (x +-> x^2-3*x+1) bisect(x +-> x^2-3*x+1, -2, 0.5, 0.05) FriCAS will attempt to step through and interpret the code. [- 2.0,0.5] [- 0.75,0.5] [- 0.125,0.5] [0.1875,0.5] [0.34375,0.5] [0.34375,0.421875] (2) - 0.0018920898 4375 Type: Float Does this help? Martin