Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Writing a system of differential equations correctly

Subject: Writing a system of differential equations correctly

From: Douglas Nicolin

Date: 15 Apr, 2013 17:52:06

Message: 1 of 7

Hi everyone!
I'm working on a model composed by a system of differential equations of the following form:

dXdt = fun(t,X,R)
dRdt = fun(t,X,R)

I wrote the model in this form because my first differential equation is a partial differential equation of the dependent variable X as a function of r and t. I intend to use finite differences to discretize the spatial derivatives. By doing this, the first equation becomes a system of differential-algebraic equations to be integrated in time. Besides the system of dae's, I have to introduce a last differential equation in the function. The dependent variable of the last differential equation appears in the system of differential-algebraic equation cited before. I would like to know the proper way to write the whole function without making any mistakes. I have no problem at all to write the system of dae's but I'm not sure how to include the last differential equation once the main variable to write the function is already being used to define only the system of dae's. I don't know if
I'm making myself clear enough with all these explanations but I'd appreciate any help.

Thanks.

Subject: Writing a system of differential equations correctly

From: Torsten

Date: 16 Apr, 2013 06:24:09

Message: 2 of 7

"Douglas Nicolin" <douglas.nicolin@gmail.com> wrote in message <kkheo6$5hh$1@newscl01ah.mathworks.com>...
> Hi everyone!
> I'm working on a model composed by a system of differential equations of the following form:
>
> dXdt = fun(t,X,R)
> dRdt = fun(t,X,R)
>
> I wrote the model in this form because my first differential equation is a partial differential equation of the dependent variable X as a function of r and t. I intend to use finite differences to discretize the spatial derivatives. By doing this, the first equation becomes a system of differential-algebraic equations to be integrated in time. Besides the system of dae's, I have to introduce a last differential equation in the function. The dependent variable of the last differential equation appears in the system of differential-algebraic equation cited before. I would like to know the proper way to write the whole function without making any mistakes. I have no problem at all to write the system of dae's but I'm not sure how to include the last differential equation once the main variable to write the function is already being used to define only the system of dae's. I don't know if
> I'm making myself clear enough with all these explanations but I'd appreciate any help.
>
> Thanks.

If the differential equation is defined for each grid point where the partial differential equation is defined (thus if you have 2*n equations to solve where n is the number of grid points), order your solution variables gridpoint-wise (discretized PDE-equation 1st grid point, ODE-equation 1st grid point, discretized PDE-equation 2nd grid point, ODE-equation second grid point, ... , discretized PDE-equation n'th grid point, ODE-equation n'th grid point).
If there is really only one additional differential equation, just add it after the n discretized PDE-equations as equation (n+1).

Best wishes
Torsten.

Subject: Writing a system of differential equations correctly

From: Douglas Nicolin

Date: 16 Apr, 2013 11:21:07

Message: 3 of 7

"Torsten" wrote in message <kkiqq9$89m$1@newscl01ah.mathworks.com>...
> "Douglas Nicolin" <douglas.nicolin@gmail.com> wrote in message <kkheo6$5hh$1@newscl01ah.mathworks.com>...
> > Hi everyone!
> > I'm working on a model composed by a system of differential equations of the following form:
> >
> > dXdt = fun(t,X,R)
> > dRdt = fun(t,X,R)
> >
> > I wrote the model in this form because my first differential equation is a partial differential equation of the dependent variable X as a function of r and t. I intend to use finite differences to discretize the spatial derivatives. By doing this, the first equation becomes a system of differential-algebraic equations to be integrated in time. Besides the system of dae's, I have to introduce a last differential equation in the function. The dependent variable of the last differential equation appears in the system of differential-algebraic equation cited before. I would like to know the proper way to write the whole function without making any mistakes. I have no problem at all to write the system of dae's but I'm not sure how to include the last differential equation once the main variable to write the function is already being used to define only the system of dae's. I don't know
if
> > I'm making myself clear enough with all these explanations but I'd appreciate any help.
> >
> > Thanks.
>
> If the differential equation is defined for each grid point where the partial differential equation is defined (thus if you have 2*n equations to solve where n is the number of grid points), order your solution variables gridpoint-wise (discretized PDE-equation 1st grid point, ODE-equation 1st grid point, discretized PDE-equation 2nd grid point, ODE-equation second grid point, ... , discretized PDE-equation n'th grid point, ODE-equation n'th grid point).
> If there is really only one additional differential equation, just add it after the n discretized PDE-equations as equation (n+1).
>
> Best wishes
> Torsten.

