EvalEquation

版本 1.0.7 (5.4 KB) 作者: 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
126.0 次下载
更新时间 2018/8/13

查看许可证

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

引用格式

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

MATLAB 版本兼容性
创建方式 R2018a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
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