You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
I have a Problem while using global variable.
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Sean de Wolski
on 8 Oct 2012
This is a good reason to not use globals. Can you show us a snippet of your code?
Have you seen:
21 Comments
Sean de Wolski
on 8 Oct 2012
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.
Sean de Wolski
on 8 Oct 2012
That's okay :). You just have to pass those in.
Urvi
on 9 Oct 2012
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!!
Walter Roberson
on 9 Oct 2012
Ummm, we'd have to see the actual code ?
Urvi
on 9 Oct 2012
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
Walter Roberson
on 9 Oct 2012
Edited: Walter Roberson
on 9 Oct 2012
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 ?
Walter Roberson
on 9 Oct 2012
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
on 9 Oct 2012
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
on 9 Oct 2012
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
on 9 Oct 2012
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
on 10 Oct 2012
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
on 10 Oct 2012
Edited: Urvi
on 10 Oct 2012
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.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)