Reputation: 145
As it is specified in the title I need to force a function to be always positive (if it runs into negative it has to equal 0). This function is inside an ODE:
This is the script from where I call the ODE:
clear
t=[0,276];
Tie=0.3;
mumax=2;
Qmin=1;
X0=[4,2];
[t,X]=ode45(@(t,X) odeset(mumax,Qmin,Tie,X),t,X0);
This is the function that I want to keep always positive
function [ func2 ] = func2 (mumax,Qmin,Q)
func2=mumax*(1-Qmin/Q);
end
and this is the ODE
function [ dXdt ] = odeset(mumax, Qmin, Tie,X)
dXdt=zeros(2,1);
dXdt(1)=func2(mumax,Qmin,X(1))-X(2)*Tie;
dXdt(2)=func2(mumax,Qmin,X(1))-X(2)*Tie;
end
Upvotes: 0
Views: 302
Reputation: 1222
Change func2
's output to
function [ out ] = func2 (mumax,Qmin,Q)
out=max(mumax*(1-Qmin/Q),0);
end
This will force the output to be positive or Zero.
Upvotes: 1