MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Thread Subject: ode with definite integral

 Subject: ode with definite integral From: Grzegorz Knor Date: 30 Jun, 2011 06:56:10 Message: 1 of 6 Hello, I wonder how to solve such a task: Suppose we want to solve a differential equation in the form: dy/dt = f(t) = exp(-te) where: te = \int from 0 to t (2^(y/10)) dt Is it possible to do it with Matlab ode solvers? Best regards Grzegorz
 Subject: ode with definite integral From: Nasser M. Abbasi Date: 30 Jun, 2011 07:17:06 Message: 2 of 6 On 6/29/2011 11:56 PM, Grzegorz Knor wrote: > Hello, > I wonder how to solve such a task: > Suppose we want to solve a differential equation in the form: > dy/dt = f(t) = exp(-te) > where: > te = \int from 0 to t (2^(y/10)) dt > Is it possible to do it with Matlab ode solvers? > > Best regards > Grzegorz You integrand is 2^(y/10) but you are integrating w.r.t. ? So teh integrand is constant? So the integral is just       2^(y/10) * Integral[ 1 , {0,t} ] which is       2^(y/10) * t ? Can you clarify first, then will give you code. --Nasser
 Subject: ode with definite integral From: Grzegorz Knor Date: 30 Jun, 2011 07:32:10 Message: 3 of 6 "Nasser M. Abbasi" wrote in message ... > On 6/29/2011 11:56 PM, Grzegorz Knor wrote: > > Hello, > > I wonder how to solve such a task: > > Suppose we want to solve a differential equation in the form: > > dy/dt = f(t) = exp(-te) > > where: > > te = \int from 0 to t (2^(y/10)) dt > > Is it possible to do it with Matlab ode solvers? > > > > Best regards > > Grzegorz > > You integrand is 2^(y/10) but you are integrating w.r.t. ? > > So teh integrand is constant? So the integral is just > > 2^(y/10) * Integral[ 1 , {0,t} ] > > which is > > 2^(y/10) * t > > ? > > Can you clarify first, then will give you code. > > --Nasser y depends on the time, so y = y(t). Here is a tex code for this equation: \frac{dy(t)}{dt} = e^{- \int_0^t 2^{y(z))/10}dz} or link: http://latex.codecogs.com/gif.latex?\huge&space;\frac{dy(t)}{dt}&space;=&space;e^{-&space;\int_0^t&space;2^{y(z))/10}dz} Grzegorz
 Subject: ode with definite integral From: Roger Stafford Date: 30 Jun, 2011 07:44:10 Message: 4 of 6 "Grzegorz Knor" wrote in message ... > Hello, > I wonder how to solve such a task: > Suppose we want to solve a differential equation in the form: > dy/dt = f(t) = exp(-te) > where: > te = \int from 0 to t (2^(y/10)) dt > Is it possible to do it with Matlab ode solvers? > > Best regards > Grzegorz - - - - - - - - -   Yes, that can easily be done with an ode solver. Define  te(t) = int('2^(y(s)/10)','s',0,t) Then we have  dte(t)/dt = 2^(y(t)/10) and  dy(t)/dt = exp(-te(t)) so this is in the proper form to be solved with an ode solver. You must of course arrange that te(0) = 0. The initial value, y(0), is up to you. Roger Stafford
 Subject: ode with definite integral From: Grzegorz Knor Date: 30 Jun, 2011 08:13:11 Message: 5 of 6 "Roger Stafford" wrote in message ... > "Grzegorz Knor" wrote in message ... > > Hello, > > I wonder how to solve such a task: > > Suppose we want to solve a differential equation in the form: > > dy/dt = f(t) = exp(-te) > > where: > > te = \int from 0 to t (2^(y/10)) dt > > Is it possible to do it with Matlab ode solvers? > > > > Best regards > > Grzegorz > - - - - - - - - - > Yes, that can easily be done with an ode solver. Define > > te(t) = int('2^(y(s)/10)','s',0,t) > > Then we have > > dte(t)/dt = 2^(y(t)/10) > > and > > dy(t)/dt = exp(-te(t)) > > so this is in the proper form to be solved with an ode solver. You must of course arrange that te(0) = 0. The initial value, y(0), is up to you. > > Roger Stafford Thank you. Grzegorz
 Subject: ode with definite integral From: Nasser M. Abbasi Date: 30 Jun, 2011 08:41:00 Message: 6 of 6 On 6/30/2011 12:44 AM, Roger Stafford wrote: > "Grzegorz Knor" wrote in message... >> Hello, >> I wonder how to solve such a task: >> Suppose we want to solve a differential equation in the form: >> dy/dt = f(t) = exp(-te) >> where: >> te = \int from 0 to t (2^(y/10)) dt >> Is it possible to do it with Matlab ode solvers? >> >> Best regards >> Grzegorz > - - - - - - - - - > Yes, that can easily be done with an ode solver. Define > > te(t) = int('2^(y(s)/10)','s',0,t) > > Then we have > > dte(t)/dt = 2^(y(t)/10) > > and > > dy(t)/dt = exp(-te(t)) > > so this is in the proper form to be solved with an ode solver. > You must of course arrange that te(0) = 0. The initial value, y(0), is up to you. > > Roger Stafford Nice Roger, that is a nice way to do it. Below I show your method, implemented in ode45, using initial value for te(0)=0, and then show another method I just hacked using the original formula as is by using quad directly, and interp1 to do the integration at each step. It is a hack, but it gives the same answer. Had to protect against 2 special cases: when ode45 uses the same time step twice, else intepr1 will not be happy, and for initial case at t=0. Here we go: Roger method: (the right way to do it) ------------------ functionfoo1 figure; y0=1; integralAtZero=0; maxTime = 10;      [t,y]=ode45(@RHS,[0 maxTime],[y0 integralAtZero]); plot(t,y(:,1)); title('solution y(t)');      function ydot = RHS(t,y)          ydot = [exp(-y(2)) ; 2^(y(1)/10) ];      end       end --------------------------- my method, just for fun :) ------------------------------ function foo y0=1; maxTime = 10; globalY = zeros(1000,1); % change as needed globalt = zeros(1000,1); % to prevent dynamic allocation globalydot = zeros(1000,1); %just a hack indx=0;      [t,y]=ode45(@RHS,[0 maxTime],y0); plot(t,y);      function ydot = RHS(currentTime,y)          if currentTime(1)>0 && currentTime(1) == globalt(indx)                 ydot= globalydot(indx);          else            indx=indx+1;            globalY(indx)=y(1);            globalt(indx)=currentTime(1);            ydot = exp(-quad(@f,0,currentTime));            globalydot(indx)=ydot;          end      end      function v = f(t) % integrate 2^(y(t)/10)          if all(t==0)             v=t;          else            yi = interp1(globalt(1:indx), globalY(1:indx),t) ;            v = 2.^(yi/10);          end      end end ------------------------------------- --Nasser