How can I make this code more efficient?
Show older comments
Hello,
Because of the large number of differential equations I'm trying to solve, I would like to make this code more efficient. I'm using for loops and I think there might be a more efficient way to approach this problem, but I ran out of ideas.
clear all
global kc n
kc=0.0156
t_final=4000; %Input final time for calculations [s]
% Define time range of interest
t_span=[0 t_final] ; % Time span (unit depends on kC and Mi)
% Define number of Mi; number of coalescence
n=1e5; % Number of Mi (i numbers)
M1=4.473e5; % Initial concentration at M1
Mi=[M1 ones(1,n-1).*1e-5]; % Matrix to combine initial conditions (Assume zero everywhere other than M1)
options = odeset('RelTol',1e-14);
[t_solution, Mi]=ode113(@Coalescence_Function_Reaction3, t_span, Mi, options);
and the function is
function dM = Coalescence_Function_Reaction3(t,M)
global kc n
for i=1:n
summation_1=sum(M(1:n)); % The first summation
%summation_2
summation_2=0;
for k=1:i-1
summation_2=summation_2+M(k).*M(i-k);
end
dM(i)=-kc.*summation_1.*M(i)+1/2.*kc.*M(i).^2+1/2.*kc.*summation_2;
end
dM=dM';
end
I need to run this for a large number of n's (>1e5) because my total final concentration is not equaling my initial concentration. (In other words, M1=/=total of Mi at t_final) Please let me know if you have any suggestions and thank you.
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!