I understand that you are trying to solve a non-linear ode with some stop intergration constraints defined in ‘stopsim’ function as an ‘event’ property.
To answer your first question, the ordinary differential equation solver functions provided with MATLAB employ a variety of variable-step methods. To get a fixed step solver, you can check the following link.:
Regarding the error you mentioned at the end:
I executed your code and realized that ‘Pb’ , ‘Pt’ and ‘Q’ were obtaining imaginary values at the end of execution. It meant that ‘gama1’ obtained negative values, which were later passed on to the “sqrt” function becoming a source of imaginary numbers. This should not have happened as you tried to control this phenomenon using the ‘stopsim’ function. In the 'stopsim' function, you defined the ‘Dir’ to be 1, which means you are tracking only zeros resulting due to an increasing value of event. However, the value of ‘Pt-Pb’ is a decreasing and does not get tracked. Also, the ‘Val’ variable is overwritten in the function. I suggest changing the ‘stopsim’ function to:
if round(x(1)-l)==0 || Pt<Pb
Val(1) = 0;
else
Val(1) = 1;
end
Ister(1) = 1;
Dir(1) = 0; %Track all zeros
The simulation should run now, but will terminate according to the conditions defined in ‘stopsim’, which happens to be at a very early stage of simulation. I would recommend checking the initial conditions and probably equations and units also, to prevent the ‘stopsim’ conditions resulting true very soon.