1
votes

I need help solving the following differential equation using the function ode45 from matlab. The issue I have is a time dependent function on the equation.

Equation:
d(C(t)*W(t))/dt = F1-F2

F1 and F2 are constants that I would like to pass on to the function. Here is an example of the code I'm using right now. In order to solve the ODE I'm using Euler's Method but I would like to use ODE45 or the appropiate ODE solver in order to compare results. Error values in Euler's Method could lead to false information.

T  = 0.0125;        %Duration (minutes)
dt = 0.01*T;       %Time step duration (minutes)
nsteps = 15*T/dt;   %Total number of timesteps
R = 0.01;           %Resistance
P1 = 2;             %Pressure 1
P2 = 5;             %Pressure 2
P3 = 80;            %Pressure 3
F1 = (P1-P2)/R;     %Flow 1
F2 = (P2-P3)/R;     %Flow 2
Cmin = 0.00003;     %Min value of Cfunction
Cmax = 0.0146;      %Max value of Cfunction
tplot = zeros(1,nsteps); %Allocate memory to save values.
P3plot = zeros(1,nsteps); %Allocate memory to save values.
%EULER'S Method.
for i=1:nsteps
    t = i*dt;
    Cnew = Cfunction(t+dt,Cmin,Cmax);
    Cold = Cfunction(t,Cmin,Cmax);
    P3 = ((F1-F2)*dt/Cnew)+((Cold*P3)/Cnew);
    P3plot(i) = P3;    %Save pressure values.
    tplot(i) = t;         %Save time values
end
plot(tplot,P3plot)

And this is the function for C:

function CV=Cfunction(t,CVS,CVD)
T =0.0125;          %Duration 
TS=0.0050;          %Duration 
tcS=0.0025;         %time constant
tcD=0.0075;         %time constant
tc=rem(t,T);        % tc=time in the current cycle, 
if(tc<TS)
  e=(1-exp(-tc/tcS))/(1-exp(-TS/tcS));
  CV=CVD*(CVS/CVD)^e;
else
  e=(1-exp(-(tc-TS)/tcD))/(1-exp(-(T-TS)/tcD));
  CV=CVS*(CVD/CVS)^e;
end

Thanks in advance.

1
What have you tried so far? Have you looked through the documentation, e.g., this page on numerically solving ODEs, and tried any examples? This is a programming site so you should edit your question to turn the equation into code.horchler
I went ahead and edited the post to include the current code I have. However, I'm not using any of the ODE solvers. To solve the ODE I'm implementing Euler's Method. I would like to find out how to solve the ODE using any of the solvers in order to compare results and determine the error values of using Euler's Method.iamlucho

1 Answers

1
votes

It looks like your equation has a simple analytic solution. Assuming all arguments are double, Whandle is a function handle and Whandle(t) is nonzero, integration over [0,t] etc.

function y = C(t, Whandle, F1, F2)
   y = 1/Whandle(t) * (F1 - F2) * t
end