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.