1
votes

I'm fairly new to Matlab and this ODE solver, below is my code:

main.m

format short;

tspan=[0 5];
y0=[0.30;-0.30;0;-0;0];


[t,y]=ode23s(@(t,y) pend(t,y),tspan,y0);

figure(1)
%subplot(2,1,1); 
plot(t,y(:,1),t,y(:,2),'k--')
set(gcf,'Position',[100,500,450,180]);
xlabel('time [s]');
legend('q_1','q_2')
ylabel('leg angle [rad]');

figure(2)
%subplot(2,1,2); 
plot(t,y(:,5))
set(gcf,'Position',[100,500,450,180]);
xlabel('time [s]')
ylabel('locomotion [m]')

pend.m

%the following function contains the right hand side of the
%differential equation of the form
%M(t,y)*y'=F(t,y)
%i.e. it contains F(t,y).it is also stored in a separate filenamed, pend.m.
function yp= pend(t,y)
m = 5;                  %leg masses [kg]        suggested: 5
               %'shin' length [m]      suggested: 0.5
b = 0.5;                %'thigh' length [m]     suggested: 0.5

L = 2*b; 

q=y(1:2);
dq=y(3:4);
Ox=y(5);

rho=0;

k0=50; %Nm/rad

v = 0;
Hsw= L*cos(q(1));  % Height of leg1
Hst= L*cos(q(2));   % Height of leg2
H1 = L - Hsw; 
H2 = L - Hst; 

if dq(1)<0
    Fid1=1;
else Fid1=0;
end
if dq(2)<0
    Fid2=1;
else Fid2=0;
end    

F1 =  -15000*min(H1-0.03*L,0)*Fid1; %N
F2 =  -15000*min(H2-0.03*L,0)*Fid2;

Fc1 = F1*L*sin(q(1));
Fc2 = F2*L*sin(q(2));

Fc=[Fc1;Fc2];
M=[m*b^2 0;0 m*b^2];
Ko=k0*[1 -1; -1 1]+m*9.8*b*[1 0; 0 1];
D=M;

ddq=inv(M)*(-rho*D*dq-Ko*q+Fc);

dOx=0;
 if Fid1==1
    dOx=-L*dq(1)*cos(q(1))*sign(F1);
 end
 if Fid2==1
dOx=-L*dq(2)*cos(q(2))*sign(F2);
 end

 yp=[dq;ddq;dOx];

The problem I am facing here is that the timespan t increases with extremely small steps which continue to decrease with time, e.g. 0.1 to 0.8 in 20s, 0.8 to 0.9 in 5mins and so on which means that it never reaches the time limit hence stuck in the loop.

I have tried different solvers like ode45 also tried giving different values of RelTol and AbsTol to control the step size but failed. It does make a difference for first few steps but then it gets all slow again.

When I use solver ode15s, it gives a warning

"Failure at t=9.246943e-01. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (1.776357e-15) at time t."

and only plots the graph till 0.92 seconds.

Any suggestions or help to solve this problem are welcome.

Thanks.

2

2 Answers

1
votes

ODE solvers with adaptive step sizes expect a smooth ODE function. A 4th order solver expects that the ODE function is at least 4 times continuously differentiable.

Your ODE function has jumps and kinks. The solver senses these as very large and chaotically oscillating higher derivative values. To compensate and restore the convergence order, the step size is decreased, further increasing the computed derivative values as your function is not really differentiable, until the step size increment becomes smaller than the smallest effective floating point increment, triggering the error you see.

Use "events" to stop and restart the integration process at exactly those non-smooth model changes.

1
votes

You may deal with the quantity at different scale.

For example; Compute the time (t) as 'millisecond' instead of as 'second'. This gives (or reserves) extra 3 decimal places to allow appropriate increment of the smallest step, Δt ≥ 1.776357e-15.

Take note: If there is/are another physical quantit(ies) fundamentally derived from the quantity t, as in velocity ('meter per second'), make sure the scales/units are equivalent. In case of the example given, the unit of velocity in a computation must be in 'meter per millisecond'.

Extra note; convert back to appropriate scale if necessary when returning required final values.