Solving System of ODEs using for cycles to reproduce summing
6 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Torsten
2015-2-11
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 个评论
Torsten
2015-2-11
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!