Using a Symbolic Expression

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 日

0 投票

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 日

1 投票

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 月 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?
Iman Ansari
Iman Ansari 2013 年 4 月 10 日
Kevin Bachovchin
Kevin Bachovchin 2013 年 4 月 10 日
I also tried eval, but it is the same problem. Is there any other way I can do this? I'm just looking to feed x = the expression in SymExpr. It seems strange that when I can copy and paste and get the result quickly, that there is no automated way to do this.
Iman Ansari
Iman Ansari 2013 年 4 月 10 日
May i see your expression?
Kevin Bachovchin
Kevin Bachovchin 2013 年 4 月 10 日
The expressions will vary, depending on the system it is that I am deriving the differential equations for with my first function.
As an example, here is a differential equation I found with my first function:
domegadt = -(2*tauL + 2*B*omega + 2*M*iR*iSa*sin(theta) - M*iR*iSb*sin(theta) - M*iR*iSc*sin(theta) - 3^(1/2)*M*iR*iSb*cos(theta) + 3^(1/2)*M*iR*iSc*cos(theta))/(2*J)
The other symbolic variables have numeric values in the 2nd function.
Any ideas would be greatly appreciated.
Iman Ansari
Iman Ansari 2013 年 4 月 11 日
clear all;
close all;
tic
syms omega tauL B M iR iSa theta iSb iSc J
domegadt = -(2*tauL + 2*B*omega + 2*M*iR*iSa*sin(theta) - M*iR*iSb*sin(theta) - M*iR*iSc*sin(theta) - 3^(1/2)*M*iR*iSb*cos(theta) + 3^(1/2)*M*iR*iSc*cos(theta))/(2*J);
domegadt=subs(domegadt,[omega tauL B M iR iSa theta iSb iSc J],[1+7.6765776i 2.8788798+765i 4.7687 1.878 8.13233+0.576i 5.876886 57.87 pi/7.65765 64343+3543i 90])
toc
domegadt =
2.9433e+03 + 3.6315e+02i
Elapsed time is 0.966587 seconds.
Kevin Bachovchin
Kevin Bachovchin 2013 年 4 月 11 日
編集済み: Kevin Bachovchin 2013 年 4 月 11 日
As I mentioned before, the function where I'm assigning a value to domegadt is being called by the MATLAB differential equation solver ode45. Therefore this function is being called many times (I'm not sure exactly how many, but I would guess hundreds or even thousands of times.) Therefore the difference between using the subs or eval command vs. just using the expression really adds up.
Is there any automated way I can just obtain the expression I would copy/paste from the symbolic variable? That's what I am looking to do.
Iman Ansari
Iman Ansari 2013 年 4 月 11 日
編集済み: Iman Ansari 2013 年 4 月 11 日
May this is what you want:
syms a b c;
SymExpr = a*b*c;
x=char(SymExpr);
a=1;b=2;c=3;
eval(['x=' x]);
or
x=eval(x)
Kevin Bachovchin
Kevin Bachovchin 2013 年 4 月 11 日
Same issue with the speed. Somehow I need to just extract the text from the symbolic equation. I can't call eval inside my function because it is getting called hundreds or even thousands of times, and with so many iterations, the difference in run-time is so pronounced.
Walter Roberson
Walter Roberson 2013 年 4 月 11 日
matlabFunction() to create a function handle that is what you would pass into ode45
Kevin Bachovchin
Kevin Bachovchin 2013 年 4 月 11 日
So how can I get a numeric value for x from the function handle created by matlabFunction?
Kevin Bachovchin
Kevin Bachovchin 2013 年 4 月 11 日
To clarify, ode45 is calling a function I wrote called DynamicModel. Inside DynamicModel I need to assign values for dx1dt, dx2dt, dx3dt, dx4dt, etc based on the saved symbolic expressions derived previously from another function. The straightforward way to assign the values for dx1dt, dx2dt, dx3dt, dx4dt is just to copy/paste, but I'm trying to do this without copying/pasting.
Iman Ansari
Iman Ansari 2013 年 4 月 11 日
syms a b c;
SymExpr = a*b*c;
x=matlabFunction(SymExpr);
a=1;b=2;c=3;
x(a,b,c)
Or
x(1,2,3)
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