Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Error root #173

Open
jmarcellopereira opened this issue Dec 28, 2024 · 5 comments
Open

Error root #173

jmarcellopereira opened this issue Dec 28, 2024 · 5 comments

Comments

@jmarcellopereira
Copy link

jmarcellopereira commented Dec 28, 2024

An error occurs when using Roots.jl to solve an equation, the result should be -2.0, not -1.58.

using Measurements, Roots

a = measurement(1.0  , 0.1)
b = measurement(-3.0 , 0.1)
c = measurement(-10.0, 0.1)

f(x) = a*x^2 + b*x + c

find_zero(f, measurement(0.0, 0.1))

−1.58±0.12
@giordano
Copy link
Member

Why not opening an issue in Roots.jl? The root finding algorithm is defined there, not here, f returns correct values:

julia> using Measurements

julia> a = measurement(1.0  , 0.1)
1.0 ± 0.1

julia> b = measurement(-3.0 , 0.1)
-3.0 ± 0.1

julia> c = measurement(-10.0, 0.1)
-10.0 ± 0.1

julia> f(x) = a*x^2 + b*x + c
f (generic function with 2 methods)

julia> f(-2.0)
0.0 ± 0.46

julia> f(-1.58)
-2.76 ± 0.31

@giordano
Copy link
Member

giordano commented Dec 28, 2024

I tried to reduce the code to

using Roots, Measurements

using Measurements, Roots

a = measurement(1.0  , 0.1)
b = measurement(-3.0 , 0.1)
c = measurement(-10.0, 0.1)
x0 = measurement(0.0, 0.1)

f(x) = a*x^2 + b*x + c
# Compare with function which doesn't have uncertainties
g(x) = Measurements.value(a*x^2 + b*x + c)

𝑭𝑿 = ZeroProblem(f, x0)
𝑮𝑿 = ZeroProblem(g, x0)
M = Order0()
p = nothing
verbose = false
tracks = Roots.NullTracks()

Zf = Roots.init(𝑭𝑿, M, p; verbose, tracks)
solve!(Zf; verbose)
Zg = Roots.init(𝑮𝑿, M, p; verbose, tracks)
solve!(Zg; verbose)

but now I'm lost in Roots.jl code. I'm not going to look into that, I'm not familiar with it and it looks quite complicated. Unless @jverzani is going to help I'll close this issue as I don't know what to do here.

@giordano
Copy link
Member

Perhaps one relevant thing is that

julia> iszero(measurement(0, 0.1))
false

julia> measurement(0, 0.1) == 0
false

julia> isequal(measurement(0, 0.1), 0)
false

which may screw up some logic in Roots.jl, but the thing is that measurement(0, err) with non-zero err is not the neutral element of addition (that's measurement(0, 0)), which is the contract of the iszero function, so I'm not going to change that.

@jmarcellopereira
Copy link
Author

Why not opening an issue in Roots.jl? The root finding algorithm is defined there, not here, f returns correct values:

julia> using Measurements

julia> a = measurement(1.0 , 0.1)
1.0 ± 0.1

julia> b = measurement(-3.0 , 0.1)
-3.0 ± 0.1

julia> c = measurement(-10.0, 0.1)
-10.0 ± 0.1

julia> f(x) = ax^2 + bx + c
f (generic function with 2 methods)

julia> f(-2.0)
0.0 ± 0.46

julia> f(-1.58)
-2.76 ± 0.31

Hello Giordano. I forgot to comment: the error in the value only appears when the uncertainties are declared.

@jmarcellopereira
Copy link
Author

I tried to reduce the code to

using Roots, Measurements

using Measurements, Roots

a = measurement(1.0 , 0.1)
b = measurement(-3.0 , 0.1)
c = measurement(-10.0, 0.1)
x0 = measurement(0.0, 0.1)

f(x) = ax^2 + bx + c

Compare with function which doesn't have uncertainties

g(x) = Measurements.value(ax^2 + bx + c)

𝑭𝑿 = ZeroProblem(f, x0)
𝑮𝑿 = ZeroProblem(g, x0)
M = Order0()
p = nothing
verbose = false
tracks = Roots.NullTracks()

Zf = Roots.init(𝑭𝑿, M, p; verbose, tracks)
solve!(Zf; verbose)
Zg = Roots.init(𝑮𝑿, M, p; verbose, tracks)
solve!(Zg; verbose)
but now I'm lost in Roots.jl code. I'm not going to look into that, I'm not familiar with it and it looks quite complicated. Unless @jverzani is going to help I'll close this issue as I don't know what to do here.

Okay, I understand. The result was consistent and so were the uncertainties. Thank you for your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants