I have a Problem while using global variable.

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

where you are declared those variables(a,b,c) global variable..???
In the different m file...matlab takes those values and uses them in my code.
instead of m files ..is der another way to delcare..??
I don't know if there is a way
sory is der possible to share your model..??
I have posted it in one of the comments below. Thanks!
if u dont mind can you pls ping to me mail.here i dont have acceses to take

Sign in to comment.

Answers (1)

This is a good reason to not use globals. Can you show us a snippet of your code?
Have you seen:

21 Comments

Urvi
Urvi on 8 Oct 2012
Edited: Urvi on 8 Oct 2012
What else can I use? Any suggestions? Actually, I am not allowed to post my code. I shall try to formulate a similar code and post it. Thanks!
thanks for the link btw!!
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.
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.
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
Urvi on 8 Oct 2012
Edited: Urvi on 8 Oct 2012
These algebraic equations do not have time 't' terms in them. They are either dependent on some constants or the variables which are evaluated from odes.
That's okay :). You just have to pass those in.
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!!
Ummm, we'd have to see the actual code ?
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
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.
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 ?
y5 is my variable in the ode...it should change...but it isn't. b5 isn't changing at all.
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.
yes because the values of a to z are stored in that file...they are constants.
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()
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).
You should only be initializing any one global variable once. Global variables can be initialized in one routine and used in another routine.
But, I have initialized it once..haven't I?
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
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 8 Oct 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!