How to use global variable which is changing in every time step?

1 回表示 (過去 30 日間)
Urvi
Urvi 2012 年 10 月 8 日
I have a particular code which looks like this:
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
I have given initial values for b1,b2 and b3 which are my global variables. I want to understand how the solver uses this code to return a new value of b1,b2 and b3.
Can global be used to keep a value constant throughout the code?
I am solving 12 algebraic and 6 odes which look like the above code. b1,b2,b3 are synonymous with my algebraic variables and dy(1).... are my odes. When I solve that code I get an error saying "NaN". So I am unable to find out loophole in my code.
Thanks!

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 8 日
use break points to debug your code
  5 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 10 月 9 日
what is your actual problem?
Urvi
Urvi 2012 年 10 月 10 日
I have tried to explain it there. Thanks once again!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 10 月 8 日
One thing you need to keep in mind is that ode45() does not promise to evaluate in strictly increasing time steps.
  2 件のコメント
Urvi
Urvi 2012 年 10 月 8 日
Okay. But I am confused about the role of "global variable" . Will the solver return a new value for the same in every time step?
Urvi
Urvi 2012 年 10 月 8 日
The solver isn't returning a new value for my code. It is using the initial values for some variables and a changing value for some global variables. What could be the problem?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by