Function ode45 event handler is missing event
显示 更早的评论
I'm writing a function to plot the position and velocity of a charged particle being affected by gravity and a charged plate beneath it. I have the acceleration formula, so I'm using ode45 to solve the differential equation. In addition the function must also catch when the particle hits the plate and restart the ode45 function from that time at a new initial position and velocity. When testing the case where the particle has 0 charge, the function catches when it bounces about 24 times; however, after the last bounce the ode45 event handler doesn't seem to catch when position is 0 and the particle falls through the plate. Any ideas on why the event function seems to stop working?
Here's the loop that solves the equation:
while isempty(t) || t(end) < 100
[t1, z1] = ode45(@odefun, [t0, 100], [z0, v0], options);
t = [t; t1]; %time
z = [z; z1]; %Position and velocity matric
z0 = 0; %new initial position
v0 = abs(z(end, 2).*cR); %new initial velocity
t0 = t(end); %new start time
nBounce = nBounce + 1; %Track the number of bounces
if v0 < 1e-4 %Break if initial veloctiy goes below a certain tolerance
break
end
end
回答(1 个)
Frederik Falk
2018-3-24
0 个投票
Did you try changing the tolerence of error of ode45? Try googling TolSet
类别
在 帮助中心 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!