ODE Solver Not Responding to Inputs
Show older comments
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 Comments
Fangjun Jiang
on 27 Aug 2011
Your code is not sufficient enough for us to see the problem.
John
on 27 Aug 2011
Accepted Answer
More Answers (2)
Lucas García
on 27 Aug 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
on 27 Aug 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 Comments
John
on 27 Aug 2011
Walter Roberson
on 27 Aug 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.
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!