[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Axiom-mail] Axiom programs which include functions?
From: |
Martin Rubey |
Subject: |
Re: [Axiom-mail] Axiom programs which include functions? |
Date: |
19 May 2008 17:01:11 +0200 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 |
"Alasdair McAndrew" <address@hidden> 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