Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Numpy Lab: Linear Functions

Topics: elementwise operations

Linear functions
We will be using the term linear equation to mean a weighted sum of inputs plus an offset. If there is just one input xx, then this is a straight line:

Equation-1

y=β+ωx,y=\beta+\omega x,

where β\beta is the y-intercept of the linear and ω\omega is the slope of the line. When there are two inputs x1x_{1} and x2x_{2}, then this becomes:

Equation-2

y=β+ω1x1+ω2x2.y=\beta+\omega_1 x_1 + \omega_2 x_2.

Any other functions are by definition non-linear.

Type your code below

# Define a linear function with just one input, x (Eq 1)
def linear_function_1D(x, beta, omega):

  return y
# Compute y using the function you filled in above

x = np.arange(0.0,10.0, 0.01)
beta = 0.0; omega = 1.0

y = linear_function_1D(x,beta,omega)

# Plot this function
fig, ax = plt.subplots()
ax.plot(x,y,'r-')
ax.set_ylim([0,10]);ax.set_xlim([0,10])
ax.set_xlabel('x'); ax.set_ylabel('y')
plt.show

# TODO -- experiment with changing the values of beta and omega
# to understand what they do.  Try to make a line
# that crosses the y-axis at y=10 and the x-axis at x=5

Now let’s investigate a 2D linear function

# We will use this function to draw 2D contour plots. Nothing to do here
def draw_2D_function(x1_mesh, x2_mesh, y):

    fig, ax = plt.subplots()

    pos = ax.contourf(x1_mesh, x2_mesh, y, levels=256 ,cmap = 'hot', vmin=-10,vmax=10.0)
    fig.colorbar(pos, ax=ax)
    
    ax.contour(x1_mesh, 
               x2_mesh, 
               y, 
               levels= np.arange(-10,10,1.0), 
               cmap='winter')
    
    ax.set_xlabel('x1')
    ax.set_ylabel('x2')
    
    plt.show()
# Define a linear function with two inputs, x1 and x2
def linear_function_2D(x1,x2,beta,omega1,omega2):


  return y

Using numpy to write general linear functions in compact form

Often we will want to compute many linear functions at the same time. For example, we might have three inputs, x1x_1, x2x_2, and x3x_3 and want to compute two linear functions giving y1y_1 and y2y_2. Of course, we could do this by just running each equation separately,

y1=β1+ω11x1+ω12x2+ω13x3y2=β2+ω21x1+ω22x2+ω23x3.\begin{align}y_1 &=& \beta_1 + \omega_{11} x_1 + \omega_{12} x_2 + \omega_{13} x_3\\ y_2 &=& \beta_2 + \omega_{21} x_1 + \omega_{22} x_2 + \omega_{23} x_3. \end{align}

However, we can write it more compactly with vectors and matrices:

[y1y2]=[β1β2]+[ω11ω12ω13ω21ω22ω23][x1x2x3],\begin{bmatrix} y_1\\ y_2 \end{bmatrix} = \begin{bmatrix}\beta_{1}\\\beta_{2}\end{bmatrix}+ \begin{bmatrix}\omega_{11}&\omega_{12}&\omega_{13}\\\omega_{21}&\omega_{22}&\omega_{23}\end{bmatrix}\begin{bmatrix}x_{1}\\x_{2}\\x_{3}\end{bmatrix},
y=β+Ωx.\mathbf{y} = \boldsymbol\beta +\boldsymbol\Omega\mathbf{x}.

Here, lowercase bold symbols are used for vectors. Upper case bold symbols are used for matrices.

# Define a linear function with three inputs, x1, x2, and x_3
def linear_function_3D(x1,x2,x3,beta,omega1,omega2,omega3):
  # TODO -- replace the code below with formula for a single 3D linear equation
  y = x1

  return y
# Define a linear function using vector input x=[x_1, x_2, x_3], and omega = [omega_1, omega_2, omega_3]
def linear_function_3D_la(x, beta, omega):
  # TODO -- replace the code below with formula for a single 3D linear equation
  y = x1

  return y

Check that both functions agree