0124 programming partial differential and gradient plot plot

Click here to enter the AI bar directory and view all articles

Combination Last article Understand the partial derivative (slope) and gradient diagram shown in the figure below.

Notice that the two red arrows in the figure indicate that the height of the two ends of the surface is different, so the slope along the y direction is larger. The purple line in the figure indicates the direction of negative gradient, that is, the direction of gradient descent.

The plot code of this figure is as follows:

#Two variable function
import plotly.offline as py
import plotly.graph_objs as go
import random
import math
py.init_notebook_mode()


#Primitive function
def func(x, y):
    res = math.pow(x, 3) + math.pow(y, 3) + 2 * x + 400 * y
    return res


#Find x-direction deflection
def slopex(x, y):
    res = 3 * math.pow(x, 2) + math.pow(y, 3) + 400 * y
    return res


#Finding the y-direction deflection
def slopey(x, y):
    res = math.pow(x, 3) + 3 * math.pow(y, 2) + 2 * x
    return res


#------------------------------------Data
#Surface rendering
surf = go.Surface(
    z=[[func(x, y) for x in range(0, 20)] for y in range(0, 20)],
    opacity=0.85,
    colorscale='Hot',
    showscale=False,
)

#Point position
point = dict(x=15, y=15, z=func(15, 15))

#Draw a blue y-line
liney = go.Scatter3d(
    x=[n for n in range(0, 21)],
    y=[point['y'] for n in range(0, 20)],
    z=[func(n, point['y']) for n in range(0, 21)],
    line=dict(width=2, color='blue'),
    mode='lines')

#Draw a blue x-line
linex = go.Scatter3d(
    y=[n for n in range(0, 21)],
    x=[point['x'] for n in range(0, 21)],
    z=[func(point['x'], n) for n in range(0, 21)],
    line=dict(width=2, color='blue'),
    mode='lines')


#Draw point and slope line, gradient line
def drawLines(p):
    #Draw point
    marker = go.Scatter3d(
        x=[p['x']],
        y=[p['y']],
        z=[func(p['x'], p['y'])],
        marker=dict(size=5, color='blue'),
        mode='markers')
    
    #The scaling value of the slope can be set arbitrarily according to the image needs
    slopeScale=0.0005
    
    #Slope line in x direction, drawing only the start and end points
    lineSlopex = go.Scatter3d(
        x=[p['x']] * 2,
        y=[p['y'], p['y'] + slopex(p['x'], p['y']) * slopeScale],
        z=[func(p['x'], p['y'])] * 2,
        line=dict(width=8, color='rgb(0,255,0)'),
        mode='lines')

    #Slope line in y direction, only the start and end points are drawn
    lineSlopey = go.Scatter3d(
        x=[p['x'], p['x'] + slopey(p['x'], p['y']) * slopeScale],
        y=[p['y']] * 2,
        z=[func(p['x'], p['y'])] * 2,
        line=dict(width=8, color='rgb(0,255,0)'),
        mode='lines')

    #Adding the slope lines of x and y directions to get the gradient direction vector
    lineGradient = go.Scatter3d(
        x=[p['x'], p['x'] + slopey(p['x'], p['y']) * slopeScale],
        y=[p['y'], p['y'] + slopex(p['x'], p['y']) * slopeScale],
        z=[func(p['x'], p['y'])] * 2,
        line=dict(width=8, color='rgb(255,0,0)'),
        mode='lines')

    #Negative gradient direction vector, i.e. gradient descent vector
    lineDescent = go.Scatter3d(
        x=[p['x'], p['x'] - slopey(p['x'], p['y']) * slopeScale],
        y=[p['y'], p['y'] - slopex(p['x'], p['y']) * slopeScale],
        z=[func(p['x'], p['y'])] * 2,
        line=dict(width=8, color='rgb(255,0,255)'),
        mode='lines')

    return [lineSlopex, lineSlopey, lineGradient, lineDescent, marker]


datas = [surf, linex, liney]
datas = datas + drawLines(point)

#----------------------------------------Drawing
layout = go.Layout(
    title='f(x,y)=x^3+y^3+2*x+400*y',
    width=800,
    height=800,
    scene=dict(
        xaxis=dict(title='x'),
        yaxis=dict(title='y'),
        zaxis=dict(title='f(x,y)'),
    ))

fig = go.FigureWidget(datas, layout=layout)
py.iplot(fig)

Click here to enter the directory of artificial intelligence DBD and watch all the articles

A new era of intelligence for everyone

If you find that the article is wrong, please leave a message to correct;
If you find it useful, please like it;
If you find it useful, please reprint it~

END

Posted by cmay4 on Sat, 30 Nov 2019 11:28:10 -0800