PT100 newton_algorithm.pdf

(80 KB) Pobierz
newton_algorithm
Solving Nonlinear Equations
with Newton’s Algorithm
- 1 -
Application Note
Solving Nonlinear Equations with Newton’s Algorithm
Example: Temperature measurement with resistive platinum sensor Pt100
1. Introduction
Solving nonlinear equations is a difficult task in general. Unless the solution can be expressed in analytical form one
must solve the equations numerically. Even if the solution is given as an expression (e.g. the roots of a quadratic
polynomial) this expression contains nonlinear functions that are not readily available on standard microprocessors and
microcontrollers. Consider the roots of a quadratic polynomial ax 2 +bx+c=0. Although the solution is given by the famous
formula x 1,2 = (-b ± (b 2 – 4ac) 1/2 )/2a, it contains a square root operation that must be implemented in software.
We will show in this application note how solutions of nonlinear equations can be obtained using Newton’s algorithm that
uses only elementary operations like addition, multiplication and division. It is always a good idea to try the Newton
algorithm to solve nonlinear equations instead of trying hard to get an analytical expression that contains non-elementary
operations that must be implemented in software.
2. Newton Algorithm
The Newton algorithm finds a zero x*, f(x*)=0 of a function f(x) by iteratively improving an initial guess. The algorithm is
applicable to functions of one or several variables. For the sake of simplicity we will concentrate here on functions with
one variable only. The general case can be found in text books on numerical mathematics.
Figure 1: How the Newton algorithm works. Starting from an initial guess x 0 , the algorithm makes a step towards the solution by finding
the intersection of the tangent in point x 0 with the x-axis. This intersection, labeled x 1 , is an improved guess of the real solution x*. By
repeated application of this procedure the algorithm eventually converges to the solution x*.
How the Newton algorithm works is shown in Figure 1 . Starting from an initial guess x 0 , the algorithm makes a step
towards the solution by finding the intersection of the tangent in point x 0 with the x-axis. This intersection, labeled x 1 , is
an improved guess of the real solution x*. We can now repeat the procedure using the tangent at x 1 to find x 2 . By
repeated application of this procedure the algorithm eventually converges to the solution x*. The choice of a suitable
initial guess is very important for fast convergence or convergence to the right solution at all. The convergence order of
ErSt Electronic GmbH
Aeschstrasse 171
CH-8123 Ebmatingen
Switzerland
Phone +41 44 980 61 44
Fax
+41 44 980 61 30
Internet info@erst.ch
http://www.erst.biz
http://www.erst.ch
322396882.012.png 322396882.013.png
Solving Nonlinear Equations
with Newton’s Algorithm
- 2 -
the Newton algorithm is 2, meaning a doubling of significant binary digits with each iteration. Usually a few iteration
cycles will be sufficient for practical purposes if the initial guess is within 10% of the final solution.
The general formulation of the Newton algorithm for finding a zero of a function of one variable is given in Figure 2
below.
Newton Algorithm
1) Make an initial guess x 0
2) Given an n-th guess x n , calculate an improved guess
x n+1 by setting
x n+1 = x n – f(x n ) / f’(x n )
3) Repeat step 2 until x n+1 meets a predefined error
tolerance, e.g. |x n+1 – x n | < 10 -8
Figure 2: Newton algorithm for finding a zero of a function of one variable.
3. Temperature Measurement with Pt100
The usual Pt100 temperature sensor has a nominal resistance of R(0) = 100Ȫ at zero degrees Celsius. The resistance
depends nonlinearly on the temperature. Over the full measurement range of -200 … +850 o C the sensor characteristic
can be expressed with two polynomials of 2 nd and 4 th order:
For the range 0 … +850 o C we have the equation
R(T) = R(0) * (1 + A*T - B*T 2 )
and for the range -200 … 0 o C we have
R(T) = R(0) * (1 + A*T - B*T 2 - C*(T - 100)*T 3 )
with the constants
A = 3.90802e-3 o C -1
B = 0.580195e-6 o C -1
C = 4.27350e-12 o C -1
T is the temperature in degree Celsius and R(T) the resistance of the sensor at temperature T. R(0) is the resistance of
the sensor at zero degrees Celsius, i.e. 100Ȫ for Pt100.
3.1. Application of the Newton Algorithm
Our task is to calculate the temperature for a given resistance, i.e. given R(T) we want to know T. As we have seen in the
discussion of the Newton algorithm we have to reformulate the problem such that we want to find the zeros of a function.
We can easily do that by setting
f(T) = R(T) – R measured
and finding T such that f(T)=0.
The Newton algorithm needs the derivative, f’(T) = df/dT, of f(T) with respect to T. For the range 0 … +850 o C we get the
equation
f’(T) = df/dT = dR(T)/dT = R(0)*(A – 2*B*T)
and for the range -200 … 0 o C we get
f’(T) = df/dT = dR(T)/dT = R(0)*(A – 2*B*T +300*C*T 2 - 4*C*T 3 )
ErSt Electronic GmbH
Aeschstrasse 171
CH-8123 Ebmatingen
Switzerland
Phone +41 44 980 61 44
Fax
+41 44 980 61 30
Internet info@erst.ch
http://www.erst.biz
http://www.erst.ch
322396882.014.png 322396882.015.png 322396882.001.png 322396882.002.png
Solving Nonlinear Equations
with Newton’s Algorithm
- 3 -
with the constants A, B and C as above.
For the initial guess T 0 we use the linear approximation
R(T) = R(0)*(1 + A*T)
and get
T 0 = (R measured / R(0) – 1) / A
Starting with n=0 we can now apply the iterative process from Figure 2 to get better approximations for the measurement
temperature:
T n+1 = T n – f(T n ) / f’(T n )
The data in the following two tables was created with the C code program in the Appendix (see Figure 3). They show the
power of the Newton algorithm. Starting with a given temperature the Pt100 resistance R(T) is calculated. Then an initial
guess of the temperature using the linear approximation is made. Successive iterations converge rapidly to the solution.
3.2. Demonstration of the Newton algorithm for T in -200...0 degrees
Given temperature Calculated R(T)
Initial guess T 0
(linear approximation)
1st iteration
T 1
2nd iteration
T 2
3rd iteration
T 3
-200.000000
18.493180
-208.562955
-200.032435
-200.000000
-200.000000
-150.000000
39.713685
-154.263068
-150.005979
-150.000000
-150.000000
-100.000000
60.254135
-101.703331
-100.000695
-100.000000
-100.000000
-50.000000
80.306838
-50.391660
-50.000027
-50.000000
-50.000000
-10.000000
96.086131
-10.014967
-10.000000
-10.000000
-10.000000
0.000000
100.000000
0.000000
0.000000
0.000000
0.000000
Table 1: Demonstration of the Newton algorithm for temperatures below zero. Starting with a given temperature we calculate the Pt100
resistance R(T). We then make an initial guess of the temperature using the linear approximation. Successive iterations converge
rapidly to the solution.
3.3. Demonstration of the Newton algorithm for T in 0...+850 degrees
Given temperature Calculated R(T)
Initial guess T 0
(linear approximation)
1st iteration
T 1
2nd iteration
T 2
3rd iteration
T 3
20.000000
107.792832
19.940615
19.999999
20.000000
20.000000
100.000000
138.500005
98.515374
99.999663
100.000000
100.000000
200.000000
175.839620
194.061494
199.994444
200.000000
200.000000
300.000000
212.018845
286.638362
299.971029
300.000000
300.000000
400.000000
247.037680
376.245976
399.905694
399.999999
400.000000
500.000000
280.896125
462.884338
499.762894
499.999990
500.000000
600.000000
313.594180
546.553447
599.493755
599.999954
600.000000
700.000000
345.131845
627.253302
699.034502
699.999825
700.000000
800.000000
375.509120
704.983905
798.304823
799.999441
800.000000
Table 2: Demonstration of the Newton algorithm for temperatures above zero. Starting with a given temperature we calculate the
Pt100 resistance R(T). We then make an initial guess of the temperature using the linear approximation. Successive iterations
converge rapidly to the solution.
4. Conclusion
The iterative Newton algorithm to find a zero of a function of one variable is both fast and easy to implement. It makes
use of only elementary operations (addition, multiplication and division) and evaluations of the function and its first
derivative. The algorithm is well suited for implementations on standard microprocessors and microcontrollers. As an
example we applied the algorithm to the calculation of the measurement temperature from the resistance of a Pt100
sensor. The simplicity and accuracy of the Newton algorithm is clearly evident from the examples (see Table 1 and Table
2 above).
ErSt Electronic GmbH
Aeschstrasse 171
CH-8123 Ebmatingen
Switzerland
Phone +41 44 980 61 44
Fax
+41 44 980 61 30
Internet info@erst.ch
http://www.erst.biz
http://www.erst.ch
322396882.003.png 322396882.004.png 322396882.005.png
Solving Nonlinear Equations
with Newton’s Algorithm
- 4 -
5. Appendix: C Code Program that demonstrates the Newton algorithm
/* Demonstration of the application of the Newton algorithm to calculate
* the temperature from the measured resistance of a Pt100 sensor.
*/
#include <stdio.h>
/* Constants for temperature dependence of resistance of Pt100 sensor */
#define A 3.90802e-3
#define B 0.580195e-6
#define C 4.27350e-12
#define R0 100
/* Pt100 resistance for the range 0 ... +850 degrees Celsius */
double pt100_above_zero ( double T) {
return R0*(1 + T*(A - B*T));
}
/* Pt100 resistance for the range -200 ... 0 degrees Celsius */
double pt100_below_zero ( double T) {
return R0*(1 + T*(A + T*(-B + T*(100*C - C*T))));
}
/* Derivative of Pt100 resistance for the range 0 ... +850 degrees Celsius */
double derivative_above_zero ( double T) {
return R0*(A - 2*B*T);
}
/* Derivative of Pt100 resistance for the range -200 ... 0 degrees Celsius */
double derivative_below_zero ( double T) {
return R0*(A + T*(-2*B + T*(300*C - 4*C*T)));
}
/* Initial guess of measurement temperature */
double initial_guess ( double R) {
return (R / R0 - 1) / A;
}
/* Newton algorithm step for the range 0 ... +850 degrees Celsius */
double newton_step_above_zero ( double temp, double rmeas) {
return temp - (pt100_above_zero(temp) - rmeas)/derivative_above_zero(temp);
}
/* Newton algorithm step for the range -200 ... 0 degrees Celsius */
double newton_step_below_zero ( double temp, double rmeas) {
return temp - (pt100_below_zero(temp) - rmeas)/derivative_below_zero(temp);
}
/* Demonstrates the application of the newton algorithm for temperatures
* above zero.
*/
void demo_above_zero ( double temp) {
double rmeas, t0, t1, t2, t3;
/* Calculate Pt100 resistance at given temperature */
rmeas = pt100_above_zero(temp);
printf("Given temperature: %f\n", temp);
printf("R(T): %f\n", rmeas);
/* Calculate Pt100 resistance at given temperature */
rmeas = pt100_above_zero(temp);
/* Recalculate temperature from resistance */
t0 = initial_guess(rmeas);
printf ("Initial guess: %f\n", t0);
t1 = newton_step_above_zero (t0, rmeas);
printf ("1st iteration: %f\n", t1);
ErSt Electronic GmbH
Aeschstrasse 171
CH-8123 Ebmatingen
Switzerland
Phone +41 44 980 61 44
Fax
+41 44 980 61 30
Internet info@erst.ch
http://www.erst.biz
http://www.erst.ch
322396882.006.png 322396882.007.png 322396882.008.png
 
Solving Nonlinear Equations
with Newton’s Algorithm
- 5 -
t2 = newton_step_above_zero (t1, rmeas);
printf ("2nd iteration: %f\n", t2);
t3 = newton_step_above_zero (t2, rmeas);
printf ("3rd iteration: %f\n", t3);
printf("\n");
}
/* Demonstrates the application of the newton algorithm for temperatures
* below zero.
*/
void demo_below_zero ( double temp) {
double rmeas, t0, t1, t2, t3;
/* Calculate Pt100 resistance at given temperature */
rmeas = pt100_below_zero(temp);
printf("Given temperature: %f\n", temp);
printf("R(T): %f\n", rmeas);
/* Recalculate temperature from resistance */
t0 = initial_guess(rmeas);
printf ("Initial guess: %f\n", t0);
t1 = newton_step_below_zero (t0, rmeas);
printf ("1st iteration: %f\n", t1);
t2 = newton_step_below_zero (t1, rmeas);
printf ("2nd iteration: %f\n", t2);
t3 = newton_step_below_zero (t2, rmeas);
printf ("3rd iteration: %f\n", t3);
printf("\n");
}
void main() {
printf("Demonstration of the Newton algorithm for T in -200...0 degrees\n");
demo_below_zero(-200);
demo_below_zero(-150);
demo_below_zero(-100);
demo_below_zero(-50);
demo_below_zero(-10);
demo_below_zero(0);
printf("Demonstration of the Newton algorithm for T in 0...+850 degrees\n");
demo_above_zero(0);
demo_above_zero(20);
demo_above_zero(100);
demo_above_zero(200);
demo_above_zero(300);
demo_above_zero(400);
demo_above_zero(500);
demo_above_zero(600);
demo_above_zero(700);
demo_above_zero(800);
}
Figure 3: Example C code that demonstrates the application of the Newton algorithm to calculate the temperature from the measured
resistance of a Pt100 sensor.
© ErSt Electronic GmbH, 1998-2006. All rights reserved. All specifications are subject to change without notice.
ErSt Electronic GmbH
Aeschstrasse 171
CH-8123 Ebmatingen
Switzerland
Phone +41 44 980 61 44
Fax
+41 44 980 61 30
Internet info@erst.ch
http://www.erst.biz
http://www.erst.ch
322396882.009.png 322396882.010.png 322396882.011.png
 
Zgłoś jeśli naruszono regulamin