Filter löschen
Filter löschen

ode solvers & loops for various values

1 Ansicht (letzte 30 Tage)
harley
harley am 17 Sep. 2014
Bearbeitet: Jan am 5 Okt. 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);

Akzeptierte Antwort

Michael Haderlein
Michael Haderlein am 17 Sep. 2014
Bearbeitet: Jan am 5 Okt. 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 Kommentare
Michael Haderlein
Michael Haderlein am 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 ;-)
harley
harley am 18 Sep. 2014
thanks for that

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mischa Kim
Mischa Kim am 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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by