Grid Search Algorithm


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.

Hi!, welcome to the julia community!
there is packages that can do that for you, for example:
you can directly derivate using ForwardDiff (better than numerical aproximation and faster than symbolic derivation)
if you are looking for the univariate newton algorithm, there is 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?


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:

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
    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
    return xiplus1

have a look at my package

it uses my package Reduce.jl for symbolic differentiation

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

Is this a homework problem?

1 Like