I have a Problem while using global variable.
Afficher commentaires plus anciens
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 commentaires
swaraj
le 9 Oct 2012
where you are declared those variables(a,b,c) global variable..???
Urvi
le 9 Oct 2012
swaraj
le 9 Oct 2012
instead of m files ..is der another way to delcare..??
Urvi
le 9 Oct 2012
swaraj
le 9 Oct 2012
sory is der possible to share your model..??
Urvi
le 9 Oct 2012
swaraj
le 9 Oct 2012
if u dont mind can you pls ping to me mail.here i dont have acceses to take
Réponses (1)
Sean de Wolski
le 8 Oct 2012
0 votes
This is a good reason to not use globals. Can you show us a snippet of your code?
Have you seen:
21 commentaires
Urvi
le 8 Oct 2012
Urvi
le 8 Oct 2012
Sean de Wolski
le 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.
Urvi
le 8 Oct 2012
Sean de Wolski
le 8 Oct 2012
That's okay :). You just have to pass those in.
Urvi
le 9 Oct 2012
Walter Roberson
le 9 Oct 2012
Ummm, we'd have to see the actual code ?
Urvi
le 9 Oct 2012
Urvi
le 9 Oct 2012
Walter Roberson
le 9 Oct 2012
Modifié(e) : Walter Roberson
le 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 ?
Urvi
le 9 Oct 2012
Walter Roberson
le 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.
Urvi
le 9 Oct 2012
Walter Roberson
le 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
le 9 Oct 2012
Walter Roberson
le 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.
Urvi
le 10 Oct 2012
Walter Roberson
le 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
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!