How to apply If else condition properly for ode45 function file?

조회 수: 4 (최근 30일)
Sunit Gupta
Sunit Gupta 2014년 4월 29일
댓글: Sunit Gupta 2014년 4월 29일
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
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
Sunit Gupta 2014년 4월 29일
Hi Mischa, Thanx for ur answer, but this is not what i m looking for. As the system of equations changed from 1 to 2, ideally the values of x(1) and x(2) should not change. But they are changing. To test it, just put
function dx = test(t,x,omega)
x(2)
if (x(2) + omega)>0
dx = [x(2,1); ...
0.01*x(2,1)-x(1,1)];
else
keyboard
dx = [0; 0];
end
end
Now as the system will change from 1 to 2 you can see that the values of variables will change.
Mischa Kim
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
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
Parham
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
Sunit Gupta 2014년 4월 29일
Thanx Parham, I think the same. It is happening because of numerical integration only. But can i handle this by fixing the time step, say 0.001?
Regards Sunit

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

카테고리

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