Using a Symbolic Expression

조회 수: 3 (최근 30일)
Kevin Bachovchin
Kevin Bachovchin 2013년 4월 10일
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