clear all;


R=20; L=2e-3; C=40e-6;
global E=40;
A=[-1/(R*C) 1/C; -1/L 0];
b1=[0 0]'; b2=[0 E/L]';
global u_ref=32;
x_1=[u_ref u_ref/R]';

%Equacion diferencial
function retval=f2(x,u,t,params)
  retval=params.A*x + (params.b2).*u(t);
end


function retval=control_opt2(t)
  freq=1e7;
  retval=(square(t*freq/(2*pi), 0.8)+1)/2; % 2pi para la normalizacion, see help square  
end

params=struct("A", A, "b1", b1, "b2", b2);

t=[linspace(0,1e-3,10000+1)];

%The bug is triggered here. To see the difference uncomment the line with ode4
%and compare the results.
x=lsode(@(x,tau) f2(x, @control_opt2 ,tau, params), [u_ref u_ref/R]', t);

%[t x]=ode23(@(tau,x) f2(x, @control_opt2 ,tau, params), t, [u_ref u_ref/R]');


plot(t, x(:,1),t,x(:,2), t, arrayfun(@control_opt2 ,t))
