Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Roots package complex roots

$
0
0

I am using the Halley’s method in the Roots.jl package and I have given a MWE below.
I would like to clarity two points:
1. How can I specify that if roots are complex then it should return NaN?
2. I would like to amend the function to use Halley method first, and if it fails to converge then it should switch to Schroder’s method or vice versa. What changes I need to make to the code?

using Roots
function root_func(c;method=Roots.Halley(),guess=0.01,max_evals=100)
    t = collect(0:length(c)-1)
    f(x) = sum(@views c[i]/(1+x)^t[i] for i in 1:length(c))
    #find derivatives of f(x)
    df(x) = sum(@views -t[i]*c[i]/(1+x)^(t[i]+1) for i in 1:length(c))
    ddf(x) = sum(@views (t[i]^2+t[i])*c[i]/(1+x)^(t[i]+2) for i in 1:length(c))
    return find_zero((f,df,ddf),guess,method;maxevals=max_evals)
end

Example usage:
root_func([-100,10,100,1,1]
root_func([-100,10,100,1,1];method=Roots.Schroder(),guess=0.01,max_evals=100)

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 2795

Latest Images

Trending Articles



Latest Images