Using a Symbolic Expression

5 次查看(过去 30 天)
Hello,
I have one function that creates a symbolic expression for a variable, which is often lengthy. In another function, I am loading that symbolic expression and would like to evaluate that symbolic expression based upon parameters defined above.
For example, in my first function I solve for x to have the symbolic expression SymExpr = a*b*c. In my second function, I have a, b, and c defined as numeric values. I then load the saved symbolic expression SymExpr. I tried saying x = SymExpr, but this makes x a symbolic variable and x = char(SymExpr) but this makes x a string. I want x to be a double and I want to obtain the numeric value.
I know I could do this by just copying and pasting in the second function x = a*b*c, but my expressions from the first function can get very lengthy, so I'm trying to do this without having to copy/paste.
Thank you,
Kevin
  1 个评论
Kevin Bachovchin
Kevin Bachovchin 2013-4-10
I'm using this expression for x in conjunction with the MATLAB differential equation solver od45. When I just copy paste in the expression for x, it takes less than a minute for ode45 to run. When I use x=subs(SymExpr), it has been running for the past 10 minutes and still hasn't finished. Do you have any idea why this is, or how I can improve the speed?

请先登录,再进行评论。

采纳的回答

Kevin Bachovchin
Kevin Bachovchin 2013-4-12
For the expressions that exceed the maximum command line length, I was able to print them to a file instead of the command line and copy them from the file and paste into my function. There has to be an easier way of doing it though. There's probably some really simple solution that doesn't involve copying and pasting.

更多回答(1 个)

Iman Ansari
Iman Ansari 2013-4-10
Hi
syms a b c;
SymExpr = a*b*c;
x=subs(SymExpr,[a b c],[1 2 3])
or
syms a b c;
SymExpr = a*b*c;
a=1;b=2;c=3;
x=subs(SymExpr)
  15 个评论
Kevin Bachovchin
Kevin Bachovchin 2013-4-11
Same issue with run-time as when I use eval or subs. Somehow I need to just extract the text from the symbolic equation in automated way. It seems like there should be some easy way to do this.
I can't do manual copy and paste of the symbolic equation for some larger systems because the symbolic equation exceeds maximum line length of 25,000 characters so the output gets truncated when I try to print to the command window in order to copy/paste.
Kevin Bachovchin
Kevin Bachovchin 2013-4-11
For the expressions that exceed the maximum command line length, I was able to print them to a file instead of the command line and copy them from the file and paste into my function. There has to be an easier way of doing it though. There's probably some really simple solution.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by