I want to use a variable value in my ode45
function which in calculated in another function. For that I used global
to access that variable in all functions but its not working in that way. My main function code is as follows:
function window_sine
%%%%%%% Here is the global variable
global vgth;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
epsilon0=8.85*10^-12;
d_mos=0.65*10^-9;
epsilon_mos=5*epsilon0;
d_g=20*10^-9;
epsilon_g=10*epsilon0;
vt=0;
e=1.6*10^-19;
n=[];
i=1;
t2=[];
u=30; % cm^2/v*S
h=1.05*10^-34; % j*s
cond_d=e^2/h;
ex=1.5;
%Capacitor Calaculation
c_g=(epsilon_g/d_g);
c_mos=(epsilon_mos/d_mos);
c_t=1/((1/c_g)+(1/c_mos));
step=0.1;
t = 0:step:10;
%Input Voltage
vg =t;
% Surface Voltage
vs=(1-(c_t/c_g))*vg;
vg2=vg(1:(length(vg)-1));
%Condition
while i<length(vg)
n(i)=((c_g*(vg(i)-vs(i)-vt))/e)*(10^-4);% this 10^-4 is for cm^2 unit
if n(i) >= 6.70*10^12 & n(i) <= 6.82*10^12
%%%%%%% Here is the simple calculation of vgth %%%%%%%%%%%%%%
vgth=vg(i);
disp(vgth);
end
i=i+1;
end
figure
plot(vg2,n)
title('Carrier Concentration vs Gate input')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ode function calling............
p0=0.01;
[t,p] = ode45(@state,t,p0);
%plotting state variable..................
figure
plot(t,p)
title('Percolation vs input voltage')
p2=p(1:(length(p)-1));
figure
plot(n,p2)
title('Percolation vs carrier concentration')
%%%%%%%%%%%%%%%%%%%%%% Here is the ode45 function calling %%%%%%%%%%%%%
function dpdt = state( t,p)
smooth=1;
vg=t;
k=10^-1;
window1=1-((2*p)-1).^(2*smooth);
%%% See, I just used the vgth value in below.which is global%%%%%%
%%%%The vgth vwas declared in main function and calculated in main function%
dpdt=k*(vg+vgth)*window1;
end
end
vgth
should be accessible in ode function as well which is not working here. If i replace global vgth
in ode45
with some constant, then it works fine. This means there is a problem with my global variable accessibility.
You can probably use the following
[t,p] = ode45(@(t,y) state(t,y,vgth), t, p0);
with
function dpdt = state( t,p, vgth)
smooth=1;
vg=t;
k=10^-1;
window1=1-((2*p)-1).^(2*smooth);
dpdt=k*(vg+vgth)*window1;
No need for global
or such shenenigans.