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 Comment

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)

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

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?
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.
May i see your expression?
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.
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 on 11 Apr 2013
Edited: Kevin Bachovchin on 11 Apr 2013
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.
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)
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.
matlabFunction() to create a function handle that is what you would pass into ode45
So how can I get a numeric value for x from the function handle created by matlabFunction?
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.
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)
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.
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!