Clear Filters
Clear Filters

Solving System of ODEs using for cycles to reproduce summing

3 views (last 30 days)
Dear all,
Thank you first all for your attention reading this post.
I am a kind of new in Matlab so it might be that the answer to my question is really easy but I don't see the solution for the moment.
I want to solve a system of coupled differential equations that in mathematical form are presented below.
I am creating a function for this system that looks like
function dXdz = coupled_ODE(z,X,y,n,alpha,beta,J,Pf,uf)
m = length(X); % Defines the number of elements of the independent variable
dXdz = zeros(m,1); % Initializes the column vector of the elements to the left of the ODE
for i = 1:n
dXdz(1) = dXdz(1) - alpha*J(i)*(Pf*X(i+1) - X(m)*y(i));
end
for i = 1:n
dXdz(i+1) = (-1/X(1))*(X(i+1)*dXdz(1) + alpha*J(i)*(Pf*X(i+1) - X(m)*y(i)));
end
dXdz(m) = beta*(uf - X(1))/X(m);
where the parameters from y to uf are given as inputs, since after each iteration in the main code those are updated. Notice also that alpha and beta are constants.
This function is called in the main body as
[Z, Xout] = ode45(@coupled_ODE,zspan,Xin,[],yi,num_species,alpha,beta,J,Pf,uf);
I am getting problems in the results I obtain. They are not at all what I expect. The output of this ode45 is always the same at each iteration, indicating that no numerical integration of the ODEs is being performed at all.
I don't see where the problem can be. I might suspect that is coming from this for cycles I introduced to reproduce the summings, but I am not sure. Any idea?
Hopefully, you can see the error (and I would be not surprised if it is an amateur mistake).
Thank you all! Greetings
  1 Comment
rantunes
rantunes on 11 Feb 2015
Edited: rantunes on 11 Feb 2015
I just want to add that the input vector X is a sex-elements column vector, with the elements
X = [u xi p]';
where xi is a vector of 4 elements. This explains why I used "X(i+1)", for instance.

Sign in to comment.

Accepted Answer

Torsten
Torsten on 11 Feb 2015
Change the call to ODE45 to
[Z,Xout]=ode45(@(z,X)coupled_ODE(z,X,yi,num_species,alpha,beta,J,Pf,uf),zspan,Xin);
Best wishes
Torsten.
  15 Comments
Torsten
Torsten on 11 Feb 2015
Don't think in iterations, think in states.
If the parameters yi,.... can be calculated from the states of u, xi and p (which are given variables in coupled_ODE), you can calculate dXdz.
Best wishes
Torsten.
rantunes
rantunes on 15 Feb 2015
Finally I figured out what was the problem. It was a value that I was considering wrong in terms of units that were providing very low dXdz and thus no "visible" integration.
The implementation above was always correct.
Thanks. Rodrigo

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!