Is there any substitute to collect subs and eval ?
1 次查看(过去 30 天)
显示 更早的评论
I am using ode45 to solve an equation.
Follwing line is from the parent file where I call my function. I am passing all the required parameters in my function.
[t,x] = ode45(@(t,x) eqns_v8(t,x,xg,para),t,x0);
In my function file, I am using collect(), subs() and eval() . The collect() function is taking to long to run. Is there any substitute to the way I coded it??
sets = [x(2) ; 1/(Md*tan(theta)+Mw*Phi)*(-(Phi*Psi+Ks*tan(theta))*x(1)+Phi*Psi*eval(subs(collect(xg,t)))+(Md*g+Ks*y_bar)*sign(x(1)))];
xg is sym which contains a part of equation which I want to use. Here is the way I got xg. This part is coded in parent file and xg is passed in my function as argument.
syms t;
ag = (F*sin(Omega*t))/Mw; % Ground Acceleration
xg = int(int(ag));
I want to use the equation which I get after double intergration in another equation, Is there any other way ? Any help is welcome, Thanks in advance.
0 个评论
采纳的回答
Walter Roberson
2017-5-31
Never eval() a symbolic expression: symbolic expressions are in a language which is not quite MATLAB, and you can encounter incompatibilities when you execute them.
You should be removing the
eval(subs(collect(xg,t)))
in the code, and in the function that you are calling the ode, you should use matlabFunction to generate a function handle that you can then pass in to the ode and call inside the ode. To do this in the general case, you will need to decide which variables are eligible to appear in xg, and use the 'vars' option of matlabFunction so that you know in advance the order of the arguments so you can make a proper function call.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!