EvalEquation

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
130 Downloads
Updated 13 Aug 2018

View License

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);

Cite As

Dirk-Jan Kroon (2024). EvalEquation (https://www.mathworks.com/matlabcentral/fileexchange/68458-evalequation), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.7

Fixed bug in parsing a number like -1.e-31

1.0.6

Added text

1.0.5

Added the missing file..

1.0.4

Added multi-line support in EvalEquationScript

1.0.3

Now explicit support of arrays as value of an input variable.
Also made compatible with Matlab Coder

1.0.2

Support for more operators and functions

1.0.1

Added supported functions: sqrt abs sin cos tan asin acos atan exp

1.0.0