이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)