# Basic solving equation with NLsove

**URL:** https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481
**Category:** New to Julia
**Created:** [August 7, 2020, 8:07am UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481 "2020-08-07T08:07:16Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [August 7, 2020, 8:07am UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/1 "2020-08-07T08:07:16Z")

</div>

Total Julia noob here.

So I would like to ultimately solve an equation f(x,a)=0 for x many times as I vary a and plot the solution as a function of a. Before getting to that though, I am first trying to learn to use NLsolve in a very simple case of the function f(x)=x+1.

I am a bit confused on the usage of NLsolve.  
Based on the example in the docs [GitHub - JuliaNLSolvers/NLsolve.jl: Julia solvers for systems of nonlinear equations and mixed complementarity problems](https://github.com/JuliaNLSolvers/NLsolve.jl) I try

```julia
function f!(F, x)
    F=x+1
end

function j!(J,x)
    J=1
end

NLsolve(f!, j!, 1)

```

(I am unsure of what to put in the third argument of NLsolve which I guess is the initial condition but I tried a couple things like 1 and [0.1; 1.2] which was in the example).

I get this error: [https://paste.gg/p/anonymous/d15bfc129c704142888eebc455d3c82c](https://paste.gg/p/anonymous/d15bfc129c704142888eebc455d3c82c)

I would like this to tell me that x=-1.  
Any recommendations on how to fix error?

Thanks!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 7, 2020, 12:46pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/2 "2020-08-07T12:46:38Z")

</div>

NLsolve uses vectors, so you would need to do something like `F[1] = x[1] + 1` and `J[1] = 1`. What you are doing just assigns new values locally.

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [August 7, 2020, 2:49pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/3 "2020-08-07T14:49:34Z")

</div>

Thanks. So now I try:

```julia
function f!(F, x)
    F[1]=x[1]+1
end

function j!(J,x)
    J[1]=1
end

NLsolve(f!, j!, 1)

```

I get:

MethodError: objects of type Module are not callable

Stacktrace:  
[1] top-level scope at In[9]:8

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [August 7, 2020, 2:59pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/4 "2020-08-07T14:59:04Z")

</div>

NLsolve is the name of the module.  
nlsolve is the function (all lower case).

The last argument should be [1], a single-element vector, since your function is 1-dimensional.

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [August 7, 2020, 3:15pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/6 "2020-08-07T15:15:16Z")

</div>

Looks like the argument should be [1.0] instead of the int [1], but that’s seems to work thanks.

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [August 7, 2020, 5:07pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/7 "2020-08-07T17:07:20Z")

</div>

But now I would like to get a parameter a so that I can solve:  
x+a=0 for different a’s.  
I try:

```julia

for a in range(-1, 1, length=9)
    # a = range(-1, 1, length=9)?
    # for i in length(a)?
    function f!(F, x)
        F[1]=x[1]+a
    end

    function j!(J,x)
        J[1]=1
    end

    nlsolve(f!, j!, 1.0) 
end

```

MethodError: no method matching nlsolve(::var"#f!#13"{Float64}, ::var"#j!#14", ::Float64)

---

<div class="post-metadata">

### Author: ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)
#### Post date: [August 7, 2020, 5:35pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/8 "2020-08-07T17:35:04Z")

</div>

Put the initial value in a vector: `nlsolve(f!, j!, [1.0])`

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [August 7, 2020, 6:19pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/9 "2020-08-07T18:19:33Z")

</div>

Sorry about last embarrassment.  
Now I have:

```julia
answer=zeros(9)
counter = 1
for a in range(-1, 1, length=9)
    

    function f!(F, x)
        F[1]=x[1]+a
    end

    function j!(J,x)
        J[1]=1
    end

    
    answer[counter]=nlsolve(f!, j!, [1.0]).zero
    counter = counter + 1
end

```

I get: MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Float64

I guess the type of nlsolve().zero is not compatible with the type of the entries from zeros() function. How can I convert?

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [August 7, 2020, 6:39pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/10 "2020-08-07T18:39:09Z")

</div>

Tracking the solution x of f(x,a) = 0 under variations in parameter a is a continuation problem. You might want to use a package that implements continuation algorithms and detects bifurcations in solution curves, like [GitHub - bifurcationkit/BifurcationKit.jl: A Julia package to perform Bifurcation Analysis](https://github.com/rveltz/BifurcationKit.jl)

---

<div class="post-metadata">

### Author: ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)
#### Post date: [August 7, 2020, 7:07pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/11 "2020-08-07T19:07:32Z")

</div>

That’s interesting but I’m worried [https://github.com/rveltz/BifurcationKit.jl](https://github.com/rveltz/BifurcationKit.jl) will be too complicated for me at this stage of my learning. I am trying to do super basic things.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 7, 2020, 7:09pm UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/12 "2020-08-07T19:09:53Z")

</div>

The same problem happens over and over. Nlsolve deals with vectors (multi-dimensional problems) and you keep treating inputs and outputs as scalars. Here you try to assign the result (a vector) to a scalar (`answer[counter]`)

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [August 8, 2020, 3:05am UTC](https://discourse.julialang.org/t/basic-solving-equation-with-nlsove/44481/13 "2020-08-08T03:05:10Z")

</div>

Try to run each command one by one in the REPL by hand, in order to be able to see what the output looks like, in particular what type of object it is.

Once you can run each line by hand and “run the loop by hand” then write the actual loop.
