Search code examples
matlabdiscrete-mathematicsodedifferential-equationsmathematical-expressions

How to pass time dependent value in Ode45 in matlab


I am using to solve a simple ode function . The code is given below.

function window_sine

% Some constant values to be used later in the code.............

epsilon0=8.85*10^-12;
d_mos=0.65*10^-9;
epsilon_mos_min=5*epsilon0;
epsilon_mos_max=20*epsilon0;
d_g=10*10^-9;
epsilon_g=30*epsilon0;
epsilon_mos=5*epsilon0;
vt=0;
e=1.6*10^-19;  
n=[];
i=1;
u=30; % cm^2/v*S
h=1.05*10^-34;  % j*s
cond_d=e^2/h;

%tIME Step...........

step=0.1;
t = 0:step:10;

%Some other constant values..simple..............
c_g=(epsilon_g/d_g);
c_mos=(epsilon_mos/d_mos);
c_t=1/((1/c_g)+(1/c_mos));
% This below equation involves time "t" which is defined above.
vs=(1-(c_t/c_g))*t;

%ode function calling............

p0=0.01;
[t,p] = ode45(@state,t,p0);

%plotting the result..................

figure
plot(t,p)
title('State Variable')

%Now the Ode45 function.................

function dpdt = state( t,p)

nc=6.8*10^12;
smooth=1;   
vg=t;
k=10^-1;

window1=1-((2*p)-1).^(2*smooth);

% How to incorporate the time dependent "n" value here.......... 

dpdt=k*(n-nc)*window1;   

end

end

Now the problem is "n" in the function. its time dependent and its value can be separately calculated using the following code.

while i<length(t)
n(i)=((c_g*(t(i)-vs(i)-vt))/e)*(10^-4);% this 10^-4 is for cm^2 unit
i=i+1;
end

In SHORT, THE value of "n" linearly increases with the "t". It should be noted that "t" is same everywhere, the time used in derivative.

The problem is how to incorporate this time dependent "n" value in my ode function where I have written "n". ? Where to write this part of code, in function block or in main code ? Thanks


Solution

  • Note that the first argument of your ode-function is time, exactly for time-dependent variables like yours and rheonomic constraints. So, simply feed the constants to your function and do the time-dependent calculation inside your ode-function.

    For instance:

    function dydt = myodefun(t,y,c)
    n=1+t*c; % calculate your time-dependent value n
    dydt = [y(2)*c; (1-y(1)^2)*y(2)-y(1)];
    

    Run for instance with:

    c=1; % a constant, or a structure of constants
    myodefunn=@(t,y)myodefun(t,y,c)
    [tout,yout]=ode45(myodefunn,[0 20],[2 0]);