Changing the parameters of the dynamical system in the middle of simulation, Part 2

2 views (last 30 days)
In my previous question, I asked "suppose I want to run
f = @(t,x) [-x(1)*x(2);x(1)*x(2)-x(2);x(2)]
[t,xa]=ode45(f,[0 6], [4 0.1 0]);
this runs the system from time 0 to 6. Suppose now I want to
*run the system from time 0 to 2
*from time 2, want to run
f = @(t,x) [-0.5*x(1)*x(2);0.7*x(1)*x(2)-x(2);x(2)]
How can one write a script?" Thank you for answers.
Now I want to consider a following senario:
"Suppose I want to
*initially run the system
*but once x(2) becomes more than 1, switch to
f = @(t,x) [-0.5*x(1)*x(2);0.7*x(1)*x(2)-x(2);x(2)]
How can one write such a script?

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 26 Apr 2020
For such conditions you might just get away with simply changing the output of the f-function. This is much easier to achieve if you write f as a proper matlab-function. Something like:
function dSIRdt = odeSIRvariableF(t,y,a,b,level)
dSIRdt = zeros(3,1);
dSIRdt(1) = -a*y(1)*y(2);
if y(2) < level
dSIRdt(2) = a*y(1)*y(2) - b(1)*y(2);
dSIRdt(3) = b(1)*y(2);
else
dSIRdt(2) = a*y(1)*y(2) - b(2)*y(2);
dSIRdt(3) = b(2)*y(2);
end
If this doesn't work nicely you should have a look at ballode - where you can see how to handl the case of a bouncing ball.
HTH
  12 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 27 Apr 2020
That is one way to calculate the derivatives. If you look at the help for diff you will also be adviced to look at the gradient function that you can use. You should make a habit of reading the help and documentation of the functions you use and pay attention to the SEE also part, another useful tool for finding functions is lookfor.
Since you have an ode-function that calculates derivatives as a function of t and y you should be able to figure out some way to use that to your advantage.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!