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 x, then this is a straight line:

Equation-1

(1)#y=β+ωx,

where β is the y-intercept of the linear and ω is the slope of the line. When there are two inputs x1 and x2, then this becomes:

Equation-2

(2)#y=β+ω1x1+ω2x2.

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
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 3
      1 # Compute y using the function you filled in above
----> 3 x = np.arange(0.0,10.0, 0.01)
      4 beta = 0.0; omega = 1.0
      6 y = linear_function_1D(x,beta,omega)

NameError: name 'np' is not defined

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, x1, x2, and x3 and want to compute two linear functions giving y1 and y2. Of course, we could do this by just running each equation separately,

(3)#y1=β1+ω11x1+ω12x2+ω13x3y2=β2+ω21x1+ω22x2+ω23x3.

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

(4)#[y1y2]=[β1β2]+[ω11ω12ω13ω21ω22ω23][x1x2x3],
(5)#y=β+Ω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#