I have a Problem while using global variable.
3 次查看(过去 30 天)
显示 更早的评论
Urvi
2012-10-8
I have a system of equations as follows:
dy1/dt = f(y1,a,b,c,d,y2...) . . . . dy6/dt = f(y1,a,b,c,d,y2...)
These are my ordinary differential equations. a,b,c are changing with time and I have 12 equations for 12 such variables. All of these equations are interdependent on each other.
Now, in the beginning of my main function I have declared a,b,c as global and I have specified their initial values.
Now, I have written the equations for a,b,c.... followed by my odes, i.e. algebraic equations followed by my ordinary differential equations. I am using ode45. When I run the code, I get results but wrong because of the following reasons :
The code is not calculating new values for some global variables in each time step. The first 9 algebraic equations have the same value throughout whereas the last 3 values are changing. I cannot figure out how to pass new values for these 9 variables. As a result of this, the results for my odes are coming out be extremely absurd.
Any help/suggestion will be appreciated. Thanks.
7 个评论
回答(1 个)
Sean de Wolski
2012-10-8
This is a good reason to not use globals. Can you show us a snippet of your code?
Have you seen:
21 个评论
Urvi
2012-10-8
The content in the link that you suggested has one problem though. It says, "extra parameters can be data, or can represent variables that do not change during the optimisation." My variables are changing with time.
Sean de Wolski
2012-10-8
Could you make them into funciton handles that given a time, they return a value?
f = @(t)2*t
We now have a function that gives a different output based on time.
Urvi
2012-10-8
Okay. I am new to matlab and I am really not good at programming I do not know how to use that but I will definitely try to read up about it and see if I can actually incorporate it into my code! Thanks a lot!
Urvi
2012-10-9
Pass those in as in? Here is a similar code and i'll explain what is happening with my actual code using the following example:
M FILE (I intend to run the code directly from the editor)
function H = newmain
global b1 b2 b3
b1=1.0;
b2=2.0;
b3=0.33;
options=odeset('InitialStep', 0.01, 'MaxStep', 0.01, 'RelTol', 10., 'AbsTol',10.);
[t2,y2]=ode45(@equation,[0:0.05:0.1],[1 2 0], options);
H = [t2 y2]
X=[b1 b2 b3]
end
function dy=equation(t,y)
global b1 b2 b3
dy=zeros(3,1);
b1=((b1+(1.-exp(-b3))))
b2=b1-y(1)
b3=b1+b2+y(2)
dy(1)=-b1*y(1);
dy(2)=b3*y(1)+b2*y(2);
dy(3)=sqrt(b1)+y(1)+y(3);
end
b1 b2 b3 are my variables changing with time and I have declared them as global. dy(1),dy(2) and dy(3) are my odes. This code is working fine. The code takes a new value for b1 b2 b3 with every time step. But, for my actual code, b1 b2 b3........b9 are not changing whereas b10 b11 b12 are changing with time. (I have 12 variables hence b1.....b12) Why is this happening? I have used the exact same logic as the code above. It doesn't seem to work.
Thanks!!
Urvi
2012-10-9
function F=Main
global a b c d % these are my constants
input_parameters() % file which contains the above constants
global b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12;
%these are the variables changing with time. they have their equations. Values need to be calculated at evry time step
%Initial values of the global variables declared above
b1=0;
b2=400;
b3=0;
b4=0;
b5=0;
b6=0;
b7=0;
b8=0;
b9=0;
b10=0.00056;
b11=0.0.0008;
b12=0.009;
% calling the function that has the equations
options=odeset('InitialStep', 1, 'MaxStep',0.1, 'RelTol', 0.0010, 'AbsTol',0.0010);
[t1,x1]=ode15s(@newfunction,[0:60:1500],[200 200 200 200 1 0.5], options);
Z = [b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12]
F = [t1 x1]
end
% this function has the system of equations that need to be solved
function f=newfunction(t,x)
f=zeros(6,1);
global a b c d ......z % these are my constants
global b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12;
input_parameters()
y1=x(1);
y2=x(2);
y3=x(3);
y4=x(4);
y5=x(5);
y6=x(6);
% ALGEBRAIC EQUATIONS:
%equation 1
if y5<=1
b1=0
else
b1=a*(y5-1)
end
% equation 2
b2=273-230.170+3855.70/(16.3872-log(100*y5))
%equation 3
if y1<=400
b3=0
else
b3=(0.8*c*d*(y1-400)/l)
end
%equation 4
b4=b6/[(b5/(1-b4))-0.2]
%equation 5
y5=y9/(p*q)
%equation 6
b6=b3/(r*q)
%equation 7
b7=(6*b10*b4)/0.003
%equation 8
b8=sqrt(2*y6*b4*j/k)
%equation 9
b9=p*q*b8
%equation 10
b10=m*n^2*y6
%equation 11
b11=m*(0^2-n^2)*y6
%equation 12
b12=m*0^2*[h-y6]
% ORDINARY DIFFERENTIAL EQUATIONS:
% ODE1
dy1dt=(1/p*q*b10)*[b3*y1-b4*y2];
f(1)=dy1dt;
% ODE2
dy2dt=(1/p*q*b11)*[b3*y1-b4*y2+b9*y3+b1*y4];
f(2)=dy2dt;
% ODE3
dy3dt=(1/p*q*b12)*[b3*y2-b4*y1+b9*y3+b1*y3];
f(3)=dy3dt;
% ODE4
dy4dt=(1/p*q*b12)*[b1*y1-b1*y4];
f(4)=dy4dt;
% ODE5
dy5dt=a*b/y5 + dy4dt
f(5)=dy5dt;
% ODE6
dy6dt=s-b3/p*b4+q*y6
f(6)=dy6dt;
end
Urvi
2012-10-9
The smaller code which i posted earlier is working fine but this code isn't. B10,B11 AND B12 are changing with time but B1 TO B9 aren't. Because of this I am getting wrong results.
Walter Roberson
2012-10-9
编辑:Walter Roberson
2012-10-9
Your code changes y5 instead of b5 ?
Your changes appear to be order dependent; for example you change b4 and then use the changed b4 later in b8. Was that the intention, or were you intending to use the pre-change b4 ?
Urvi
2012-10-9
y5 is my variable in the ode...it should change...but it isn't. b5 isn't changing at all.
Walter Roberson
2012-10-9
Are you sure you should have the call to input_parameters() in newfunction? You already set the variables in the main routine and don't want to reset them.
Walter Roberson
2012-10-9
But you global'd them. global all of them in the main function, and call the initialization routine in the main function, and then when you global all of them in the called function they will already have their values and you will not need to call input_parameters()
Urvi
2012-10-9
But the first "global" values are constant and are stored in a file input_parameters. The other "global" refers to the initial values of my algebraic equations. Say b1=0 is the initial value and the equation for b1=a*(y5-1).
Walter Roberson
2012-10-9
You should only be initializing any one global variable once. Global variables can be initialized in one routine and used in another routine.
Walter Roberson
2012-10-10
You initialize some of them once in your main routine when you call input_parameters() there, and then you re-initialize some of them in your other routine when you call input_parameters() again there
Urvi
2012-10-10
编辑:Urvi
2012-10-10
But they are constants. Their value isn't changing at all. What I need to change with every time step is : global b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12; input_parameters does not contain b1 b2 b3 b4......it contains a..z(constants). I am initializing b1 b2 b3...in the present code itself.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)