how to write code exchange the y value in the ode15s?
1 次查看(过去 30 天)
显示 更早的评论
I want to make 0 when y is a condition (Y <0) when using the ODE15S solver.
The code is structured as follows, but as time t passes, y has a non-zero value.
I want to know how to write code that can be made 0 when y is less than 0 in t and y obtained using ode15s.
%f=dy/dt
%f = c * y
% f,c and y is 2-d matrix, c is a function of y and dependents on time t
[tsol,ysol]=ode15s(@(t,y) my_function(y,t,y_0,...),[0 10],y_0);
function f=my_function(y,t,y_0,...)
y_size=size(y)
for x=1:y_size(1)
if y(x)<0
y(x)=0;
end
end
...function code...
end
i'm currently get data
t y(1) y(2) ...
0 10 10
1 11 12
2 12 -1
3 -1 -2
4 -10 -3
i'm want to get data
t y(1) y(2) ...
0 10 10
1 11 12
2 12 0
3 0 0
4 0 0
0 个评论
回答(2 个)
Steven Lord
2020-2-21
Since you don't have a mass matrix for your problem, use odeset to set the NonNegative option to the indices of the components of the solution that should not be allowed to become negative. [Remember to pass the options odeset creates into your call to ode15s.] So if you don't want either y(1) or y(2) to become negative, specify 'NonNegative', [1 2] in your odeset call. See this documentation page for more information.
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!