To come in
Sewerage and drainpipes portal
  • Dried fruit sweets "Energy koloboks"
  • Raspberry grated with sugar: tasty and healthy
  • Alexey Pleshcheev: biography
  • How to preserve apple juice
  • Cabbage salad with carrots like in a dining room - the best recipes from childhood
  • An even complexion without foundation!
  • Function approximation methods. Experimental data approximation

    Function approximation methods. Experimental data approximation

    Approximation of experimental data is a method based on replacing the experimentally obtained data with an analytical function that most closely passes or coincides at the nodal points with the initial values \u200b\u200b(data obtained during the experiment or experiment). There are currently two ways to define an analytic function:

    By constructing an n-degree interpolation polynomial that passes directly through all points a given data array. In this case, the approximating function is represented in the form of an interpolation polynomial in the form of Lagrange or an interpolation polynomial in the form of Newton.

    By constructing an approximating n-degree polynomial that passes in close proximity to points from a given data array. Thus, the approximating function smoothes out all random noise (or errors) that may arise during the experiment: the measured values \u200b\u200bduring the experiment depend on random factors that fluctuate according to their own random laws (measurement or instrument errors, inaccuracy or experimental errors). In this case, the approximating function is determined using the least squares method.

    Least square method (in the English-language literature Ordinary Least Squares, OLS) is a mathematical method based on the determination of an approximating function, which is constructed in the closest proximity to points from a given array of experimental data. The proximity of the initial and approximating function F (x) is determined by a numerical measure, namely: the sum of the squares of deviations of the experimental data from the approximating curve F (x) should be the smallest.

    Least Squares Fitting Curve

    The least squares method is used:

    To solve overdetermined systems of equations when the number of equations exceeds the number of unknowns;

    To search for a solution in the case of ordinary (not overdetermined) nonlinear systems of equations;

    To approximate point values \u200b\u200bby some approximating function.

    The approximating function by the method of least squares is determined from the condition of the minimum sum of squares of deviations of the calculated approximating function from a given array of experimental data. This criterion of the least squares method is written as the following expression:

    The values \u200b\u200bof the calculated approximating function at the nodal points,

    A given array of experimental data at the nodal points.

    The quadratic criterion has a number of "good" properties, such as differentiability, providing a unique solution to the approximation problem with polynomial approximating functions.

    Depending on the conditions of the problem, the approximating function is a polynomial of degree m

    The degree of the approximating function does not depend on the number of nodal points, but its dimension should always be less than the dimension (number of points) of a given array of experimental data.

    ∙ If the degree of the approximating function is m \u003d 1, then we approximate the tabular function with a straight line (linear regression).

    ∙ If the degree of the approximating function is m \u003d 2, then we approximate the tabular function with a quadratic parabola (quadratic approximation).

    ∙ If the degree of the approximating function is m \u003d 3, then we approximate the tabular function with a cubic parabola (cubic approximation).

    In the general case, when it is required to construct an approximating polynomial of degree m for given tabular values, the condition for the minimum of the sum of squares of deviations for all nodal points is rewritten as follows:

    - unknown coefficients of the approximating polynomial of degree m;

    The number of specified table values.

    A necessary condition for the existence of a minimum of a function is the equality to zero of its partial derivatives with respect to unknown variables ... As a result, we get the following system of equations:

    We transform the resulting linear system of equations: we open the brackets and transfer the free terms to the right side of the expression. As a result, the resulting system of linear algebraic expressions will be written in the following form:

    This system of linear algebraic expressions can be rewritten in matrix form:

    As a result, a system of linear equations of dimension m + 1 was obtained, which consists of m + 1 unknowns. This system can be solved using any method for solving linear algebraic equations (for example, the Gauss method). As a result of the solution, unknown parameters of the approximating function will be found that provide the minimum sum of squares of deviations of the approximating function from the initial data, i.e. best possible quadratic approximation. It should be remembered that when even one value of the initial data changes, all the coefficients will change their values, since they are completely determined by the initial data.

    Linear approximation of initial data

    (linear regression)

    As an example, consider the method for determining the approximating function, which is specified as a linear relationship. In accordance with the least squares method, the condition for the minimum sum of squares of deviations is written in the following form:

    The coordinates of the grid points of the table;

    Unknown coefficients of the approximating function, which is given as a linear relationship.

    A necessary condition for the existence of a minimum of a function is the equality to zero of its partial derivatives with respect to unknown variables. As a result, we obtain the following system of equations:

    Let us transform the resulting linear system of equations.

    We solve the resulting system of linear equations. The coefficients of the approximating function in analytical form are determined as follows (Cramer's method):

    These coefficients provide the construction of a linear approximating function in accordance with the criterion for minimizing the sum of squares of the approximating function from the given table values \u200b\u200b(experimental data).

    Algorithm for implementing the least squares method

    1. Initial data:

    An array of experimental data with the number of measurements N

    The degree of the approximating polynomial is given (m)

    2. Calculation algorithm:

    2.1. Coefficients are determined for constructing a system of equations with the dimension

    Coefficients of the system of equations (left side of the equation)

    is the index of the column number of the square matrix of the system of equations

    Free terms of a system of linear equations (right side of the equation)

    is the index of the row number of the square matrix of the system of equations

    2.2. Formation of a system of linear equations in dimension.

    2.3. Solving a system of linear equations in order to determine the unknown coefficients of the approximating polynomial of degree m.

    2.4 Determination of the sum of squares of deviations of the approximating polynomial from the original values \u200b\u200bfor all nodal points

    The found value of the sum of squares of deviations is the minimum possible.

    Approximation using other functions

    It should be noted that when approximating the initial data in accordance with the least squares method, a logarithmic function, an exponential function, and a power function are sometimes used as an approximating function.

    Logarithmic approximation

    Consider the case when the approximating function is given by a logarithmic function of the form:

    Approximation, or approximation - a scientific method consisting in replacing some objects with others, in one sense or another close to the original, but simpler. In the tasks considered in this section and in the next one, the initial data obtained as a result of tabulation of a given function are used. It should be remembered that in real problems, the initial data are the results of observations (conducting experiments, scientific experiments, observing real events, etc.), which are subject to measurement errors and other random factors. The researcher's task is to select, based on the initial points (which at first glance are chaotically located), a functional dependence (if at all possible) that best describes the distribution of the initial data and, in some cases, try to make a forecast of further development (for example, studying the time series of changes in stock prices).

    The task... Build a table of function values F (x) \u003d ax² + bx + c for 11 argument values x in the range –1 ≤ x ≤ +1... Plot this function, then fit two types of trend lines. Use trend lines to build a forecast for two periods ahead.

    As in the previous tasks, we enter the initial data: the initial value of the function argument Xn, the final value of the function argument Xk, the number of points to split the function (the number of table rows) N, the formula for the step of the function argument dX, coefficients a, b, c, then create a main table and build a diagram (all these actions were described in detail in the section):


    Trend lines on a chart

    Trendlines allow you to graphically display trends in data and predict their future changes. This analysis is also called regression analysis. Using regression analysis, you can extend a trend line in a chart beyond the real data to predict future values.

    Trendlines can be plotted on all 2D charts (You cannot add a trendline on 3-D, Radar, Pie, Donut, or Bubble charts.)

    There are six different types of trend lines:

    • Linear
    • Polynomial
    • Logarithmic
    • Exponential
    • Power

    Trendlines added to the graph of the function do not affect the data itself or the original graph in any way.

    Formulas for calculating trend lines

    Linear... Used to linearly fit the data using the least squares method according to the equation:

    where: m - tilt angle, b - the coordinate of the intersection of the abscissa axis.

    Polynomial... Used to fit polynomial or curvilinear least squares data according to the equation:

    where: b , c 1 , c 2 , … c 6 - constants.

    You can set the degree of the polynomial from 2 to 6.

    Logarithmic... Used to fit logarithmic least squares data according to the equation:

    where: c and b - constants, ln is the natural logarithm function.

    Exponential... Used to exponentially fit the data using the least squares method according to the equation:

    where: c and b - constants, e is the base of the natural logarithm.

    Power... Used for a power-law approximation of the data using the least squares method according to the equation:

    where: c and b - constants.

    Note... Exponential and power types of approximation are not available if the function values F (x) contain negative or zero values. In addition, logarithmic and power types of approximation are not available if the values \u200b\u200bof the function argument x contain negative or zero values. Since the lab assignments use a negative lower bound for the argument Xn (x0), do not choose the logarithmic and power types of approximation!

    The moving average is the average over a given period:

    In the chart, the line drawn from the moving average points allows you to create a smoothed curve that more clearly shows the pattern in the development of the data.

    Adding a trend line to data series

    Select the diagram (click anywhere in the empty area of \u200b\u200bthe diagram), after which three additional tabs will appear on the menu ribbon: Constructor , Layout and Format ... In the tab Layout in a group Analysis click on the button .

    Department: ________ Informatics and Computer Technology_______________

    COURSE WORK

    by discipline _______________ INFORMATICS __________________________

    (name of the discipline according to the curriculum)

    THE TASK

    student of group MGP-12 Rumyantseva N.A.

    (group code) (full name)

    1. Topic: _ Implementation of the numerical method by means of Microsoft Excel and by means of the MathCAD package

    2. Initial data for work: _Variant No. 17__________________________________

    4. List of graphic material: _ Presentation of results in the form of screen forms ____________________________________________________________________

    5. Completion date of completed work: ___ 01.05.2013____________________________

    Work manager: ________ ______________ / _________ /

    (position) (signature) (full name)

    Date of issue of the assignment: __ 02/15/2013______________


    annotation

    An explanatory note is a report on the implementation of coursework. It discusses the issues of finding empirical formulas using the least squares method (OLS) using the capabilities of the Microsoft Excel package, and also discusses the solution to this problem in the MathCAD package. In this work, equations of various types are obtained using the approximation of linear, quadratic and exponential dependences. At the end of the work, it was concluded by what method the problem was solved best.

    Pages 24, tables 3, figures 14, attachments 0.

    Abstract

    The explanatory note represents the report on term paper performance. In it questions on a finding of empirical formulas by a method of the least squares (OLS) by means of possibilities of package Microsoft Excel are considered, and also the decision of the given problem in Turbo Pascal 7.0 is considered. In work the equations of various kinds by means of approximation linear, square-law and exponential dependences are received. Upon termination of work the conclusion is drawn, the problem is solved by what method is better.

    Pages 24, tables 3, figures 14, appendixes 0.

    Annotation. 2

    Introduction. 4

    Formulation of the problem. five

    General information. 6

    Linear dependence. 7

    Non-linear dependency. 7

    Initial data. ten

    Calculation of approximations in a spreadsheet processor Excel 11

    Building graphs. 17

    LINEST function .. 18

    Approximation in MathCAD program .. 19

    Introduction. 19

    Linear approximation in the MathCAD program .. 21

    Exponential approximation in the MathCAD program .. 22

    Polynomial (quadratic approximation in the MathCAD program .. 23

    References .. 24

    Introduction

    Approximation (from the Latin "approximare" - "to approach") is a scientific method, the essence of which is to replace some known values \u200b\u200bwith others, approximate and simpler. These simple values \u200b\u200bmust satisfy some dependency, the finding of which, in general, is the ultimate goal of this method.

    It is known that the functional relationship between the quantities can be either exact (this case is typical for theoretical inventions), or approximate (which is more typical for experimentally obtained data). This inaccuracy, the deviation of the obtained value from the desired dependence, which is expressed on the graph in the scatter of points at a certain distance from the curve (here I am getting ahead of myself a little) may have several reasons:

    1. Errors of direct measurements (instrumental), errors made by man (here, of course, I am not talking about gross errors that give significant deviations).

    2. The imperfection of human knowledge about nature - by no means all modern scientific concepts make it possible to accurately calculate any values \u200b\u200bfor real cases - many of them are aimed at ideal cases.

    3. The complexity and variability of nature itself (especially living). For example, in the case of sociological research, an exact coincidence of experimental data with theoretical data is not required at all - even a slight correlation of the experimental results with the expected patterns can already tell specialists a lot.

    When choosing an approximation, one should proceed from a specific research problem. Usually, the simpler the equation is used for approximation, the more approximate the resulting description of the relationship. Therefore, it is important to read how significant and what caused the deviations of specific values \u200b\u200bfrom the resulting trend. When describing the dependence of empirically determined values, it is possible to achieve much greater accuracy using some more complex, multiparameter equation. However, it makes no sense to strive with maximum accuracy to convey random deviations of values \u200b\u200bin specific series of empirical data. It is much more important to grasp the general pattern, which in this case is most logically and with acceptable accuracy expressed precisely by the two-parameter equation of the power function. Thus, choosing an approximation method, the researcher always makes a compromise: decides to what extent in this case it is advisable and appropriate to “sacrifice” the details and, accordingly, how general the dependence of the compared variables should be expressed. Along with identifying patterns masked by random deviations of empirical data from the general pattern, approximation also allows solving many other important problems: formalizing the found dependence; find the unknown values \u200b\u200bof the dependent variable by interpolation or, if applicable, extrapolation.


    Formulation of the problem

    1. Using the method of least squares, the function given in the table, approximate

    a) a polynomial of the first degree ;

    b) a polynomial of the second degree;

    c) exponential dependence.

    2. For each dependence, calculate the coefficient of determinism.

    3. Calculate the correlation coefficient (only in case a).

    4. For each dependence, draw a trend line.

    5. Using the LINEST function, calculate the numerical characteristics of the dependence y from x.

    6. Compare your calculations with the results obtained using LINEST.

    7. Draw a conclusion which of the obtained formulas best approximates the function.

    8. Process the specified experimental data using the built-in interpolation (approximation) and regression functions of the MathCAD package and compare the results with the results obtained in Microsoft Excel.


    General information

    In the experimental study of the functional dependence y \u003d f (x), measurements of the value of y are made at various values \u200b\u200bof the value of x. The results are presented in the form of table 1 or graphically.

    X x 1 x 2 ××× x n
    Y x 1 Y 2 ××× y n

    Table 1

    The task consists in the analytical representation of the required functional dependence, i.e. in the selection of a formula describing the results of the experiment. An empirical formula is usually chosen from a fairly narrow class of functions, considering, for example, a set of functions of linear, exponential, exponential, etc. In this case, they are guided by any theoretical considerations or considerations of simplicity of presenting empirical material. The found empirical formula should be such that the values \u200b\u200bof the functions calculated using it at X \u003d x i would probably differ little from the experimental data y i (i \u003d 1, 2,…, n).

    Let us denote the selected functional dependence

    will be minimal. Thus, the parameters a 1, a 2, ..., and m are determined from the condition that the sum of the squares of the deviations of the measured values \u200b\u200by i from took the smallest value.

    Using the necessary conditions for the extremum of a function of several variables, we obtain a normal system for determining the coefficients a 1, a 2, ..., and m

    where a1, a2 are unknown parameters, and system (1.3) takes the form

    where a, b are constants with x\u003e 0 and y\u003e 0.

    Taking the logarithm of equality (1.2.1), we obtain

    and applying formulas (1.1.2), we find the values \u200b\u200bof the parameters b and u, and then the value of the parameter a.

    Exemplary dependence

    Setting v \u003d lny, c \u003d lna, Y \u003d x, we obtain a linear dependence

    Table 3.6

    The smaller the Q value, the better the empirical formula corresponds to the experimental data.


    In each task it is required to use the least squares method to find the theoretical functional dependence for the function given in the table. As a theoretical functional dependence, use:

    - First degree polynomial ,

    - Indicative function,

    - Power function,

    - Second degree polynomial.

    For each dependence, find the theoretical value of the function, the sum of the squares of the deviations of the empirical values \u200b\u200bof the function from the theoretical values, indicate the smallest value of this value and the approximating function to which it corresponds. Plot a trend line for each dependence and show the equation of that line in a diagram. Show on the diagram the value of the coefficient of determinism R 2. This coefficient is calculated by the formula

    , (2.1)

    where are the given values \u200b\u200bof the function,

    The theoretical values \u200b\u200bof the function,

    Arithmetic mean, i \u003d 1, 2,…, n.

    If the coefficient of determinism is 1, then the theoretical and empirical values \u200b\u200bof the function completely coincide. If the coefficient

    determinism is 0, then the theoretical dependence is chosen unsuccessfully.

    Initial data

    Some experiment was done. Its results are written in the form of a table, where x i is the value specified by the researcher (for example, the concentration of reagents in a chemical solution), y i is the measured value (in our example, this can be the rate of the reaction).

    x i y i x i y i x i y i x i y i x i y i
    0.21 1.62 4.98 40.09 7.96 63.31 12.33 97.77 17.32 126.45
    1.19 8.65 5.49 43.56 8.32 67.45 13.21 105.34 18.43 144.34
    2.43 16.76 6.07 48.45 9.43 72.87 14.72 112.56 19.38 160.45
    3.12 24.45 6.81 52.21 10.21 81.34 15.53 121.89 20.45 161.34
    4.54 32.87 7.21 57.34 11.54 89.45 16.23 108.54 21.22 170.59

    table 2

    Calculation of approximations in an Excel spreadsheet

    Introduction

    The main tool for solving complex mathematical problems at the present time are numerical methods, which make it possible to reduce the solution of a problem to performing a finite number of arithmetic operations on numbers; the results are obtained as numerical values. Numerical methods allow one to obtain only a solution to the problem with specific values \u200b\u200bof parameters and initial data.

    Many numerical methods were developed a long time ago, but in manual calculations, they could only be used to solve not too laborious problems. With the advent of computers, a period of rapid development of numerical methods and their introduction into practice began. Only a computer is capable of performing in a short time the amount of computations in the billions, trillions and more operations required to solve many modern problems.

    The numerical method, along with the ability to obtain a result in a reasonable time, must also have another important quality - it does not introduce significant errors into the computational process.

    Approximation

    In the empirical (experimental) study of the functional dependence of one quantity on another, a number of measurements of the function y are made from each specific value of the argument x. The measurement results can be presented graphically or in the form of a table.

    The task is to provide an analytical representation of the functional dependence of y on x, which describes the result of these experiments. The peculiarity of the problem is inaccuracy, errors. During the experiment, the measured values \u200b\u200bof y i contain random errors and measurement errors. The task boils down to getting such a functional dependence that adds up all the errors - smoothes them out.

    The approximating function y (x) is selected from a number of standard and simple ones. Let's designate the functional dependence f (x i; a 1;… a n). (1)

    Here the parameters a and n cannot be determined exactly, they contain errors. To obtain unbiased and consistent estimates of the parameters a 1; … A n - you can use the least squares method.

    The least squares method allows one to obtain unbiased inconsistent estimates of the parameter a 1; … A n. In this case, it is assumed that the measurements of the displacement of the function are made independently of each other and the errors obey the law of the normal probability distribution.

    The essence of the method. If all measurements уi ... yn are made with the same accuracy, then the estimates of the parameters a 1; … A n are determined from the condition that the sum of the squares of the deviation of i is the least.

    y i \u003d y i - f (x i; a 1; ... a n)

    S \u003d (y i - f (x i; a 1; ... a n)) 2

    If the parameters a 1; … A n enter the approximating function (1) linearly, then the system of equations will also be linear; in cases where they are not linear, it is necessary to transform the functional dependence.

    Linear function approximation

    The task comes down to the following. A number of values \u200b\u200bof the function y 1, y 2,…, y n are obtained for the corresponding values \u200b\u200bof the argument x 1, x 2,…, x n. It is necessary to find the values \u200b\u200bof a and b of the expression y \u003d ax + b.

    S \u003d? (yi-f (a * xi + b)) ^ 2

    dS / da \u003d 2 *? (yi-a * xi-b) * xi

    dS / db \u003d 2 *? (yi-a * xi-bi)

    Let's open the sum sign:

    Yi * xi-a *? Xi ^ 2-b *? Xi \u003d 0

    Let's rename the sums,

    where the values \u200b\u200bof A, B, C, D are known from the table.

    Having determined the values \u200b\u200bof a and b, we will solve the posed problem of determining the parameters of the expression y \u003d ax + b.

    y: -10.29, -6.64, -6.70, -4.31, -3.26, -2.20, -0.08, 1.50, 3.81, 3.62

    The graph of the resulting function:

    Power function approximation

    Objective: The objective is as follows. A number of values \u200b\u200bof the function y 1, y 2,…, y n are obtained for the corresponding values \u200b\u200bof the argument x 1, x 2,…, x n. It is necessary to find the values \u200b\u200bof a and b of the expression y \u003d ax b. To do this, let us log this expression:

    ln y \u003d b ln x + ln a

    and make replacements:

    Then we get the familiar expression for a straight line:

    The expression of the function to be minimized for this case will take the form:

    S \u003d? (yi-f (a * xi + b)) ^ 2

    We solve the system by Cramer's method and find the parameters a and b

    Data: x: 2,4,6,8,10,12,14,16,18,20

    y: 0.41, 0.19, 0.10, 0.07, 0.05, 0.04, 0.03, 0.02, 0.02, 0.02

    The graph of the resulting function:

    Parabolic function approximation

    Objective: The objective is as follows. A number of values \u200b\u200bof the function y 1, y 2,…, y n are obtained for the corresponding values \u200b\u200bof the argument x 1, x 2,…, x n. It is necessary to find the values \u200b\u200bof a and b and with the expression y \u003d ax 2 + bx + c.

    The expression of the function to be minimized for this case will take the form:

    S \u003d? (yi-f (a * xi ^ 2 + b * xi + c)) ^ 2

    Finding a and b is reduced to solving the system of equations:

    Differentiate to find the minima:

    dS / da \u003d? (yi-a * xi ^ 2-b * xi + c) * xi ^ 2 \u003d 0

    dS / db \u003d? (yi-a * xi ^ 2-b * xi + c) * xi \u003d 0

    dS / dc \u003d? (yi-a * xi ^ 2-b * xi + c) \u003d 0

    Let's open the sum sign:

    Yi * xi ^ 2-a *? Xi ^ 4-b *? Xi ^ 3-c *? Xi ^ 2 \u003d 0

    Yi-a *? Xi ^ 2-b *? Xi * yi-c * N \u003d 0

    Let's rename the sums:

    G - aE - bD - cC \u003d 0

    F - aD - bC - cA \u003d 0

    B - aC - bA - cN \u003d 0

    where A, B, C, D, E, F, G are known from the table.

    We solve the system by Cramer's method and find the parameters a and b

    Having determined the values \u200b\u200bof a and b and c, we will solve the posed problem of determining the parameters of the expression y \u003d ax 2 + bx + c.

    Data: x: 1,2,3,4,5,6,7,8,9,10

    In the practice of an amateur microcontroller, it is often necessary to work with some kind of analog sensor, for example, a thermistor or thermocouple. Many of the analog sensors have a non-linear response, which is usually plotted in the documentation.

    The horizontal shows the output voltage of the sensor, and the vertical shows the measured value. When developing a program that processes a signal from such a sensor, a problem arises: how can a similar characteristic of a sensor be implemented in the program as a function?

    The simplest method is to replace a complex curved line with a broken line, as shown in the following figure.

    A thin blue line consists of straight line segments that coincide as much as possible with the main curve. It is much easier to work with line segments in the program, since mathematically a straight line is described by a simple equation Y \u003d k * X + c... Of course, replacing a smooth curve with straight sections gives only an approximate picture of the correspondence X and Y, but here you have to make compromises.

    So, we have replaced the original curve with a broken line, that is, we performed a piecewise linear approximation.

    The polyline is defined by coordinates (X; Y) of its break points, i.e., for our case, these are the points (0.8; 83), (2.2; 88), (3; 88), (4.2; 80), (5.2; 70), (7.8; 30), ( 10; 20) and (14.8; 12).

    AB given by the coordinates of two points (XA; YA) and (XB; YB)... A segment is a part of a straight line, and the equation of a straight line, as already mentioned, is described as follows: Y \u003d k * X + c... Since both points lie on a straight line, you can form a system of two equations:

    YA \u003d k * XA + c
    YB \u003d k * XB + c

    In this system, we have two unknowns k and c, therefore, to find them, this system must be solved. I hope that the solution of the system of equations of labor will not be possible, so I do not quote the process itself, but only give a ready-made solution:

    k \u003d (YA - YB) / (XA - XB); c \u003d YA - k * XA

    Thus, returning to our broken line, using the above formulas, we can easily obtain equations for each of the segments of the broken line according to the coordinates of its break points. It remains only to describe the process in C language. (Code)

    #define PT_CNT 8

    typedef struct (
    float X, Y;
    ) POINT;

    POINT line \u003d ((0.8, 83), (2.2, 88), (3, 88), (4.2, 80), (5.2, 70), (7.8, 30), (10, 20), (14.8, 12 ));

    float function (float x) (
    for (int i \u003d 0; i< (PT_CNT-1); i++){
    if ((x\u003e \u003d line [i] .X) && (x<= line.X){
    float k \u003d (line [i] .Y - line.Y) / (line [i] .X - line.X);
    float with \u003d line [i] .Y - k * line [i] .X;
    return k * x + c;
    }
    }
    return 0;
    ) (/ code)

    First, we define the constant PT_CNT, which indicates the number of breakpoints of our polyline. In this case, the beginning and end of the polyline are also the points of its break.

    Then we define a new type POINT to describe the break point. Well, after that we set an array containing all breakpoints. This will be the initial data for further calculations.

    Now we define the function functionwhich receives as a parameter x the measured voltage at the sensor, and returns the corresponding value of the measured parameter calculated according to the broken line graph. It works according to a very simple algorithm.

    Function input parameter x is checked for belonging to one of the segments of the polyline, for which the loop iterates over adjacent pairs of points from the array lineand x is compared to the X coordinates of these points. If the required segment is found, then the formulas are used to calculate the coefficients of the equation of the straight line k and fromand then the function value is calculated.

    If for some reason the parameter value x did not get inside our polyline, the function will return 0, however, this is an extremely slippery case, you should always be sure that this does not actually happen, since it is impossible to distinguish the normally calculated value of the function from this erroneous one.

    As you can see, everything is pretty simple. The code turns out to be quite universal, that is, by redefining the constant PT_CNT and accordingly the contents of the array line you can implement piecewise linear approximation of any curve with any approximation accuracy. And only one thing should be taken care of: so that the curve and its approximating segments never contain vertical pieces, because for a vertical segment, the calculation of the coefficient k will result in division by 0, which means a completely wrong result. Fortunately, real sensors do not create such a situation in their characteristics.