ODE45 code running indefinitely
조회 수: 4 (최근 30일)
이전 댓글 표시
My code continues to run indefinitely. I'm trying to use ode45 to output time and two different x values. I'm just not sure what is going on/why it takes so long. Any thoughts?
My main file:
time = (0:0.1:10);
initial = [0 0 0 0];
[T,X] = ode45('memsODE',time,initial);
My ode function file:
function xdot = memsODE(t,x)
m = [4*10^(-11) 4*10^(-9)];
k = [0.25 0.05];
b = 1*10^(-10);
P = 13790;
A = 4*10^(-8);
delta = 100*10^(-6);
c = [4266666666.66667 -640000 21.3333333333333];
f1 = c(1)*x(1).^3+c(2)*x(1).^2+c(3)*x(1);
if (x(1)<(delta/2))
f2 = 1;
else
f2 = -1;
end
xdot(1) = x(2);
xdot(2) = (-f1-2*k(1)*(x(1)-x(3)))/m(1);
xdot(3) = x(4);
xdot(4) = (-b*x(4)-2*k(2)*x(3)+2*k(1)*(x(1)-x(3))+P*A*f2)/m(2);
xdot = xdot';
댓글 수: 0
채택된 답변
Walter Roberson
2018년 4월 26일
The ode* routines cannot handle discontinuities. You need to create an event function to detect when x(1) switches between less than delta/2 or not, and assert termination when it does. Then you restart the computation from the last time of the previous run, using the last output of the previous run as the initial conditions for the new run.
I am working on an adjusted version.
댓글 수: 1
Walter Roberson
2018년 4월 26일
Adjusted code enclosed.
Your function oscillates fairly rapidly on switching f2 values. It gets between 2 and 8 iterations before crossing the border again. After 100 iterations it has only reached 0.000908514
You need to recheck your equations.
추가 답변 (0개)
참고 항목
카테고리
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!