invr{                          ⍝ Approx inverse of real-valued function.
    0::'no inverse'⎕SIGNAL ⎕EN  ⍝ pass back domain error.
    ⍺←1+⎕CT+0×⍵                 ⍝ initial guess - slightly more than 1.
    ∆x←⎕CT*÷2                   ⍝ increment delta-x.
    -∘⍵∘⍺⍺{                     ⍝ Newton-Raphson: find root of ⍺⍺(x)-⍵ = 0.
        ⍵⍵ ⍵:⍵                  ⍝ all items within ⎕ct of inverse: finished.
        y y∆←⍺⍺¨0 ∆x+⊂⍵         ⍝ f(x) f(x+∆x)
        ∇ ⍵-y×∆x÷y∆-y           ⍝ refined estimate.
    }(⍵∘≡∘⍺⍺)⍺                  ⍝ starting from best guess.
}

code_colours

test script

Back to: notes

Back to: Workspaces