ode solvers & loops for various values

1 次查看(过去 30 天)
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);

采纳的回答

Michael Haderlein
Michael Haderlein 2014-9-17
编辑:Jan 2014-10-5
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 个评论
Michael Haderlein
Michael Haderlein 2014-9-18
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 ;-)

请先登录,再进行评论。

更多回答(1 个)

Mischa Kim
Mischa Kim 2014-9-17
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by