Thanks Torsten! I'll try the way you said. This should work well.
Best wishes
Douglas.

Subject: Writing a system of differential equations correctly

From: Douglas Nicolin

Date: 16 Apr, 2013 20:32:10

Message: 4 of 7

"Douglas Nicolin" <douglas.nicolin@gmail.com> wrote in message <kkjc73$nc9$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kkiqq9$89m$1@newscl01ah.mathworks.com>...
> > "Douglas Nicolin" <douglas.nicolin@gmail.com> wrote in message <kkheo6$5hh$1@newscl01ah.mathworks.com>...
> > > Hi everyone!
> > > I'm working on a model composed by a system of differential equations of the following form:
> > >
> > > dXdt = fun(t,X,R)
> > > dRdt = fun(t,X,R)
> > >
> > > I wrote the model in this form because my first differential equation is a partial differential equation of the dependent variable X as a function of r and t. I intend to use finite differences to discretize the spatial derivatives. By doing this, the first equation becomes a system of differential-algebraic equations to be integrated in time. Besides the system of dae's, I have to introduce a last differential equation in the function. The dependent variable of the last differential equation appears in the system of differential-algebraic equation cited before. I would like to know the proper way to write the whole function without making any mistakes. I have no problem at all to write the system of dae's but I'm not sure how to include the last differential equation once the main variable to write the function is already being used to define only the system of dae's. I don't know

> if
> > > I'm making myself clear enough with all these explanations but I'd appreciate any help.
> > >
> > > Thanks.
> >
> > If the differential equation is defined for each grid point where the partial differential equation is defined (thus if you have 2*n equations to solve where n is the number of grid points), order your solution variables gridpoint-wise (discretized PDE-equation 1st grid point, ODE-equation 1st grid point, discretized PDE-equation 2nd grid point, ODE-equation second grid point, ... , discretized PDE-equation n'th grid point, ODE-equation n'th grid point).
> > If there is really only one additional differential equation, just add it after the n discretized PDE-equations as equation (n+1).
> >
> > Best wishes
> > Torsten.
>
> Thanks Torsten! I'll try the way you said. This should work well.
> Best wishes
> Douglas.


I'm still dealing with errors when running the code I developed. Actually the error arise because of a mistake of writing in my opinion. But I'm able to see what's wrong until now. My function is shown below:

function DXDt = testsol(t,X)
            
global N

N = 50;
r = 1;
dr = r/N;
Xeq = 1.7409;
rhoss = 1057;
rhoagua = 1000;
Do = 3e-11;
k1 = 1e-2;

F(1) = 3*Do*exp(k1*X(1))/(X(N)^2)*((2*X(2)-2*X(1))/(dr)^2);
            
for i = 2:N-2
     F(i) = Do*exp(k1*X(i))/(X(N)^2)*(((X(i+1)-X(i-1))/(2*dr))*...
            ((2/(i*dr))+k1*(X(i+1)-X(i-1))/(2*dr))+(X(i+1)-2*X(i)+X(i-1))/(dr)^2)+...
            (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)*((X(i+1)-X(i-1))/dr);
end
            
for i = N-1
     F(i) = Do*exp(abs(k1*X(i))/(X(N)))*(((Xeq-X(i-1))/(2*dr))*...
            ((2/(i*dr))+k1*(Xeq-X(i-1))/(2*dr))+(Xeq-2*X(i)+X(i-1))/(dr)^2)+...
            (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)^2;
