# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import matplotlib.pyplot as plt
import numpy as np
import math


# +
#f(x) = x**2
def derivative_of_fun(x):
    derv = 2*x
    return derv


def update_xvalue(x, lr):
    '''
    Vectorized Gradient Descent

    Features:(200, 3)
    Labels: (200, 1)
    Weights:(3, 1)
    '''
    #new_x = x
    
    #gradient = first derivative of f(x) = x**2
    d = derivative_of_fun(x)

    #4 - Multiply the gradient by our learning rate
    gradient = lr*d

    #5 - Subtract from currrent x value
    new_x = x - gradient

    return new_x


# +

def execute_gradient(x, lr):
    itr = 0
    x_updated = x
   
    while(True):
    #for i in range(0,itr):
        itr = itr + 1
        x_current = x_updated
        #print(x_current)
        x_updated = update_xvalue(x_current ,lr)
        
        if(itr == 31): #Until its previous version and current version are not close, we iterate
            break
        print(x_updated)  
        
    return x_updated

def execute_gradient_for(x, lr,itr):
   
    x_updated = x
    for i in range(0,itr):
      
        x_current = x_updated
        #print(x_current)
        x_updated = update_xvalue(x_current ,lr)
        
      
        print(x_updated)  
        
    return x_updated
    
# -

#RUN only one while commenting other two from below to get the output
x_in = float(-2)
out_x  = execute_gradient(x_in,0.6)#oscillate
out_x  = execute_gradient(x_in,1.5)#diverge
out_x  = execute_gradient(x_in,0.1)#converge
#print(out_x )

StarcoderdataJupyterScriptsDedupFiltered