Family of ODE with summation

1 vue (au cours des 30 derniers jours)
Sara  Crosetto
Sara Crosetto le 23 Déc 2020
Commenté : Alan Stevens le 23 Déc 2020
I need to solve this family of ODE with summation inside and I think I'm not using properly ODE45. I really need some help.
Where K_Br is a matrix of known values previously calculated.
I wrote a function to define the system of ODE:
function ndot = System_ni (t,n)
ndot=zeros(M,1);
n=zeros(M,1);
V=zeros(M,1);
for i = 1:M
for j=1:i-1
V(j)=K_Br(j,i-j).*n(j);
end
nw(i)=1/2*sum(V(1:i-1).*n(1:i-1));
for j=1:M
VV(j)=K_Br(i,j).*n(j);
end
nnw(i)=n(i)*sum(VV(1:M));
ndot(i) =nw(i)+nnw(i);
end
ndot(1)=n(1)*sum(K_Br(1,:)*n(:));
end
And I'm recalling in in ODE45 with the intial condition.
cond_in=[N_in;zeros(M-1,1)];
[t,n] = ode45(@System_ni,[0 T],cond_in);
But something it's not working and I think I'm making some mistakes with ODE45.
Thank you in advance for any help!
  2 commentaires
Walter Roberson
Walter Roberson le 23 Déc 2020
It is not obvious to me why you are overwriting the already-calculated ndot(1) at the end ?
Sara  Crosetto
Sara Crosetto le 23 Déc 2020
Because equation is different for ndot(1) since first summation is zero.
Actually, I should have written it at the very beginning, I think.

Connectez-vous pour commenter.

Réponses (1)

Alan Stevens
Alan Stevens le 23 Déc 2020
Your System_ni function doesn't seem to have acces to your K_Br matrix. You need to pass it into System_ni or specify it within System_ni.
  6 commentaires
Sara  Crosetto
Sara Crosetto le 23 Déc 2020
Modifié(e) : Sara Crosetto le 23 Déc 2020
The error is:
Warning: Failure at t=1.066340e+01. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (2.842171e-14) at
time t.
Solution is not correct for time <t=1.066340e+01, anyway.
The whole code and the function are attached. Thank you so much for your kind help!
Alan Stevens
Alan Stevens le 23 Déc 2020
Try the following (note: I've included the System_ni function at the end of the Report8 script. You will need to decide if the end result is sensible.
N_in=1.7521e+14;
N=100;
T=500;
Lo=1.68e-07;
M=500;
L=20*Lo;
l=linspace(Lo,L,M);
Kb=1.38e-23;
eta_w=9e-04;
Temp=337.15;
C=(2*Kb*Temp)/(3*eta_w);
K_Br=zeros(M,M);
for i=1:M
for j=1:M
K_Br(i,j)=C*((l(i)+l(j))^2/(l(i)*l(j)));
end
end
cond_in=[N_in;zeros(M-1,1)];
[t,n] = ode45(@(t,n) System_ni(t,n,K_Br),[0 T],cond_in); % Use this form to pass an extra parameter to System_ni
plot(t,n(:,1))
function ndot = System_ni (~,n,K_Br) % Take K_Br from previous calcuation rather than recalculating it.
M = size(K_Br,1);
ndot=zeros(M,1);
V=zeros(M,1);
nnw=zeros(M,1);
nw=zeros(M,1);
for i = 1:M
for j=1:i-1
V(j)=K_Br(j,i-j).*n(j);
end
nw(i)=1/2*sum(V(1:i-1).*n(1:i-1));
VV = K_Br(i,:)*n; % row vector * column vector automatically does summation
nnw(i)=n(i)*VV; % VV is a single value
ndot(i) = nw(i) - nnw(i); % nnw needs negative sign
end
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by