0

I try to figure out how to input external variables(changing with time - hormone levels) into ode. I'm thinking about using events. I try to do optimisation (optimilisation of differential equations in Matlab with built-in events)

In order to learn how to use them I've tried to run code in Matlab ode solvers: changing state and specified time However, I do sth wrong. I guess that the problem is with my function myfun, but I don't know why.

I've tried to run following:

           function ydot=myfun(t,y,a)

           S1 = 2 *a* 2 * y(2)  -  2 * 3 * y(1)*y(1);
           S2 = 3 * y(1)*y(1) + a -  5 * y(2);
           ydot = [S1; S2];
           y_0=[0,0];
           %[t,y]=ode45(@myfun,[0:0.1:Tfinal],y_0);


and run following with nested myfun function:


%function events=events_in_Matlab(a)
       dt=0.01;
       T=0:dt:30;
       y_0=[0 0];
       Y=zeros(length(T),length(y_0));


       %t=0 to t=10, pass parameter a=0 to add to ODEs
       a=0;
       [~,Y(1:10/dt+1,:)]=ode45(@(t,y)myfun(t,y,a),T(1:10/dt+1),y_0);
       %t=10 to t=20, pass parameter a=10 to add to ODEs
       a=10;
       [~,Y(10/dt+1:20/dt+1,:)]=ode45(@(t,y)myfun(t,y,a),T(10/dt+1:20/dt+1),
       Y(10/dt+1,:));
       %t=20 to t=30, pass parameter a=20 to add to ODEs
       a=20;

      [~,Y(20/dt+1:end,:)]=ode45(@(t,y)myfun(t,y,a),T(20/dt+1:end),(20/dt+1,:));
Community
  • 1
  • 1
Malgorzata
  • 19
  • 3

1 Answers1

0

I think your code is right but your differential equation might not be ok.

First, your last equation is missing a "Y", it should be:

[~,Y(20/dt+1:end,:)]=ode45(@(t,y)myfun(t,y,a),T(20/dt+1:end),Y(20/dt+1,:));

Second, it seems that this is a stiff system of differential equations. I suggest you to use another solver, for example ode23s instead of ode45. In this way I managed to obtain a solution (with a lot of warnings due to near singular matrix).

I hope it helps you!

  • Thanks Alessandro, I've corrected a missing Y, but still get a series or errors and it doesn't run. I try to execute events_in_Matlab, as function myfun is nested into ode but a get a following error messages and it doesn't run: Undefined function or variable 'myfun'. Error in events_in_matlab>@(t,y)myfun(t,y,a) Error in odearguments (line 87) Do I try to run in improper way? Thanks in advance. – Malgorzata Mar 01 '17 at 15:40
  • I run it like this: In the main file `dt=0.01; T=0:dt:30; y_0=[0 0]; Y=zeros(length(T),length(y_0)); %t=10 to t=20, pass parameter a=10 to add to ODEs a=10; [T1,Y1]=ode23s(@(t,y)myfun(t,y,a),T(10/dt+1:20/dt+1),Y(10/dt+1,:)); plot(T1,Y1)` And in the function to solve: `function ydot=myfun(t,y,a) S1 = 2 *a* 2 * y(2) - 2 * 3 * y(1)*y(1); S2 = 3 * y(1)*y(1) + a - 5 * y(2); ydot = [S1; S2]; end` – Alessandro Trigilio Mar 09 '17 at 18:23
  • Big thank you for explanation, it will let me start with more complex example which I have. – Malgorzata Mar 20 '17 at 12:09
  • You´re welcome! Don't forget to mark the question as answered if your're ok with it! – Alessandro Trigilio Mar 22 '17 at 03:50