Using math formulas for generating data

php-RGraph provides two php functions in addition to the rgraph_chart and rgraph_options classes. These two functions are used to generate data for line or scatter charts.

calculate_Regression

Description
function does a basic regression analysis. It provides three builtin calculations (all based on the linear regression model) which are described by following functions:
  1. f(x) = ax + b (linear regression)
  2. f(x) = abx (exponential regression)
  3. f(x) = axb (exponential regression)
Function Call
calculate_Regression($x_values, $y_values, $user_formula=null, $number_format="en", $precision=2)
Parameter
$x_values and $y_values are arrays of numerical values
$user_formula - you may provide own regression formula which then will be used
$number_format - defines the number format of calculated output formula
$precision - defines the decimal scale of calculated output formula
Return Value
an array containing arrays of x/y pairs and a calculated regression formula
Example
$result = calculate_Regression($x_values, $y_values);
$data = $result['values'];
$formula = $result['formula'];

getDataviaFormula

Description
function generates data based on a provided math formula
Function Call
getDataviaFormula($formula, $x_interval=1, $x_min=0, $x_max=5, $max_iterations = 500)
Parameter
$formula - Notation of your formula is derived by script-language "PHP". Pls. read following related information:
  1. You may specify formula with or without equal sign e. g. formula="y = x * e^x" or formula="x * e^x"
  2. Independent variable must be set to "x"
  3. Creation of data is controlled by four parameters:
    1. "$x_min" - x-value for start of calculation
    2. "$x_max" - x-value for end of calculation
    3. "$x_interval" - x-increment for calculation
    4. "$max_iterations" maximum of calculation steps

    Calculation stops if either x_max or $max_iterations are reached.
  4. Constants:
    • Pi (π): "pi"
    • Euler's number (e): "e"
  5. Operators:
    • Add: "+"
    • Subtract: "-"
    • Multiply: "*"
    • Divide: "/"
    • Power: "^"
  6. Functions
    • sin
    • sinh
    • arcsin
    • asin
    • arcsinh
    • asinh
    • cos
    • cosh
    • arccos
    • acos
    • arccosh
    • acosh
    • tan
    • tanh
    • arctan
    • atan
    • arctanh
    • atanh
    • sqrt
    • abs
    • ln
    • log10
    • log
Return Value
an associative array with keys "x_values", "y_values" or in case of an error "error" has error infos stored and x_values and y_values are empty (not set).
Example
$data = getDataviaFormula("y=2*e^-(0.2 * x)*sin(x * 2*pi)", 0.1, 0, 20);
$x_values = $data['x_values'];
$y_values = $data['y_values'];