ODE Solver Not Responding to Inputs
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have an issue I need to sort out with ode45 in MATLAB. This is a 'test case' for a very large project I am working on which is having this issue. It is easier to explain on a simpler problem.
I have created a program which models two vehicles which have a tow-rope between them. I want to impart a specific velocity on the front vehicle to 'forcefully drive' it. Below is the DE's in the rates of change file:
xd = zeros(4,1);
xd(1)=x(2);
xd(2)=-(k/M1).*(x(1)-x(3))-(120/M1).*sign(x(2)).*abs(x(2))^2+f1/M1;
xd(3)=x(4);
xd(4)=(k/M2).*(x(1)-x(3))-(800/M2).*sign(x(4)).*abs(x(4))^2-f2/M2;
Then to 'drive' the front vehicle I add after this:
if t>0 && t<20
xd(1) = 1*t;
xd(2) = 1;
else
xd(1) = 0;
xd(2) = 0;
end
So I reason that the front car will accelerate at a constant rate (2m/s) then come to an instant stop at 20 seconds.
On the plot produced the velocity of the front car does not stop; it just holds a constant value. However, the car behind acts as if the front car has stopped!
Also, when I un-comment the 'driving' lines I do not get zero:
else
xd(1) = 0
xd(2) = 0
end
Basically the above 2 lines are not being implemented. Your advice is much appreciated.
2 comentarios
Respuesta aceptada
Jan
el 27 de Ag. de 2011
I still do not get the actual problem. Is it correct that it is described by:
"On the plot produced the velocity of the front car does not stop; it just holds a constant value. However, the car behind acts as if the front car has stopped!"
So it might be possible, that the production of the plot is wrong? Or the interpretation of how the car acts or the expectation how it should act? Or do you think, that the velocity and position calculated by the intergator do not match?
Another problem: The ODE45 (from the text, or ODE23 as in the code) integrator needs a smooth function, because it evaluates the ODE at several intermediate times for each step to estimate the average slope. If some time steps are before and some are behind your time switching points. Therefore all functions, which destroy the continuity, are a bad idea in the ODF function: IF, SIGN, ABS, MIN, MAX, RAND, ...
And finally I do not understand, why you create xd(1) and xd(2) and overwrite it some lines later.
Más respuestas (2)
Lucas García
el 27 de Ag. de 2011
I suggest that you debug your code from t>20 on by setting a conditional breakpoint at
if t>0 && t<20
To do this, after setting a breakpoint, right click it and go to "Set/Modify condition". Then place the condition
t > 20
Now you will be able to debug whenever t>20. Let see what happens.
Walter Roberson
el 27 de Ag. de 2011
According to the documentation,
Function f = odefun(t,y), for a scalar t and a column vector y, must return a column vector f corresponding to f(t,y)
Your code does not appear to be doing that: your code appears to be returning a fixed size output vector for which the third and 4th outputs are not set according to the time in a manner consistent with the first 2.
The behavior you are seeing in whatever routine you are in would appear to be consistent with the possibility that t is a vector instead of a scalar... though in such a case I would expect an error about the use of && with a vector.
2 comentarios
Walter Roberson
el 27 de Ag. de 2011
Darn, the documentation confuses me; it looks like I was misinterpreting it.
Sorry, this is not something I have time to work on at present.
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!