Create some simple tests of fixed points
jlperla opened this issue · comments
@jasmineyang This is to get started on #144 Depending on the preferred workflow you discussed with @pkofod this could work, or maybe a branch like in #146 if he prefers. In either case, I would like to be able to track (and work on this) with you.
First, we should add in a file fixed_points.jl
in https://github.com/JuliaNLSolvers/NLsolve.jl/tree/master/test where we will add in the tests.
Take the following code to get started, which we will replace.
function fixedpoint(f, x0; residualnorm = (x -> norm(x,Inf)), tol = 1E-10, maxiter=100)
residual = Inf
iter = 1
xold = x0
while residual > tol && iter < maxiter
xnew = f(xold)
residual = residualnorm(xold - xnew);
xold = xnew
iter += 1
end
return (xold,iter)
end
function fixedpoint!(f!, x0; residualnorm = (x -> norm(x,Inf)), tol = 1E-10, maxiter=100)
residual = Inf
iter = 1
xold = x0
xnew = copy(x0)
while residual > tol && iter < maxiter
f!(xold, xnew)
residual = residualnorm(xold - xnew);
xold = copy(xnew)
iter += 1
end
return (xold,iter)
end
To get started, at the top of the file, which we will replace as soon as possible with convenience functions using the NLsolve.jl functions more directly.
After that, we should start adding in some simple tests:
- Let the map be
$f(x) = \alpha x + 1$ for a scalar$x$ . Add in a test (usingfixedpoint
rather thanfixedpoint!
) to find the fixed point of this (which is the solution to$x^* = 1/(1-\alpha)$ for$|\alpha|<1$ .
fixedpoint(x -> 0.5x + 1, 1.0)
- Take an
$\alpha > 1$ and ensure that it fails correctly withiter == maxiter
or something like that.
fixedpoint(x -> 1.1x + 1, 1.0)
Next, lets look at a slightly less trivial mapping.
- Do a matrix valued function like
A = [0.5 0; 0 0.2]
b = [1.0; 2.0]
f(x) = A * x + b #A matrix equation
fixedpoint(f,[1.0; 0.1])
- Do an inplace matrix valued function
A = [0.5 0; 0 0.2]
b = [1.0; 2.0]
function f!(x, fx)
fx[:] = A * x + b
end
fixedpoint!(f!,[1.0; 0.2])
- Do a variation of this using https://github.com/JuliaArrays/StaticArrays.jl (you will not be able to do an inplace one with staticarrays, which is fine).
There are several ways to do this, but the "standard" workflow is simply to create a fork and work in that. If @jasmineyang forks this repository, she can make a branch there, and make a PR against this repository. You will still be able to see her changes in her repository, and you can even make PRs against her branch on her fork.
If this is confusing let me know, and I can either guide you through it, or we can set up something that is easier for you to work on.
Closing since these seem to work.