Error with brackets symsum

2 ビュー (過去 30 日間)
Aaron
Aaron 2022 年 12 月 22 日
編集済み: Aaron 2022 年 12 月 23 日
f2 = figure;
figure(f2);
ic = linspace(0, 100, K);
[t, n] = ode 45(@(t, n) ODEfunc(n, K);
k_bar = 1/N * symsum(i*n(i),i,0,K);
**I want to solve some ODES eventually with k_bar
I keep getting errors with brackets, im not sure If im tired but i cant spot where its referring to!
Thanks in advance

回答 (1 件)

Alan Stevens
Alan Stevens 2022 年 12 月 22 日
Do you mean something more like this?
N = 100; % Total number of particles
one = 1;
alpha = 1;
K = 7; % 7 states
n0 = 4*ones(K,1);
tspan = 0:0.1:5;
[t, n] = ode45(@(t, n) ODEfunc(t, n, K, one, N, alpha), tspan, n0);
plot(t, n),grid
xlabel('time'), ylabel('n')
legend('n1','n2','n3','n4','n5','n6','n7')
function dndt = ODEfunc(~, n, K, one, N, alpha)
S = n(1);
for i=2:K
S = i*n(i) + S;
end
k_bar = S/N;
dndt = zeros(K,1);
dndt(1,:) = alpha*n(1)*k_bar - one*n(1); % Note that this is for state 1.
for i= 2:K-1
dndt(i,:) = alpha*n(i)*(k_bar - i) + one*n(i-1) - one*n(i); % For state 2 to 6
end
dndt(K,:) = alpha*n(K,1)*(k_bar - K) + one*n(K-1,1); % For state 7
end
  2 件のコメント
Alan Stevens
Alan Stevens 2022 年 12 月 22 日
If N is constant I would expect the sum of dndt over all states to be zero, but it doesn't seem to be.
Aaron
Aaron 2022 年 12 月 22 日
Hi, I just realised I forgot to add a -1 with the k_bar, making (k_bar-1) in the first term. It now converges to nice values. thanks for the help on the syntax

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by