File Exchange EvalEquation

version 1.0.7 (5.4 KB) by Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

This function can be used to evaluate an (list of) equation(s) written as string (char-array). It is based on the Shunting-yard algorithm

Updated 13 Aug 2018

This function can be used to evaluate an equation written as string (char-array).
The basis of the function is the Shunting yard algorithm, as in the C#
example of: https://rosettacode.org/wiki/Parsing/Shunting-yard_algorithm

The shunting-yard algorithm is a method for parsing mathematical expressions
specified in infix notation to postfix notation.

Example

result = EvalEquation( '1+2/(2*3)');
disp(result)
1.3333

or with variables,

result = EvalEquation( 'y+x/(2*3)','x',2,'y',3);
disp(result)
3.3333

Supported operators: ^ * / + - ( ) > < == >= <=
Supported functions: sqrt abs sin cos tan asin acos atan exp sign round floor ceil
max(a,b) min(a,b)
Supported constants: pi
************************************************************************************************************************************

This function EvalEquationScript can be used to evaluate a list of equations
and finaly return one of the variables as result

result = EvalEquationScript(script, output_variable_name, variable1, value1, variable2, value2,....)

Example,

script = cell(5,1);
script{1}='a=1';
script{2}='b=a*2';
script{3}='%Calculate max of a,b time x';
script{4}='x=x+a';
script{5}='c=max(a,b)*x';

result = EvalEquationScript(script,'c','x',3);

 13 Aug 2018 1.0.7 Fixed bug in parsing a number like -1.e-31 13 Aug 2018 1.0.6 Added text 13 Aug 2018 1.0.5 Added the missing file.. 13 Aug 2018 1.0.4 Added multi-line support in EvalEquationScript 13 Aug 2018 1.0.3 Now explicit support of arrays as value of an input variable. Also made compatible with Matlab Coder 12 Aug 2018 1.0.2 Support for more operators and functions 10 Aug 2018 1.0.1 Added supported functions: sqrt abs sin cos tan asin acos atan exp
MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux