如何使用ode45使用全局变量?

时间:2015-10-05 12:45:40

标签: matlab math global-variables mathematical-optimization ode

我想在我的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

end

vgth也应该可以在ode函数中访问,这在这里不起作用。如果我用global vgth中的ode45替换某个常量,那么它可以正常工作。这意味着我的全局变量可访问性存在问题。

1 个答案:

答案 0 :(得分:1)

您可以使用以下

ApplicationWindow

UeKeypad

不需要[t,p] = ode45(@(t,y) state(t,y,vgth), t, p0); 或此类shenenigans。