ode solvers & loops for various values

3 views (last 30 days)
harley
harley on 17 Sep 2014
Edited: Jan on 5 Oct 2014
hello i have shown part of a code below. The first 'Rh' value of 70 is used to find 'EMC' & 'Kx' and the 'Kx' is used in the ode45 solver. What i am trying to do is when my 'x' value gets to say 0.5 i want to stop the ode45 solver and use the next 'Rh' value of 60 to say x=0.25 and the same with the next 'Rh' value of 50 to say x=0.1. any help would be appreciated.
Rh = [70 60 50];
EMC = (18/W)*((K1*K2*(Rh/100))/(1+K1*K2*(Rh/100)) + (K2*(Rh/100)/(1+K2*(Rh/100))));
Kx = 1/ (a0*exp(c0/(T))*e + (b0*exp(c0/(T))*v^-p)*exp(-z/(FSP-EMC)));
[t,x]=ode45('Mass_bal_func',(t0:60:tf),IMC);

Accepted Answer

Michael Haderlein
Michael Haderlein on 17 Sep 2014
Edited: Jan on 5 Oct 2014
The ode45 function provides the opportunity to detect "events" which would be appropriate in your case. So, put the ode45 part into a loop and run until the respective event is detected. There is an example of how to use events in the Matlab help ( http://www.mathworks.com/help/matlab/math/ordinary-differential-equations.html#f1-669698 ) and I remember another example in Cleve Moler's textbook ( http://www.mathworks.com/moler/chapters.html ).
In the two equations (EMC, Kx), you'll need to replace most of the * / by .* and ./ respectively. This way, you get all three Kx values at once.
  3 Comments
Michael Haderlein
Michael Haderlein on 18 Sep 2014
I don't know, I haven't used Simulink so far. But it's not complicated to use events. Let me show it with a very small example:
odefun=@(t,x) -x; %just as example
evtfun=@(t,x) deal2(x-.25,1,0); %please see comment below
options=odeset('events',evtfun);
[t,x]=ode45(odefun,[0 10],10); %no events
[te,xe]=ode45(odefun,[0 10],10,options); %with events
figure, plot(t,x,te,xe)
deal2 is almost the same as the original deal function, however, there's a tiny difference:
function varargout = deal2(varargin)
if nargin==1,
varargout = varargin(ones(1,nargout));
else
varargout = varargin(1:nargout); %here, deal might either throw an error or pass all varargin
end
In the original Matlab deal function, the second option (after else) is a bit different. If you want details what happens here, tell me. Otherwise just accept it ;-)

Sign in to comment.

More Answers (1)

Mischa Kim
Mischa Kim on 17 Sep 2014
Harley, you are looking for something called events in MATLAB. Check out the section Events Location that explains how to use events in combination with ode solvers with the bouncing ball example.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!