How to apply If else condition properly for ode45 function file?
古いコメントを表示
Hi, I am trying to solve system of first order ode via ode45. Please find the attached file for it. My system of equations are
x(2,1) %to show what is the value of my variable.
if x(2,1)+omega>0
dx(1,1)=x(2,1); %These are the first set of equations of an unstable oscillator (negative %damping).
dx(2,1)=0.01*x(2,1)-x(1,1);
else
keyboard
dx(1,1)=0;
dx(2,1)=0;
end
Now when i m using this file in ODE45 for omega=2, then as the system of equations is changed from 1 to 2, there should be no change in the solution of x(1,1)and x(2,1), but during the running they are changing. Please help me out for this.
Regards Sunit
回答 (2 件)
Mischa Kim
2014 年 4 月 29 日
編集済み: Mischa Kim
2014 年 4 月 29 日
Sunit, use something like
function myODE(omega)
IC = [1 -1];
options = odeset('AbsTol',1e-10,'RelTol',1e-10);
[t,X] = ode45(@test,[0 2],IC,options,omega);
figure
plot(t,X(:,2),'r',t,X(:,1),'b')
xlabel('t')
ylabel('x(1), x(2)')
grid
end
function dx = test(t,x,omega)
if (x(2) + omega)>0
dx = [x(2,1); ...
0.01*x(2,1)-x(1,1)];
else
dx = [0; 0];
end
end
Is this the behavior you are looking for?
2 件のコメント
Sunit Gupta
2014 年 4 月 29 日
Mischa Kim
2014 年 4 月 29 日
編集済み: Mischa Kim
2014 年 4 月 29 日
I am not sure I understand. Let's say you start with initial conditions such that you end up with system 1 (the non-trivial ODE). When x(2) + omega goes negative, the code switches to system 2. From that time on the state vector remains constant. With the above IC, use e.g.
myODE(1.2)
and remove the semi-colons from the two dx = ... commands. If you change the solver settings and replace the ode call by
options = odeset('AbsTol',1e-10,'RelTol',1e-10);
[t,X] = ode45(@test,[0 10],IC,options,omega);
you'll see that the state vector remains constant when x(2) reaches -1.2.

On the other hand if you execute
myODE(1)
the ode solver starts with system 2 (as it should) and remains in that state (as it should).
Parham
2014 年 4 月 29 日
I tested this but the thing you're saying did not happen:
function []=pap()
[AA,BB] = ode45(@testttt,[0,5],[2,.1])
end
function dx= testttt(t,x)
x(2,1)
omega = 2;
if x(2,1)+omega>0
dx(1,1)=x(2,1);
dx(2,1)=0.01*x(2,1)-x(1,1);
else
dx(1,1)=0;
dx(2,1)=0;
end
end
4 件のコメント
Sunit Gupta
2014 年 4 月 29 日
Mischa Kim
2014 年 4 月 29 日
See plot screen shot above for myODE(1.2) and IC = [1 -1].
Parham
2014 年 4 月 29 日
Ok. The problem is when MATLAB integrates, it finds the jacobian and use it while integrating. That is the reason why the time increments of the beginning of the integration is very small (0.01) and increase to larger values ( 10 20 ...). So, when the integration time increases, your value (x(1,2)) might go below the omega value that you defined and not exactly stop at that value. This kind of switching I guess can be handled using the event handling of the ode solver. Check out the event handling of ode solvers.
Sunit Gupta
2014 年 4 月 29 日
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!