Help with event function in ode15s
1 次查看(过去 30 天)
显示 更早的评论
My code integrates with ode15s a function called CellODE with the parameters mentioned. I would like my event function to stop the CellODE function to stop running when any value of Y_out is larger than one. Without Event checker, My code would produce Y_out which would be a matrix of how ever many rows ode15s decided it needed, and space_steps amount of collumns. I want event checker to stop this code running the moment Y_out produces a value higher than 1. (this might happen even after the 3rd or 4th time step..). The below code is the main script Im using.
close all; clc;
P=1.2;
k=1.2;
mu_n=1;
mu_w=1;
T=5000;
space_steps=1000;
x=linspace(0,1,space_steps).';
dx=1/space_steps;
n0=0.6*(1-(x).^2);
s_0=1;
T_span=[0;T];
options = odeset('Events',@MyEvent);
[T_out,Y_out,te,ye,ie] = ode15s(@(t,y) CellODE(t,y,k,P,mu_n,mu_w,space_steps),T_span,[n0;s_0],options);
The below code is the event checker function, I'm sure that something is incorrect in the second line.
function [val, isterminal, dir] = MyEvent(~, Y)
val = (Y>=1);
isterminal = 1;
dir = 0;
end
Thanks for any help!
0 个评论
回答(2 个)
Jacob Wood
2020-2-19
The event you are looking to implement sounds quite similar to the example event Matlab uses in the documentation. We can change the example a touch to detect the point where height = 1 instead of 0 with a small edit:
function [value,isterminal,direction] = bounceEvents(t,y)
% value = y(1); % Detect height = 0
value = y(1) - 1; % Detect height = 1
isterminal = 1; % Stop the integration
direction = -1; % Negative direction only
Steven Lord
2020-2-19
function [val, isterminal, dir] = MyEvent(~, Y)
val = (Y>=1);
isterminal = 1;
dir = 0;
end
Your event function's value should not be logical true or false. It should be a value that can cross zero, so that crossing can be detected.
function [val, isterminal, dir] = MyEvent(~, Y)
val = Y-1;
isterminal = 1;
dir = 0;
end
2 个评论
Steven Lord
2020-2-19
Can you show the full text of the error message (all the text displayed in red) and the exact contents of your event function?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!