# Grid Search Algorithm

**URL:** <https://discourse.julialang.org/t/grid-search-algorithm/23121>\
**Category:** New to Julia\
**Created:** [April 13, 2019, 8:54pm UTC](https://discourse.julialang.org/t/grid-search-algorithm/23121 "2019-04-13T20:54:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sasquatch](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@Sasquatch](https://discourse.julialang.org/u/Sasquatch)\
**Post date:** [April 13, 2019, 8:54pm UTC](https://discourse.julialang.org/t/grid-search-algorithm/23121/1 "2019-04-13T20:54:42Z")

</div>

Hello,

I’m really new to programming and want to create a programm that uses the newton method to find the zero points of a function. To do that I need the differentiated function. I’m supposed to differentiate numerically by using f(x)-f(x-1)/x-(x-1). I have no idea how im supposed to implement this into Julia. Any help would be much appreciated. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 13, 2019, 9:40pm UTC](https://discourse.julialang.org/t/grid-search-algorithm/23121/2 "2019-04-13T21:40:46Z")

</div>

Hi!, welcome to the julia community!  
there is packages that can do that for you, for example:  
you can directly derivate using [ForwardDiff](https://github.com/JuliaDiff/ForwardDiff.jl) (better than numerical aproximation and faster than symbolic derivation)  
if you are looking for the univariate newton algorithm, there is [Roots.jl](https://github.com/JuliaMath/Roots.jl), it has a lot of algorithms, including the newton one.  
if you need to implement this yourself as a part of a course, we can help you too, do you have something in mind?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 13, 2019, 9:48pm UTC](https://discourse.julialang.org/t/grid-search-algorithm/23121/3 "2019-04-13T21:48:06Z")

</div>

the grid search if i remember correctly implies finding zeros of a multivariate function in one direction iteratively, here is a newton implementation in julia, that is one part of the grid search algorithm:

```julia
function mynewton(f,df,x0,tol=1e-08,itermax=100) #here i give optional
# arguments, so later i don't need to write it in the function
    xiplus1 = zero(eltype(x0)) #basically 0.0, but more adaptable
    #good to remember that 1 and 1.0 are of distinct type, the first one is
    #an integer, the second one is a float
    xi = x0
    #main loop
    tolf=1000
    iter_count=0
    while (tolf>=tol) && (iter_count<=itermax) #the loop stops
        # if the tolerance or the maximum number of iterations is achieved
        xiplus1 = xi - f(xi)/df(xi)
        tolf = abs(f(xiplus1))
        iter_count+=1 #it adds 1 to the variable
        xi = xiplus1
    end
    return xiplus1
end

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 13, 2019, 11:09pm UTC](https://discourse.julialang.org/t/grid-search-algorithm/23121/4 "2019-04-13T23:09:44Z")

</div>

have a look at my package

[https://github.com/chakravala/Fatou.jl](https://github.com/chakravala/Fatou.jl)

it uses my package [Reduce.jl](https://github.com/chakravala/Reduce.jl) for symbolic differentiation

unless you have some really weird function, I’d recommend a symbolic over finite difference

---

<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:** [April 14, 2019, 5:40am UTC](https://discourse.julialang.org/t/grid-search-algorithm/23121/5 "2019-04-14T05:40:13Z")

</div>

> [@Sasquatch](#):
>
> I’m supposed to differentiate numerically by using f(x)-f(x-1)/x-(x-1).

Is this a homework problem?
