我想在我的ode45函数中使用一个变量值,这个值在另一个函数中计算出来。为此,我使用global在所有函数中访问该变量,但它不是这样工作的。我的主要功能代码如下:
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
endvgth在ode函数中也应该是可访问的,但这里不起作用。如果我用一些常量替换了global vgth中的ode45,那么它就能正常工作。这意味着我的全局变量可访问性存在问题。
发布于 2015-10-05 13:01:43
您可能可以使用以下方法
[t,p] = ode45(@(t,y) state(t,y,vgth), t, p0);使用
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; 不需要global或类似的恶作剧。
https://stackoverflow.com/questions/32948994
复制相似问题