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개)
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
2013년 4월 10일
Iman Ansari
2013년 4월 10일
May x=eval(SymExpr) help you but:
Kevin Bachovchin
2013년 4월 10일
Iman Ansari
2013년 4월 10일
May i see your expression?
Kevin Bachovchin
2013년 4월 10일
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
2013년 4월 11일
편집: Kevin Bachovchin
2013년 4월 11일
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
2013년 4월 11일
Walter Roberson
2013년 4월 11일
matlabFunction() to create a function handle that is what you would pass into ode45
Kevin Bachovchin
2013년 4월 11일
Kevin Bachovchin
2013년 4월 11일
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
2013년 4월 11일
Kevin Bachovchin
2013년 4월 11일
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!