Error with brackets symsum

1 次查看(过去 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 CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by