ApproxFun.jl icon indicating copy to clipboard operation
ApproxFun.jl copied to clipboard

Error in Newton Iteration

Open MasonProtter opened this issue 4 years ago • 3 comments

I tried adapting Nonlinear Boundary Value problems example from the README to a BVP I was interested in and his this error:

using ApproxFun, Plots

@time let
  rmin = 0.01
  rmax = 5.0
  κ = 10.0

  r = Fun(identity, rmin..rmax)
  
  N = (f, h) -> [
    h'(rmin)/f(rmin)^2 + 1/(κ*rmin);
    f(rmax) - 1;
    h(rmax);
    h'(rmax);
    f'' + f'/r + κ^2 * ((h'')^2/(f^3) - f*(1 - f^2));
    h'' + h'*(1/r - 2/f) - f^2*h
  ]

  f0 = tanh(r)
  h0 = tanh(r)
  f, h = newton(N, [f0, h0])
end

#+RESULTS:
MethodError: no method matching +(::Fun{Ultraspherical{Int64, IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}})
Closest candidates are:
  +(::Fun, !Matched::UniformScaling) at /home/mason/.julia/packages/ApproxFunBase/dn7g5/src/Fun.jl:309
  +(::Fun{S, T, VT} where VT, !Matched::T) where {S, T<:Number} at /home/mason/.julia/packages/ApproxFunBase/dn7g5/src/Fun.jl:307
  +(::Fun, !Matched::Number) at /home/mason/.julia/packages/ApproxFunBase/dn7g5/src/Fun.jl:308
  ...

Stacktrace:
 [1] +(a::Fun{Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}, b::ApproxFun.DualFun{Fun{Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}, Fun{Ultraspherical{Int64, IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}})
   @ ApproxFun ~/.julia/packages/ApproxFun/2jR1D/src/Extras/autodifferentiation.jl:43
 [2] +
   @ ./operators.jl:540 [inlined]
 [3] (::var"#153#154"{Fun{Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}, Float64, Float64, Float64})(f::Fun{Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}, h::ApproxFun.DualFun{Fun{Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}, ConstantOperator{Float64, Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}}})
   @ Main ./In[78]:11
 [4] newton(N::Function, u0::Vector{Fun{Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}}; maxiterations::Int64, tolerance::Float64)
   @ ApproxFun ~/.julia/packages/ApproxFun/2jR1D/src/Extras/autodifferentiation.jl:142
 [5] newton(N::Function, u0::Vector{Fun{Chebyshev{IntervalSets.ClosedInterval{Float64}, Float64}, Float64, Vector{Float64}}})
   @ ApproxFun ~/.julia/packages/ApproxFun/2jR1D/src/Extras/autodifferentiation.jl:121
 [6] macro expansion
   @ In[78]:22 [inlined]
 [7] top-level scope
   @ timing.jl:207
 [8] eval
   @ ./boot.jl:360 [inlined]
 [9] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1090

The examples in the README worked fine though.

MasonProtter avatar Jan 12 '21 17:01 MasonProtter

N is a function of two arguments but it seems like you actually have a vector-valued case...

not sure vector-valued Jacobians work

dlfivefifty avatar Jan 12 '21 19:01 dlfivefifty

Isn't that also the case with the examples in the README though? E.g.

x=Fun(identity, 0..1)
N = (u1,u2) -> [u1'(0) - 0.5*u1(0)*u2(0);
                u2'(0) + 1;
                u1(1) - 1;
                u2(1) - 1;
                u1'' + u1*u2;
                u2'' - u1*u2]

u10 = one(x)
u20 = one(x)
u1,u2 = newton(N, [u10,u20])

MasonProtter avatar Jan 12 '21 20:01 MasonProtter

Er it's even simpler: try adding

+(a::Fun) = copy(a)

dlfivefifty avatar Jan 12 '21 20:01 dlfivefifty

I suspect this issue is resolved now, but the original example takes far too long to run on my system. I'm closing this, but you are welcome to reopen if the issue persists.

jishnub avatar Apr 20 '23 13:04 jishnub