MATLAB: Dsolve and heaviside (unit step response)

dsolveheaviside

Hi, I'm trying to understand the behaviour of dsolve and heaviside for solving a simple ODE.
syms y(t)
Y = dsolve(diff(y)+y == 1, y(0) == 0)
ezplot(Y,[0,5])
Gives the desired response of a first order LTI ODE to a step, i.e. Y = 1 – exp(-t) (not too worried about negative time). However,
sympref('HeavisideAtOrigin', 1)
Y = dsolve(diff(y)+y == heaviside(t), y(0) == 0)
ezplot(Y,[0,5])
Produces Y = sign(t)/2 – exp(-t)/2 + 1/2 so the value at Y(0) is 0.5, rather than starting from Y(0)=0.
Given the same initial value and forcing function 1 for t>=0, they should produce the same result? Obviously, the first result is the one I was expecting.

Best Answer

Got in touch with Mathworks "bugs" and this is only correct when you include the 'IgnoreAnalyticConstraints', false flag. Without this, the step response is wrong for 1st order, 2nd order .... Apparently "By default , the solver applies some simplifications while solving differential equations which may lead to unexpected results in rare cases". Not sure how rare a step response is though.
syms y(t) Dy = diff(y); D2y = diff(y,2);
% 1st order step Y1 = dsolve(Dy+y==heaviside(t), y(-1)==0); Y2 = dsolve(Dy+y==heaviside(t), y(-1)==0, 'IgnoreAnalyticConstraints', false); figure(1); ezplot(Y2-Y1);
% 2nd order step Y1 = dsolve(D2y+Dy+y==heaviside(t), y(-1)==0, Dy(-1)==0); Y2 = dsolve(D2y+Dy+y==heaviside(t), y(-1)==0, Dy(-1)==0, 'IgnoreAnalyticConstraints', false); figure(2); ezplot(Y2-Y1);