# LGF_RegressionLine (FC / 3.0.1)

Overview

Author: Siemens Digital Industry

## Short description

The simplest case of a regression is the regression line. This means that the assumed relationship between the input and output signal is a linear straight line.

Figure: Regression line

### Block Interface

| LGF_RegressionLine (FC) |
--- | --- | ---
|  |  |  |  | Ret_Val |  |  | Void
|  |  |  |  | slope |  |  | LReal
|  |  |  |  | intercept |  |  | LReal
|  |  |  |  | error |  |  | Bool
|  |  |  |  | status |  |  | Word
Array[*] of LGF_typeRegressionLine |  |  | values |  |  | Array[*] of LGF_typeRegressionLine

### Output parameter

Identifier | Data type | Description
--- | --- | ---
Ret_Val | Void | Void - Function has no return value
slope | LReal | Gradient of straight line
intercept | LReal | The intersection with the Y axis
error | Bool | FALSE: No error TRUE: An error occurred during the execution of the FB
status | Word | 16#0000-16#7FFF: Status of the FB 16#8000-16#FFFF: Error identification (see following Table)

### In/Out parameter

Identifier | Data type | Description
--- | --- | ---
values | Array[*] of LGF_typeRegressionLine | The data points are transferred with their X- and Y-values. The data type `LGF_typeRegressionLine` has the following structure: • x (Real) • y (Real)

### Status & Error codes

Code / Value | Identifier / Description
--- | ---
16#0000 | STATUS_EXECUTION_FINISHED Status: Execution finished without errors
16#8200 | ERR_NOT_ENOUGH_VALUES Error: Not enough Values. The block requires at least two pairs of values to calculate a regression line. Increase the size of the array at the input parameter `values` in the second dimension.

## User defined datatype(s)

### LGF_typeRegressionLine (UDT / V3.0.1)

The data type is for transferring datapoints (Key- Value pairs) to `LGF_RegressionLine` and calculate the interpolated linear equation parameters slope and intercept.

Identifier | Data type | Default value | Description
--- | --- | --- | ---
x | Real | 0.0 | X-Axis value
y | Real | 0.0 | Y-Axis value

## Functional description

The block calculates the regression line with the following line equation:
f(x) = m \cdot x + t
m: Gradient of straight line
t: Intersection with y-axis
N: number of array elements

The gradient m is calculated using the following equation:

m = \frac{n \cdot \sum_{1}^{N}(x(n) \cdot y(n)) - (\sum_{1}^{N}x(n) \cdot \sum_{1}^{N}y(n))}{n \cdot \sum_{1}^{N} x^2(n) - (\sum_{1}^{N}x(n))^2}

The intersection t with the Y axis is calculated using the following equation:

t = \frac{\sum_{1}^{N} y(n)}{N} - b \cdot \frac{\sum_{1}^{N} x(n)}{N}

## Change log

Version & Date | Change description
--- | ---
| 1.0.0 | Siemens Industry Online Support
| 23.11.2018 | First released version
| 1.0.4 | Simatic Systems Support
| 15.11.2019 | Code refactoring, comments added
| 3.0.0 | Simatic Systems Support
| 23.04.2020 | Set version to V3.0.0, harmonize the version of the whole library
| 3.0.1 | Simatic Systems Support
| 06.04.2021 | Insert documentation