end

F(N) = ((rhoss/rhoagua)*Do*exp(k1*X(N-1))/(X(N)))*(Xeq-X(N-2))/dr;

DXDt = F';

The main problem is that the last differential equation, defined in F(N) is not part of the system generated after using finite differences in the pde. This ode is the last one to be added that I mentioned before. However, the variable X(N) must represent the dependent variable of the last ode and it must appear in the system as well. When I run the whole code for the solution of my function I get this error message:
 "??? Attempted to access X(50); index out of bounds because numel(X)=1."
which is natural because X(N) is not calculated before. My problem is that X(N) is a variable that must appear in the whole system, but is also a dependent variable to be calculated by integration in time. That's the biggest mistake I'm making: I'm not sure how to write this function properly.

Subject: Writing a system of differential equations correctly

From: Torsten

Date: 17 Apr, 2013 06:46:06

Message: 5 of 7

>
> I'm still dealing with errors when running the code I developed. Actually the error arise because of a mistake of writing in my opinion. But I'm able to see what's wrong until now. My function is shown below:
>
> function DXDt = testsol(t,X)
>
> global N
>
> N = 50;
> r = 1;
> dr = r/N;
> Xeq = 1.7409;
> rhoss = 1057;
> rhoagua = 1000;
> Do = 3e-11;
> k1 = 1e-2;
>
> F(1) = 3*Do*exp(k1*X(1))/(X(N)^2)*((2*X(2)-2*X(1))/(dr)^2);
>
> for i = 2:N-2
> F(i) = Do*exp(k1*X(i))/(X(N)^2)*(((X(i+1)-X(i-1))/(2*dr))*...
> ((2/(i*dr))+k1*(X(i+1)-X(i-1))/(2*dr))+(X(i+1)-2*X(i)+X(i-1))/(dr)^2)+...
> (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)*((X(i+1)-X(i-1))/dr);
> end
>
> for i = N-1
> F(i) = Do*exp(abs(k1*X(i))/(X(N)))*(((Xeq-X(i-1))/(2*dr))*...
> ((2/(i*dr))+k1*(Xeq-X(i-1))/(2*dr))+(Xeq-2*X(i)+X(i-1))/(dr)^2)+...
> (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)^2;
> end
>
> F(N) = ((rhoss/rhoagua)*Do*exp(k1*X(N-1))/(X(N)))*(Xeq-X(N-2))/dr;
>
> DXDt = F';
>
> The main problem is that the last differential equation, defined in F(N) is not part of the system generated after using finite differences in the pde. This ode is the last one to be added that I mentioned before. However, the variable X(N) must represent the dependent variable of the last ode and it must appear in the system as well. When I run the whole code for the solution of my function I get this error message:
> "??? Attempted to access X(50); index out of bounds because numel(X)=1."
> which is natural because X(N) is not calculated before. My problem is that X(N) is a variable that must appear in the whole system, but is also a dependent variable to be calculated by integration in time. That's the biggest mistake I'm making: I'm not sure how to write this function properly.

