MATLAB: Is there any substitute to collect subs and eval

collectdifferential equationsevalfunctionMATLABode45subs

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.

Best Answer

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.