Tabulation function: how to write a program?
Tabulation of the function is a classical problem of mathematics and programming. It consists in the sequential finding of magnitude f (x) at varying values x. The results of calculations are most often output in the form of a table of two lines. The first corresponds to x, the second - f (x).
Theory
The algorithm for determining the values of a function consists of six steps.
- Selection of the initial and final value of the argument, the number of points.
- Calculation of the step is the value by which the argument will change.
- The argument is assumed to be equal to the initial value.
- Function calculation.
- Increase the argument by the step value.
- Repeat steps 4-5 until the required number of points is counted.
The values set in the first step are not alwaysthey can be specified by the task. In practice, there is a situation where a range of values and a step of calculations is specified. Tabulation of the function in this case does not require finding the number of points, since the condition of the termination of the calculation (point 6 of the algorithm) is the equality of the argument to the specified finite value.
Practical example
An example will help to understand how to use theory. Let there be given a quadratic function g (x) = x2 + 9. We compose a table of its values in the range [-2; 2], taking the number of points equal to five. From the initial data, it is easy to estimate that the calculation step should be equal to 1.
In accordance with the algorithm, the next action is the calculation g(-2), "-2" is the initial value of the function. Consistently increasing x per unit (in programming this operation is called incrementing) and defining the function g, the function is tabulated.
x | -2 | -1 | 0 | 1 | 2 |
g (x) | 13 | 10 | 9 | 10 | 13 |
Checking the correctness of calculations is easy - you should get a graph of the parabola.
Software implementation
Creating a function table manually - lessonlong. Calculations should be carried out carefully, an error in the calculation will make the remaining values also incorrect. The solution is to transfer the task to the computer.
Below is a tabulation functionon the so-called "pseudocode". To execute it, you must specify a function, the initial and final value of the arguments, the number of points. In the example, the f (x) = 18 * x + 5. The result of the program is the sequentially derived values x, f (x).
- Argument: = N.Value.
- Calculation step: = (N. value - K. value) / Col. points.
- FOR (Counter: = 0 to the Number of points).
Start:
- Function: = 18 * Argument + 5.
- Argument: = N. value + Counter * Step.
- Output to the screen (Argument, Function).
The end.
The code adapts to any programming language. That is, tabulation of the function can be implemented in Pascal, C +, C # and even in the language of office programming VBA, integrated into the package of MS Office.