# how to write code exchange the y value in the ode15s?

3 views (last 30 days)
MIN KYU LEE on 21 Feb 2020
Answered: darova on 21 Feb 2020
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

Steven Lord on 21 Feb 2020
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.