# Convert polynomial operations inputs in double vector as output

2 ビュー (過去 30 日間)
Nélio Dias 2022 年 7 月 23 日
コメント済み: Nélio Dias 2022 年 7 月 24 日
I am developing a Matlab App to work with control theory, transfer function, and controllers, so I need to build a function that takes the polynomial inputs in a string format from the user and transforms them into numbers vectors. I know that was the symbolic toolbox, but Matlab doesn't allow me to create a standalone version with this toolbox, so I am trying to develop the fuction that will convert this. This is an example of what the function has to do:
converter('2*s^2 + s +1') = [2 1 1]
converter('s*(s+1)*(s+5)') = [1 6 5 0]
converter('(s+1)*(s+3)^2') = [1 8 21 18]

サインインしてコメントする。

### 採用された回答

Matt J 2022 年 7 月 24 日

converter('2*s^2 + s +1')
ans = 1×3
2.0000 1.0000 1.0000
converter( 's*(s+1)*(s+5)' )
ans = 1×4
1.0000 6.0000 5.0000 0.0000
converter('(s+2)*(s+3)^2')
ans = 1×4
1.0000 8.0000 21.0000 18.0000
function p=converter(str)
fn=str2func("@(s)"+vectorize(str));
x=linspace(0,1,100);
y=fn(x);
p=polyfit(x,y,1);
for i=2:20
if p(1)<=1e-6*p(2), break; end
p=polyfit(x,y,i);
end
p(1)=[];
end
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Matt J 2022 年 7 月 24 日
It should work in the Matlab Compiler, though.
Nélio Dias 2022 年 7 月 24 日
Thanks, help me a lot

サインインしてコメントする。

### その他の回答 (1 件)

Walter Roberson 2022 年 7 月 24 日
You should probably approach this as a task in Parsing. It is a lot easier to get this kind of task right if you write a formal definition of what you will accept, such as in Backus-Naur Form (BNF)
At the moment I do not see any BNF tools implemented in MATLAB, so I would suggest that you consider using yacc and lex to create some C code that you can call from MATLAB.
I have a memory that there used to be Blog posts about building "tiny languages" in MATLAB, but I cannot find that.
##### 2 件のコメントなしを表示なしを非表示
Walter Roberson 2022 年 7 月 24 日
Will it always be simple literal constant integers? Integers and fixed point numbers? leading zeros before a decimal point optional? Digits after a decimal point optional? If .5 and 5. are considered valid, so digit before period is not necessary, and digits after period is not needed, then is period by itself an error or to be treated as 0?
Are exponents in floating point accepted? integer followed by exponent without decimal point? Which exponent characters are permitted? e E d D h H?
are expressions such as sqrt(2) permitted? sin(pi/7)? sin(pi*s/7)? sqrt(s^2 + 1)?
is division by an s expression supported?
Is using [] as brackets supported? If so does it mean list building?
Nélio Dias 2022 年 7 月 24 日
I think in integers and to two decimal places

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLogical についてさらに検索

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by