You must solve your problem in _one_ complete system of ODEs, not by calling the ODE integrator twice - once for X and once for R (that's what I think you are trying to do ?).
Thus if the number of grid points is N, the dimension of the vector X and the dimension of the vector F from above must be N+1. Here, X(1) to X(N) represent your original solution variable X in the N grid points and element X(N+1) represents your solution variable R ; elements F(1) to F(N) represent the temporal derivatives of your solution variable X in the N grid points and element F(N+1) represents dR/dt.

Best wishes
Torsten.

Subject: Writing a system of differential equations correctly

From: Douglas Nicolin

Date: 17 Apr, 2013 11:53:07

Message: 6 of 7

"Torsten" wrote in message <kklgfe$g4l$1@newscl01ah.mathworks.com>...
> >
> > I'm still dealing with errors when running the code I developed. Actually the error arise because of a mistake of writing in my opinion. But I'm able to see what's wrong until now. My function is shown below:
> >
> > function DXDt = testsol(t,X)
> >
> > global N
> >
> > N = 50;
> > r = 1;
> > dr = r/N;
> > Xeq = 1.7409;
> > rhoss = 1057;
> > rhoagua = 1000;
> > Do = 3e-11;
> > k1 = 1e-2;
> >
> > F(1) = 3*Do*exp(k1*X(1))/(X(N)^2)*((2*X(2)-2*X(1))/(dr)^2);
> >
> > for i = 2:N-2
> > F(i) = Do*exp(k1*X(i))/(X(N)^2)*(((X(i+1)-X(i-1))/(2*dr))*...
> > ((2/(i*dr))+k1*(X(i+1)-X(i-1))/(2*dr))+(X(i+1)-2*X(i)+X(i-1))/(dr)^2)+...
> > (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)*((X(i+1)-X(i-1))/dr);
> > end
> >
> > for i = N-1
> > F(i) = Do*exp(abs(k1*X(i))/(X(N)))*(((Xeq-X(i-1))/(2*dr))*...
> > ((2/(i*dr))+k1*(Xeq-X(i-1))/(2*dr))+(Xeq-2*X(i)+X(i-1))/(dr)^2)+...
> > (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)^2;
> > end
> >
> > F(N) = ((rhoss/rhoagua)*Do*exp(k1*X(N-1))/(X(N)))*(Xeq-X(N-2))/dr;
> >
> > DXDt = F';
> >
> > The main problem is that the last differential equation, defined in F(N) is not part of the system generated after using finite differences in the pde. This ode is the last one to be added that I mentioned before. However, the variable X(N) must represent the dependent variable of the last ode and it must appear in the system as well. When I run the whole code for the solution of my function I get this error message:
> > "??? Attempted to access X(50); index out of bounds because numel(X)=1."
> > which is natural because X(N) is not calculated before. My problem is that X(N) is a variable that must appear in the whole system, but is also a dependent variable to be calculated by integration in time. That's the biggest mistake I'm making: I'm not sure how to write this function properly.
>
> You must solve your problem in _one_ complete system of ODEs, not by calling the ODE integrator twice - once for X and once for R (that's what I think you are trying to do ?).
> Thus if the number of grid points is N, the dimension of the vector X and the dimension of the vector F from above must be N+1. Here, X(1) to X(N) represent your original solution variable X in the N grid points and element X(N+1) represents your solution variable R ; elements F(1) to F(N) represent the temporal derivatives of your solution variable X in the N grid points and element F(N+1) represents dR/dt.
>
> Best wishes
> Torsten.


You are wright. But in my system, the variable X(N) represents what would be my R(1) let's say. Once in the position N, in my case, there is the time derivative dRdt, I considered that the variable X(N) is my "R" variable that appears all over the system and in the last equation. That's my real problem. If my X(N) were a variable solve as a part of the system formed by my pde, there would be no errors on my code. But it's not. Adapting the indexes for the whole system is the problem. In other problems, when the dependence of the system on the X(N) variable does not exist it is easy to write the correct function. I'm search for examples similar to may case day by day and until now, I got nothing. I found many articles showing similar problems but solved in a different way.

Kind regards
Douglas

Subject: Writing a system of differential equations correctly

From: Torsten

Date: 17 Apr, 2013 12:28:07

Message: 7 of 7

"Douglas Nicolin" <douglas.nicolin@gmail.com> wrote in message <kkm2f3$2se$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kklgfe$g4l$1@newscl01ah.mathworks.com>...
> > >
> > > I'm still dealing with errors when running the code I developed. Actually the error arise because of a mistake of writing in my opinion. But I'm able to see what's wrong until now. My function is shown below:
> > >
> > > function DXDt = testsol(t,X)
> > >
> > > global N
> > >
> > > N = 50;
> > > r = 1;
> > > dr = r/N;
> > > Xeq = 1.7409;
> > > rhoss = 1057;
> > > rhoagua = 1000;
> > > Do = 3e-11;
> > > k1 = 1e-2;
> > >
> > > F(1) = 3*Do*exp(k1*X(1))/(X(N)^2)*((2*X(2)-2*X(1))/(dr)^2);
> > >
> > > for i = 2:N-2
> > > F(i) = Do*exp(k1*X(i))/(X(N)^2)*(((X(i+1)-X(i-1))/(2*dr))*...
> > > ((2/(i*dr))+k1*(X(i+1)-X(i-1))/(2*dr))+(X(i+1)-2*X(i)+X(i-1))/(dr)^2)+...
> > > (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)*((X(i+1)-X(i-1))/dr);
> > > end
> > >
> > > for i = N-1
> > > F(i) = Do*exp(abs(k1*X(i))/(X(N)))*(((Xeq-X(i-1))/(2*dr))*...
> > > ((2/(i*dr))+k1*(Xeq-X(i-1))/(2*dr))+(Xeq-2*X(i)+X(i-1))/(dr)^2)+...
> > > (i*dr/X(N)^2)*(rhoss/rhoagua)*Do*exp(k1*X(N-1))*((Xeq-X(N-2))/dr)^2;
> > > end
> > >
> > > F(N) = ((rhoss/rhoagua)*Do*exp(k1*X(N-1))/(X(N)))*(Xeq-X(N-2))/dr;
> > >
> > > DXDt = F';
> > >
> > > The main problem is that the last differential equation, defined in F(N) is not part of the system generated after using finite differences in the pde. This ode is the last one to be added that I mentioned before. However, the variable X(N) must represent the dependent variable of the last ode and it must appear in the system as well. When I run the whole code for the solution of my function I get this error message:
> > > "??? Attempted to access X(50); index out of bounds because numel(X)=1."
> > > which is natural because X(N) is not calculated before. My problem is that X(N) is a variable that must appear in the whole system, but is also a dependent variable to be calculated by integration in time. That's the biggest mistake I'm making: I'm not sure how to write this function properly.
> >
> > You must solve your problem in _one_ complete system of ODEs, not by calling the ODE integrator twice - once for X and once for R (that's what I think you are trying to do ?).
> > Thus if the number of grid points is N, the dimension of the vector X and the dimension of the vector F from above must be N+1. Here, X(1) to X(N) represent your original solution variable X in the N grid points and element X(N+1) represents your solution variable R ; elements F(1) to F(N) represent the temporal derivatives of your solution variable X in the N grid points and element F(N+1) represents dR/dt.
> >
> > Best wishes
> > Torsten.
>
>
> You are wright. But in my system, the variable X(N) represents what would be my R(1) let's say. Once in the position N, in my case, there is the time derivative dRdt, I considered that the variable X(N) is my "R" variable that appears all over the system and in the last equation. That's my real problem. If my X(N) were a variable solve as a part of the system formed by my pde, there would be no errors on my code. But it's not. Adapting the indexes for the whole system is the problem. In other problems, when the dependence of the system on the X(N) variable does not exist it is easy to write the correct function. I'm search for examples similar to may case day by day and until now, I got nothing. I found many articles showing similar problems but solved in a different way.
>
> Kind regards
> Douglas

Write the solution vector X in local variables:

function DXDt = testsol(t,X)
             
global N
 
N = 50;
r = 1;
dr = r/N;
Xeq = 1.7409;
rhoss = 1057;
rhoagua = 1000;
Do = 3e-11;
k1 = 1e-2;

X_pde=zeros(N-1);

For i=1:N-1
  X_pde(i)=X(i);
end
R=X(N);
...

Now you can work with X_pde and R.
Does this solve your problem ?

Best wishes
Torsten.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us