<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Sensitivity, accuracy, errors, and repeatability
Linear approximation of nonlinear transfer function. The response of a temperature sensor is given as
$$R(T)=R_{0}e^{\beta(\frac{1}{T}-\frac{1}{T_{0}})}$$
where $R_{0}$ is the resistance of the sensor at temperature $T_{0}$ and $\beta$ is a constant that depends on the material of the sensor. $T_{0}$=20 ºC. Temperatures $T$ and $T_{0}$ are in K. Given
Step1:  The plot shows the nonlinear behaviour of the sensor and the two points used for estimating the curve.
b. Approximate the transfer function as a straight line connecting the end points and calculate the maximum error expected as a percentage of full scale.
We approximate the transfer function as a straight line as $R(T)_{\text{linear}} = aT+b$ and compute the error.
Step2:  Note how the error starts from zero reaches a maximum of 66.5 % and comes back down to zero at the other end point as expected.
c. Approximate the transfer function as a linear least squares approximation and calculate the maximum error expected as a percentage of full scale.
Mediante la funcion polyfit se realizo la aproximacion lineal de la funcion de transferencia del sensor. El maximo error con respecto a la temperatura es calculado a partir de la diferencia entre R(T) y la aproximacion obtenida, en el intervalo definido por los puntos de corte de la funciones. Esta diferencia dividida por la pendiente de la aproximacion me dará el error en el eje correspondiente a la temperatura en K
<ASSISTANT_TASK:>
Python Code:
import matplotlib.pyplot as plt
import numpy as np
from math import log, exp
%matplotlib inline
# Evaluate beta for this sensor 
T_0=273.15+20;
N=(1/273.15-1/293.15)-(1/298.15-1/293.15);
beta= log(3000/1000)/N;
R_0=1000/exp(beta*((1/298.15)-(1/293.15)));
## Results
print('Beta for this sensor = %2.2f and the resistance of the sensor at temperature T0 is R_0 = = %2.2f' % (beta, R_0))
# Plot the sensor transfer function for the intended span.
# T= np.arange(start = -45, stop = 121, step = 1)+273.15;
T = np.linspace(-45,120)+273.15
R_T= R_0*np.exp(beta*(1/T-1/T_0));
# Plot
# plt.plot(T,R_T,T[45],R_T[45],'ro',T[45+25],R_T[45+25],'ro')
plt.plot(T,R_T)
plt.ylabel('Resistance of the sensor[ohm]')
plt.xlabel('Temperature [K]')
plt.show()
Explanation: Sensitivity, accuracy, errors, and repeatability
Linear approximation of nonlinear transfer function. The response of a temperature sensor is given as
$$R(T)=R_{0}e^{\beta(\frac{1}{T}-\frac{1}{T_{0}})}$$
where $R_{0}$ is the resistance of the sensor at temperature $T_{0}$ and $\beta$ is a constant that depends on the material of the sensor. $T_{0}$=20 ºC. Temperatures $T$ and $T_{0}$ are in K. Given: R(T)=1000 $\Omega$ at 25 ºC and 3000 $\Omega$ at 0 ºC. The sensor is intended for use between  -45 ºC and 120 ºC.
a. Evaluate $\beta$ for this sensor and plot the sensor transfer function for the intended span.
b. Approximate the transfer function as a straight line connecting the end points and calculate the maximum error expected as a percentage of full scale.
c. Approximate the transfer function as a linear least squares approximation and calculate the maximum error expected as a percentage of full scale.
Solution
a. Evaluate $\beta$ for this sensor and plot the sensor transfer function for the intended span.
From the two known points
$R_1= 3000\, \Omega$, $T_1= 273.15\, \text{K}$.
$R_2= 1000\, \Omega$, $T_2= 298.15\, \text{K}$.
From the general equation and the points,
$$R_{0}= \frac{R_{1}}{e^{\beta(\frac{1}{T_{1}}-\frac{1}{T_{0}})}} \enspace,$$
we obtain
$$\beta= \frac{\ln(\frac{R_{2}}{R_{1}})}{(\frac{1}{T_{2}}-\frac{1}{T_{1}})} \enspace.$$
End of explanation
# Linear fit with just end points
a, b = np.polyfit(np.array([T[0],T[-1]]),np.array([R_T[0],R_T[-1]]),1)
print('The coefficients are a = %2.4f and b = %2.4f' % (a, b))
# Linear approximation
R_T_linear = a*T+b
# Plot
plt.plot(T,R_T_linear,'b:',label='Linear approximation')
plt.plot(T,R_T,label='Transfer function')
plt.ylabel('Resistance of the sensor[ohm]')
plt.xlabel('Temperature [K]')
plt.legend(loc='upper right')
plt.show()
# Output Full scale
FS = np.abs(np.max(R_T)-np.min(R_T))
error=np.abs(R_T-R_T_linear)/FS*100;
# error_X=np.abs(error_Y/a2);
plt.ylabel('error [%]')
plt.plot(T,error)
plt.xlabel('Temperature [K]')
plt.show()
print('The maximum error expected as a percentage of full scale is = %2.2f %%' % (np.max(error)))
Explanation: The plot shows the nonlinear behaviour of the sensor and the two points used for estimating the curve.
b. Approximate the transfer function as a straight line connecting the end points and calculate the maximum error expected as a percentage of full scale.
We approximate the transfer function as a straight line as $R(T)_{\text{linear}} = aT+b$ and compute the error.
End of explanation
# polyfit computes the coefficients a and b of degree=1
a,b = np.polyfit(T,R_T,1)
# Linear approximation
R_T_lsq = a*T+b
# Plot
plt.plot(T,R_T_lsq,'b:',label='Least Squares fit')
plt.plot(T,R_T,label='Transfer function')
plt.ylabel('Resistance of sensor [ohm]')
plt.xlabel('Temperature [K]')
plt.legend(loc='upper right')
plt.show()
error=np.abs(R_T-R_T_lsq)/FS*100;
# error_X=np.abs(error_Y/a2);
plt.ylabel('error [%]')
plt.plot(T,error)
plt.xlabel('Temperature [K]')
plt.show()
print('The maximum error expected as a percentage of full scale is = %2.1f %%' % (np.max(error)))
Explanation: Note how the error starts from zero reaches a maximum of 66.5 % and comes back down to zero at the other end point as expected.
c. Approximate the transfer function as a linear least squares approximation and calculate the maximum error expected as a percentage of full scale.
Mediante la funcion polyfit se realizo la aproximacion lineal de la funcion de transferencia del sensor. El maximo error con respecto a la temperatura es calculado a partir de la diferencia entre R(T) y la aproximacion obtenida, en el intervalo definido por los puntos de corte de la funciones. Esta diferencia dividida por la pendiente de la aproximacion me dará el error en el eje correspondiente a la temperatura en K:
End of explanation
<END_TASK>