I had to solve a similar problem, solving
dx/dt= (b(t)-x(t)/tau
where b(t) is a step function.
To solve i wrote 3 files. Two files for the step signal, i.e. the stepsignal function and the r.h.s function that is used in it.
The third file(script) solves dx/dt= (b(t)-x(t)/tau by calling the other two files and then plots
dx/dt= (b(t)-x(t))/tau and the step signal on the same plot to compare the two.
These three functions/script are shown below. Hope this is of some use.
Andrew Boardman
MID RNR
function dxdt=steprhs(t,x)
tau=1;
dxdt=(stepsignal(t,5)-x)/tau; %uses function stepsignal.m
%
%Written by
%Andrew Boardman 11/03/03
%
%This function is the
%rhs of the ode dy/dx = ( b(t) - x(t) ) / tau
%where b(t)=f step (t,5)
%the rhs is used by solvestepsignal.m
function signal=stepsignal(t,t0)
%function signal = stepsignal(t,t0)
%
%if t<t0 we return 0, otherwise we return 1
%
if t<t0
signal=0;
else
signal=1;
end
%
%written by
%Andrew Boardman 11/03/03
%
%This function is the step-function
%f step (t,t0) as defined on sheet 4
%This function is solved and plotted
%by solvestepsignal.m
t=0:0.1:20;
for i=1:length(t)
s(i)=stepsignal(t(i),5);
end
plot(t,s,'-r')
grid on
hold on
xlabel('t')
ylabel('input and output response')
%
[t,x]=ode45('steprhs',[0 20],0);
plot(t,x,'-o')
legend('stepsignal(t,5)','x(t)')
hold off
%
%written by
%Andrew Boardman 11/03/03
%
%This file solves and plots the
%ode dy/dx = ( b(t) - x(t) ) / tau
%where b(t)=f step (t,5)
%also plots f step (t,5) against t