Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I sovle a non-homogenous 1st order ODE?

Status
Not open for further replies.

rocketscientist201

Aerospace
Joined
Apr 14, 2003
Messages
1
Location
GB
How do you solve:

dx/dt= b(t)-x(t)

where b(t) is a step function
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top