Solving System of ODEs using for cycles to reproduce summing
2 views (last 30 days)
Show older comments
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
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!