Symbolic or function handle to numeric

Is there a way to turn a symbolic expression or function handle into a numeric expression? For example, say i have:
syms x
y=x^2;
and would like to turn it into to this
y=x^2;
Where x is no longer a symbolic variable such that when I give x a value like
x=[1 2 3];
and I type
y
I get
y=[1 4 9];
I know function handles will do this but matlab coder can not take function handles, so is there a way to do this?
Thanks
Drew

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 20 日
編集済み: Azzi Abdelmalek 2016 年 4 月 20 日

0 投票

syms x;
y=x^2;
out=subs(y,[1 2 3])

3 件のコメント

Drew Mitchell
Drew Mitchell 2016 年 4 月 20 日
Thanks, I might not have been clear enough:
Im not actually interested in the output, I am interested in making the string 'y=x^2' become y=x^2 so that I can put it in a standalone function that I can use with matlab coder. If I do what you propose I will get an error because coder cant handle symbolic variables.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 20 日
編集済み: Azzi Abdelmalek 2016 年 4 月 20 日
have you tried inline function?
y=inline('x.^2')
Drew Mitchell
Drew Mitchell 2016 年 4 月 20 日
That is also not supported by coder.
I appreciate the responses though Thanks!!

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

Walter Roberson
Walter Roberson 2016 年 4 月 21 日

0 投票

syms is not supported by the coder either. So anything having to do with symbolics has to be done and over with before code is generated. At the time you are running the symbolic code, you can use matlabFunction to write to a file.

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

質問済み:

2016 年 4 月 20 日

回答済み:

2016 年 4 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by