Solving a set of differential equations using ode45 with a time dependent parameter
Show older comments
Hello,
I'm trying to solve a set of differential equations using ode45 (code below). The parameter 'Flux' is time dependent. At this moment the ModelFotoconversie_ODE function creates an array with the values of 'Flux' at each time. How can I adapt my code in such way that the ModelFotoconversie_ODE function only calculates the parameter 'Flux' at the specific time t ? _________________________________________________________________________________________________________ function [t,C] = ODE(t, C_SP0,C_MC0,pars,Vtot,L,eps,Flux,Nu,RatioDark)
tspan = [0 t(end)];
y0 = [C_SP0 C_MC0];
[t, C] = ode45(@(t,C) ModelFotoconversie_ODE(t,C,pars,Vtot,L,eps,Flux,Nu,RatioDark), tspan, y0);
end
_________________________________________________________________________________________________________
function [ DC ] = ModelFotoconversie_ODE(t,C,pars, Vtot,L,eps,Flux,Nu,RatioDark)
k(1,1)=pars(1);
k(1,2)=k(1,1)*RatioDark;
QE(1,1)=pars(3);
QE(1,2)=pars(4);
N_Av= 6.022140857e23;
h = 6.626*10^(-34); %J.s
Abs_tot = [eps(1,1)*C(1)+eps(1,2)*C(2)' , eps(2,1)*C(1)+eps(2,2)*C(2)'];
Flux_Transm = Flux.*10.^(-Abs_tot.*L);
N_abs_tot = ((Flux- Flux_Transm)./(h*Nu))/1000;
N_Abs_UV = [eps(1,1)*C(1) eps(1,2)*C(2)].*(N_abs_tot/Abs_tot(1,1));
N_Abs_Geel = [eps(2,1)*C(1) eps(2,2)*C(2)].*(N_abs_tot/Abs_tot(1,2));
N_Abs= [N_Abs_UV; N_Abs_Geel] ;
N_Abs_SP= sum(N_Abs(:,1));
N_Abs_MC= sum(N_Abs(:,2));
dC1= k(1,2)*C(2) - k(1,1)*C(1)+(QE(1,2)*N_Abs_MC - QE(1,1)*N_Abs_SP)/(Vtot*N_Av);
dC2= k(1,1)*C(1) - k(1,2)*C(2)+(QE(1,1)*N_Abs_SP - QE(1,2)*N_Abs_MC)/(Vtot*N_Av);
DC = [dC1; dC2]; end
Answers (1)
Star Strider
on 10 May 2017
0 votes
12 Comments
Bram Callewaert
on 10 May 2017
Star Strider
on 10 May 2017
I don’t know what ‘Flux’ is, although I assume it’s a vector. The documentation uses the interp1 function to interpolate a value for the time-dependent variable at a particular time. The time is an argument to the ODE function, so use that as the query value in the interp1 call to return the value of ‘Flux’ at ‘t’.
Note that for this, ‘Flux’ is defined inside the ODE function, not passed as a parameter, and the interp1 call is simply used to provide an appropriate value for it.
Bram Callewaert
on 10 May 2017
Edited: Bram Callewaert
on 10 May 2017
Star Strider
on 10 May 2017
I can’t understand your code, and without formatting (use the [{}Code] button), it’s difficult to read.
What information do you want to get out of ‘Flux’ at time ‘t’? I don’t see an assignment for ‘tijd’.
That seems to be the issue, so let’s concentrate on that just now.
Bram Callewaert
on 10 May 2017
Edited: Bram Callewaert
on 10 May 2017
Star Strider
on 10 May 2017
I’m more lost than I was before.
What is ‘Flux’ and what is the time vector that goes with it?
I was thinking of something like this:
Flux = [sin(2*pi*[0:18]/9); cos(2*pi*[0:18]/9)]';
tFlux = [0:18]';
t = 10.2;
Flux_at_t = interp1(tFlux, Flux, t)
Flux_at_t =
0.71119 0.64757
Bram Callewaert
on 10 May 2017
Star Strider
on 11 May 2017
I would put ‘Flux’ and ‘tjid’ inside your ‘ModelFOtoconversie_ODE’ function, then do:
f = interp1(tjid, Flux_metingen, t, 'linear', 'extrap');
I added ‘'linear','extrap'’ to keep the interpolation from crashing if it inadvertently exceeds the minimum or maximum values of ‘tjid’.
Bram Callewaert
on 11 May 2017
Star Strider
on 11 May 2017
The NaN values are the result of calculations producing 0/0, Inf/Inf or other values. One way to deal with that is to replace the 0 in ‘Flux’ with eps.
I cannot follow your code, but I notice that you have a variable named eps. Please rename your variable! Until you do, my fix for your NaN problem will not work.
Bram Callewaert
on 11 May 2017
Star Strider
on 11 May 2017
I can’t follow your code. It will be worthwhile for you to use the debug functions in the Editor to step through your code to see where the problems arise. That’s the only option I can think of at this point.
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!