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

How to solve equations given in a vector form? #218

Closed
homocomputeris opened this issue Jun 23, 2019 · 7 comments
Closed

How to solve equations given in a vector form? #218

homocomputeris opened this issue Jun 23, 2019 · 7 comments

Comments

@homocomputeris
Copy link

Let's have a simple example which can be coded directly:

using NLsolve
function f!(F, x)
    F[1] = x[1]^2 - 4.0
    F[2] = x[2]^2 - 4.0
end

nlsolve(f!, [4.0; 4.0], autodiff=:forward)

is as expected:

Results of Nonlinear Solver Algorithm

  • Algorithm: Trust-region with dogleg and autoscaling
  • Starting Point: [4.0, 4.0]
  • Zero: [2.0, 2.0]
  • Inf-norm of residuals: 0.000000
  • Iterations: 5
  • Convergence: true
    • |x - x'| < 0.0e+00: false
    • |f(x)| < 1.0e-08: true
  • Function Calls (f): 6
  • Jacobian Calls (df/dx): 6

If my vector x has dimension, say, 100, I don't want to code the function elementwise. If I don't miss anything in Julia syntax, it would be

function g!(G, x)
    G = x.^2 - [4.0; 4.0]
end

nlsolve(g!, [4.0; 4.0], autodiff=:forward)

which is incorrect:

Results of Nonlinear Solver Algorithm

  • Algorithm: Trust-region with dogleg and autoscaling
  • Starting Point: [4.0, 4.0]
  • Zero: [4.0, 4.0]
  • Inf-norm of residuals: 0.000000
  • Iterations: 0
  • Convergence: true
    • |x - x'| < 0.0e+00: false
    • |f(x)| < 1.0e-08: true
  • Function Calls (f): 1
  • Jacobian Calls (df/dx): 1

How do I define the equation in vector form and get a valid result?

@ChrisRackauckas
Copy link
Contributor

mutate: G .=

@homocomputeris
Copy link
Author

@ChrisRackauckas Thanks, the syntax keeps surprising me.

An opposite question: can the package solve scalar equations like

function g!(G, x)
    G = 4.0 - x*2
end
nlsolve(g!, 1.0, autodiff=:forward)

I get

MethodError: no method matching nlsolve(::typeof(g!), ::Float64; autodiff=:forward)

@ChrisRackauckas
Copy link
Contributor

NLsolve.jl doesn't do scalar equations. Though Roots.jl is all about scalar rootfinding.

@homocomputeris
Copy link
Author

Thanks, Chris!

@fredrikekre
Copy link

You can put your scalar equation in a length-1 vector and solve though.

@pkofod
Copy link
Member

pkofod commented Jun 24, 2019

@ChrisRackauckas
Copy link
Contributor

You can put your scalar equation in a length-1 vector and solve though.

Yeah, but that's not really using scalars well though. It's a bit more expensive and it's not using methods that are tailored towards 1D, which is a much simpler problem. If someone is actually doing a bunch of 1D rootfinding problems, it's probably better to point them in another direction. If what they're doing is just a 1D test case and then straight to ND, then sure length-1 vector is fine.

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

4 participants