I have a Problem while using global variable.

조회 수: 3 (최근 30일)
Urvi
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
Urvi
Urvi 2012년 10월 9일
I have posted it in one of the comments below. Thanks!
swaraj
swaraj 2012년 10월 9일
if u dont mind can you pls ping to me mail.here i dont have acceses to take

댓글을 달려면 로그인하십시오.

답변 (1개)

Sean de Wolski
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
Walter Roberson
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
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 CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by