Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Maxiters seems to be offset by 1 #63

Closed
KronosTheLate opened this issue Apr 28, 2022 · 3 comments
Closed

Maxiters seems to be offset by 1 #63

KronosTheLate opened this issue Apr 28, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@KronosTheLate
Copy link
Contributor

Check out the following example:

julia> using NonlinearSolve, LinearAlgebra

julia> f(u,p) = u .* u .- p;

julia> u0 = [1.0, 1.0];

julia> p = 2.0;

julia> probN = NonlinearProblem{false}(f, u0, p);

julia> bla0 = solve(probN, NewtonRaphson(), maxiters = 0).u;

julia> bla1 = solve(probN, NewtonRaphson(), maxiters = 1).u;

julia> bla2 = solve(probN, NewtonRaphson(), maxiters = 2).u;

julia> norm(f(u0, p))
1.4142135623730951

julia> norm(f(bla0, p))
1.4142135623730951

julia> norm(f(bla1, p))
1.4142135623730951

julia> norm(f(bla2, p))
0.3535533905932738

It seems as if setting maxter to 1 acutally results in zero iterations occuring. And I am left to wonder if maxter=2 then means 2 iterations, or actually 1 iteration...

This is the same for my actual application, which makes me more certain that the issue is not specific to this problem. The other example is:

julia> using NonlinearSolve, LinearAlgebra

julia> v = 120;  #kg

julia> k = 2.5;  #m

julia> w = 4.0;  #km/m

julia> α = 2e-7; #kg⁻¹

julia> function F(x⃗, parameters)
           L₀, L, p, x, θ, φ, a, H = x⃗
           (;d, n) = parameters
           return [
           a*(cosh(x/a)-1) - p,
           2a * sinh(x/a) - L ,
           2x + 2k*cos(θ) - d,
           p+k*sin(θ) - n,
           sinh(x/a) - tan(φ),
           (1+v/(w*L₀)) * tan(φ) - tan(θ),
           L₀ * (1 + α*H) - L,
           w*L₀ / 2sin(φ) - H,
           ]
       end;

julia> params = (d=30, n=5);

julia> u0 =    [29, 27, 1, params.d/2, deg2rad(45),  deg2rad(22.5), 40, 500];

julia> probN = NonlinearProblem{false}(F, u0, params);

julia> bla0 = solve(probN, NewtonRaphson(), maxiters = 0).u;

julia> bla1 = solve(probN, NewtonRaphson(), maxiters = 1).u;

julia> bla2 = solve(probN, NewtonRaphson(), maxiters = 2).u;

julia> norm(F(u0, params))
348.4941911206226

julia> norm(F(bla0, params))
348.4941911206226

julia> norm(F(bla1, params))
348.4941911206226

julia> norm(F(bla2, params))
3.865006154302766
@KronosTheLate
Copy link
Contributor Author

Some investigations into number of function calls:

julia> using NonlinearSolve, LinearAlgebra

julia> function f(u,p)
           p[2] += 1  # This counts function calls
           u .* u .- p[1]
       end
f (generic function with 1 method)

julia> u0 = [1.0, 1.0];

julia> p = [2.0, 0];

julia> probN = NonlinearProblem{false}(f, u0, p);

julia> p[2]
0.0

julia> bla0 = solve(probN, NewtonRaphson(), maxiters = 0).u;

julia> p[2]
1.0

julia> bla1 = solve(probN, NewtonRaphson(), maxiters = 1).u;

julia> p[2]
2.0

julia> bla2 = solve(probN, NewtonRaphson(), maxiters = 2).u;

julia> p[2]
5.0

I do not get why the calls that do nothing end up calling the function. Nor do I get why computing bla2 has 3 function calls - does it not only need 2 to compute a gradient? My intuition is that those numbers should all be 1 lower. But then again, I do not have good understanding of the details of the algorithms. So this is mostly to inform whoever might take a look at this.

@ChrisRackauckas
Copy link
Member

Did this get fixed?

@ChrisRackauckas
Copy link
Member

Fixed by #203

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants