Download or view NewtonsMethod.frink in plain text format
use functionUtils.frink
use allTransforms.frink
/** This program tries to help you find a Newton's method iteration for a
particular expression. */
NewtonsMethod[f] :=
{
body = functionBody[f]
df = transformExpression[makeDerivative[f]]
vars = functionArgumentsAsSymbols[f]
// println["f = " + inputForm[body]]
// println["df = " + inputForm[df]]
return transformExpression[vars@0 - body/df]
}
/** This program tries to help you find a Halley's method iteration for a
particular function . */
HalleysMethod[f] :=
{
body = functionBody[f]
df = transformExpression[makeDerivative[f]]
df2 = transformExpression[makeDerivative[f, 2]]
vars = functionArgumentsAsSymbols[f]
// println["f = " + inputForm[body]]
println["df = " + inputForm[df]]
println["df2 = " + inputForm[df2]]
return transformExpression[vars@0 - 2 body df / (2 df^2 - df df2)]
}
symbolicMode[true]
/** Newton's method solves for the roots or zeroes of a function f, that is,
where
f[x]=0
The below creates a two-argument anonymous function that implicitly
encodes the condition
x^2 - n = 0
or, alternately,
x^2 = n
or, alternately,
x = sqrt[n]
which is what we really want to be solving in this case.
We want to find a way to transform the equation we want to be solving, e.g.
x = sqrt[n]
into the form that Newton's method implicitly expects. Which appears to
be solving for n, (e.g. n = x^2) and then transforming that into
(x^2 - n) === 0 or (n - x^2) === 0
*/
f = {|x, n| x^2 - n}
println["The next term (Newton's Method) is given by iterating: "]
println[inputForm[NewtonsMethod[f]]]
println[]
println["The next term (Halley's method) is given by iterating: "]
println[inputForm[HalleysMethod[f]]]
Download or view NewtonsMethod.frink in plain text format
This is a program written in the programming language Frink.
For more information, view the Frink
Documentation or see More Sample Frink Programs.
Alan Eliasen was born 20217 days, 23 hours, 51 minutes ago.