# Solving system of equations

**URL:** https://discourse.julialang.org/t/solving-system-of-equations/27691
**Category:** New to Julia
**Created:** [August 18, 2019, 7:42pm UTC](https://discourse.julialang.org/t/solving-system-of-equations/27691 "2019-08-18T19:42:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![zaccazy](https://avatars.discourse-cdn.com/v4/letter/z/5e9695/32.png) [@zaccazy](https://discourse.julialang.org/u/zaccazy)
#### Post date: [August 18, 2019, 7:42pm UTC](https://discourse.julialang.org/t/solving-system-of-equations/27691/1 "2019-08-18T19:42:19Z")

</div>

Hi. I’m interested in ‘nlsolve’ function for solving system of equations.

Let us consider simple example:  
Eq.1: y=a+x  
Eq.2: y=2x-1,  
where a is a vector that it can take values 3 and 5.

I want to solve system of equations for each value of a.  
For a=3, (x,y)=(4,7), and for a=5, (x,y)=(6,11).

I solve it in a loop:

using NLsolve

function f!(F, x, a)  
F[1] =a+x[1]-x[2]  
F[2] =2\*x[1]-1-x[2]  
end

K=zeros(2, length(a))  
initial\_x=[0.1; 0.2]

a=[3.0; 5.0]

for i=1:length(a)  
res=nlsolve((F,x) → f!(F, x, a[i]), initial\_x)  
K[:,i]=res.zero  
end

Is there any smart way to avoid looping?

Also, I would appreciate if you can explain the helper function `n_ary`.  
Thank you.

---

<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 18, 2019, 9:17pm UTC](https://discourse.julialang.org/t/solving-system-of-equations/27691/2 "2019-08-18T21:17:03Z")

</div>

This is just a system of linear equations so there is no need for NLSolve. Assuming it is just a simple example, what is wrong with writing a loop?

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [August 19, 2019, 12:32am UTC](https://discourse.julialang.org/t/solving-system-of-equations/27691/3 "2019-08-19T00:32:58Z")

</div>

Welcome to the Julia community! I can’t help with your particular issue, but it might be worth taking a look at [this thread](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) about posting questions here in discourse. In particular, you can get the code formatted, making it a bit easier to read. Compare

function foo()  
println(“this looks like normal text”)  
end

To:

```julia
function bar()
    println("oh, so much better!")
end

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [August 20, 2019, 9:43am UTC](https://discourse.julialang.org/t/solving-system-of-equations/27691/4 "2019-08-20T09:43:50Z")

</div>

If it’s just about avoiding this loop:

> [@zaccazy](#):
>
> ```julia
> a=[3.0; 5.0]
> 
> for i = 1:length(a)
> res = nlsolve((F,x) -> f!(F, x, a[i]), initial_x)
> K[:,i] = res.zero
> end
> 
> ```

then I guess you could do

```julia
res = [nlsolve((F,x) -> f!(F, x, aᵢ), initial_x) for aᵢ in a]
K = [r.zero for r in res]

```

But I’m not sure that’s an improvement over the loop? Loops in Julia are generally as fast as “vectorized” code, so in case you’re coming from Matlab/R or a similar environment where vectorization is the key to fast code you can stop worrying!
