# odeFunction error- Only variables and declared parameters can be symbolic

9 views (last 30 days)
Commented: AdarG on 17 Aug 2019
Hi,
I have the following code (deriving the Lagrangian equations). I get an error when I call odeFunction:
Found symbolic object 'k' in DAEs. Only variables and declared parameters can be symbolic.
Error in sym/odeFunction (line 118)
A = feval(symengine, 'daetools::odeFunction', expr, vars, params{:});
I also get an error (different one) when using matlabFunction
What's wrong? How can I convert to numeric function handle to solve with ode solvers?
my code:
clc
clearvars
syms y theta(t) thetad(t) r(t) rd(t) rd_f thetad_f r0(theta) k(theta) m g cr ctheta
y=symfun(r(t)*cos(theta(t)),t);
T=m/2*(xdot^2+ydot^2);
V=k(theta(t))/2*(r0(theta(t))-r(t))^2+m*g*cos(theta(t))*r(t);
L=T-V;
% derivatiation Lagrangian to q and qdot
dLdq=[functionalDerivative(L,r);functionalDerivative(L,theta)];
% defining sym functions (derivatives)
rd_f=diff(r(t),t);
% subsituting from symbolic vars [rd,thetad] to sym functions
dLdqdd_f=diff(dLdqd_f,t);
% fromulation the ODE equations
eq=dLdqdd_f-dLdq_f+losses;
[eqs,vars] = reduceDifferentialOrder(eq,[r,theta]);
[M,F] = massMatrixForm(eqs,vars);
% transfering to ode handels
f=M\F;
odefun = odeFunction(f,vars,r0, k, m, g ,cr, ctheta); % I get an error
ff=matlabFunction(F); % I get an error here too
Srivardhan Gadila on 13 Aug 2019
Are the functions "k" and "r0" known?

Hi, yes they are known

Srivardhan Gadila on 13 Aug 2019
In that case declare the functions r0, k using function handle & remove their symbolic declarations in the line 3 (syms r0(theta) k(theta)) and also remove the r0, k from the odeFunction's arguments. Then try running the code.
Thanks, but what if I don't know in advance the functions of r0(theta), k(theta)?
Can I get a general expression to the derivatives, like r0'(theat) and k'(theta)?

R2018b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!