2
votes

I have symbolic variables which are:

[q dq ddq n]

q=[q1; q2];
dq=[dq1; dq2];
ddq=[ddq1; ddq2];

and variable n is

 n=[2*dq1 + (83*dq1*dq2)/400;
   (83*dq1^2)/800 + 2*dq2]

I'm trying to obtain the values of an ODE function using the variables:

state=[q; dq];
u=[5;5];

q1=state(1);
q2=state(2);
dq1=state(3);
dq2=state(4);

dxdt1=dq1;
dxdt2=dq2;
ddxdt=inv(B)*(u-n);  %which is 2x1 matrix
                     %B is a 2x2 numeric matrix
dstate=[dxdt1; dxdt2; ddxdt];

using this variables I try to obtain the solution of the equation

t0=0;
tf=1;
N=10;
tspan=linspace(t0,tf,N);

x0=[0.5; 0.5;zeros(2,1)];
function_q=@(tspan,state)ode_system(tspan,state,B,n,u);

where odesystem is

function dydt=ode_system(time, state,B,n_t,u)
q1=state(1);
q2=state(2);
dq1=state(3);
dq2=state(4);

dxdt1=dq1;
dxdt2=dq2;
ddxdt=inv(B)*(u-n_t);
dydt=[dxdt1; dxdt2; ddxdt]

end

at the end I try to calculate

[tout,xout]=ode45(function_q, tspan, x0)

when I run the code it gives an error and say

Error using odearguments (line 110) Inputs must be floats, namely single or double.

Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in mainfunc (line 140) [tout,xout]=ode45(function_q, tspan, x0) try open('Error using odearguments (line 110) ↑ Error: String is not terminated properly.

I think the problem is resulted from to put symbolic variable to ode45 function. How can I solve this problem? How can I define n matrix in numerical form

1

1 Answers

2
votes

The problem is the type of the return value from ode_system. Because n_t is symbolic, dydt is symbolic and ode45 doesn't like that. Within ode_system, try using subs to replace the variables in n_t with their corresponding values. And then use double to convert the symbolic number to a "regular" number. This will be slow if you make a bigger system, but it should be fine for now.

function dydt=ode_system(time, state,B,n_t,u)
q1=state(1);
q2=state(2);
dq1=state(3);
dq2=state(4);

dxdt1=dq1;
dxdt2=dq2;
free_vars = sym({'dq1','dq2'});
free_values = [dq1,dq2];
n_t = subs(n_t, free_vars, free_values);
n_t = double(n_t);
ddxdt=inv(B)*(u-n_t);
dydt=[dxdt1; dxdt2; ddxdt]

end