File Exchange

image thumbnail

EvalEquation

version 1.0.7 (5.4 KB) by Dirk-Jan Kroon
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

1 Download

Updated 13 Aug 2018

View Version History

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 (2020). EvalEquation (https://www.mathworks.com/matlabcentral/fileexchange/68458-evalequation), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)

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!