线性最小二乘法的矩阵解法——以求电阻为例

Table of Contents

The problem is originally from an assignment, given by the professor of my summer international course Machine Learning for Autonomous Driving, who is an extraordinary, excellent, kind and talented person.

Introduction

Using a multimeter, you measure the voltage drop across the resistor at various current values and collect the following data:

Current (A) Voltage (V)
0.2 1.23
0.3 1.38
0.4 2.06
0.5 2.47
0.6 3.17

With the data in hand, your goals are to:

  1. Fit a line through the origin (i.e., determine the parameter R for y = Rx) to this data by using the method of least squares. You may assume that all measurements are of equal importance.
  2. Consider what the best estimate of the resistance is, in ohms, for this component.

Getting Started

The first step is to import the necessary Python modules and load the current values and voltage measurements into NumPy arrays:

import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt

# Store the voltage and current data as column vectors.
I = np.mat([0.2, 0.3, 0.4, 0.5, 0.6]).T
V = np.mat([1.23, 1.38, 2.06, 2.47, 3.17]).T

Plot the measurements:

# plt.scatter(I, V)
plt.scatter(np.asarray(I), np.asarray(V))

plt.xlabel('Current (A)')
plt.ylabel('Voltage (V)')
plt.grid(True)
plt.show()

Estimating the Slope Parameter

In this step, the key point is to construct matrix H and vector \textbf{y} to find \hat{R}.

With the data given, we have H = \begin{bmatrix} 0.2 & 1 \\ 0.3 & 1 \\ 0.4 & 1 \\ 0.5 & 1 \\ 0.6 & 1\end{bmatrix}.

As for the vector \textbf{y}, each element of \textbf{y} is the voltage value V that we want to fit.

Therefore, we have \textbf{y} = \begin{bmatrix} 1.23 \\ 1.38 \\ 2.06 \\ 2.47 \\ 3.17\end{bmatrix}.

According to the formula \hat{R} = (H^TH)^{-1}H^T\textbf{y}, the result should be \begin{bmatrix} 4.97 \\ 0.074 \end{bmatrix}.

Plotting the Results

Through the steps above, after running R = R[0, 0], we can get the resistance value.

I_line = np.arange(0, 0.8, 0.1).reshape(8, 1)

V_line = np.dot(R, I_line)

plt.scatter(np.asarray(I_line),np.asarray(V_line))
plt.plot(I_line, V_line)
plt.xlabel('Current (A)')
plt.ylabel('Voltage (V)')
plt.grid(True)
plt.show()

The code plots the results.

Summary

The two goals in Introduction are:

  1. Fit a line through the origin (i.e., determine the parameter R for y=Rx) to this data by using the method of least squares. You may assume that all measurements are of equal importance.
  2. Consider what the best estimate of the resistance is, in ohms, for this component.

Now the parameter R is determined by the method of least squares, and we can say that 4.97\Omega, which is close enough to 5\Omega, is the best estimate of the resistance.

Share