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

Bizarre Bug #241

Closed
janrpeters opened this issue Jun 8, 2020 · 2 comments
Closed

Bizarre Bug #241

janrpeters opened this issue Jun 8, 2020 · 2 comments

Comments

@janrpeters
Copy link

janrpeters commented Jun 8, 2020

I have a piece of illustrative code that works flawlessly:

using NLsolve;

f(x) = [x[2]; -9.81*sin(x[1])/16.0];

for i=1:1000
    x = [-0.5243754571784831; 0.5382977006541599]

    function f_root!(F_root, y)
        h = 0.01;
        w = f(y);
        F_root[1] = (y[1] - x[1])/h - w[1];
        F_root[2] = (y[2] - x[2])/h - w[2];
    end
    z = nlsolve(f_root!, x).zero 
    println("z[",i,"]=",z)
end

Surprisingly, vectorizing it yields a nondeterministic bug:

using NLsolve;

f(x) = [x[2]; -9.81*sin(x[1])/16.0];

for i=1:1000
    x = [-0.5243754571784831; 0.5382977006541599]

    function f_root!(F_root, y)
        h = 0.01;
        w = f(y);
        F_root = (y - x)./h - w;
    end
    z = nlsolve(f_root!, x).zero 
    println("z[",i,"]=",z)
end
@antoine-levitt
Copy link
Contributor

F_root = ... only changes the binding of F_root, not its value. Use F_root .=.

@janrpeters
Copy link
Author

Thanks!

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