Using a Symbolic Expression

2 views (last 30 days)
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 Comment
Kevin Bachovchin
Kevin Bachovchin on 10 Apr 2013
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?

Sign in to comment.

Accepted Answer

Kevin Bachovchin
Kevin Bachovchin on 12 Apr 2013
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.

More Answers (1)

Iman Ansari
Iman Ansari on 10 Apr 2013
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 Comments
Kevin Bachovchin
Kevin Bachovchin on 11 Apr 2013
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 on 11 Apr 2013
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!