function [Value_out, Error_out] = MultyVarError (formula, varargin)
% Version 1.2.
%
% This function calculates the error of a dependent variable
% according to the given errors of number of independent variables.
% First arguement is a text formula of the dependence, then independent
% variables must be provided: variable's name (text), its value, and
% error. Example:
%   MultyVarError ('x^y', 'x', 4, 0.1, 'y', 2, 0.2)
% If no return values are expected, the result is printed in the console,
% if two values are expected, they are assigned with the value of the
% function in the calculating point and its error. Otherwise, an error
% message is generated.

    if (rem (nargin - 1, 3) ~= 0  ||  ~ischar (formula))
        error ('Wrong arguement list.')
    end
    N = (nargin - 1) / 3;
    Vars = cell (1, N);
    Values = cell (1, N);
    for i = 1:N
        Vars{i} = varargin{3*i - 2};
        Values{i} = varargin{3*i - 1};
    end
    sum = 0;
    for i = 1:N
        sum = sum + (subs (diff (formula, Vars{i}), Vars, Values) * ...
                    varargin{3*i})^2;
    end
    Value = subs (formula, Vars, Values);
    Error = subs (sqrt (sum));
    if nargout == 2
        Value_out = Value;
        Error_out = Error;
    elseif nargout == 0
        fprintf ('Value = %d +/- %d.\n', Value, Error);
    else
        error ('Wrong return values list.')
    end
end