Error with brackets symsum

2 views (last 30 days)
Aaron
Aaron on 22 Dec 2022
Edited: Aaron on 23 Dec 2022
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

Answers (1)

Alan Stevens
Alan Stevens on 22 Dec 2022
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 Comments
Alan Stevens
Alan Stevens on 22 Dec 2022
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 on 22 Dec 2022
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

Sign in to comment.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!