Roots of a function: Difference between revisions
Content added Content deleted
m (→{{header|CoffeeScript}}: remove one line of dead code) |
(Add Axiom task) |
||
Line 169: | Line 169: | ||
Return ((x-3)*x+2)*x |
Return ((x-3)*x+2)*x |
||
}</lang> |
}</lang> |
||
=={{header|Axiom}}== |
|||
Using a polynomial solver: |
|||
<lang Axiom>expr := x^3-3*x^2+2*x |
|||
solve(expr,x)</lang> |
|||
Output: |
|||
<lang Axiom> (1) [x= 2,x= 1,x= 0] |
|||
Type: List(Equation(Fraction(Polynomial(Integer))))</lang> |
|||
Using the secant method in the interpreter: |
|||
<lang Axiom>digits(30) |
|||
secant(eq: Equation Expression Float, binding: SegmentBinding(Float)):Float == |
|||
eps := 1.0e-30 |
|||
expr := lhs eq - rhs eq |
|||
x := variable binding |
|||
seg := segment binding |
|||
x1 := lo seg |
|||
x2 := hi seg |
|||
fx1 := eval(expr, x=x1)::Float |
|||
abs(fx1)<eps => return x1 |
|||
for i in 1..100 repeat |
|||
fx2 := eval(expr, x=x2)::Float |
|||
abs(fx2)<eps => return x2 |
|||
(x1, fx1, x2) := (x2, fx2, x2 - fx2 * (x2 - x1) / (fx2 - fx1)) |
|||
error "Function not converging."</lang> |
|||
The example can now be called using: |
|||
<lang Axiom>secant(expr=0,x=-0.5..0.5)</lang> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |